@ai-matrx/records 0.44.1 → 0.46.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.
@@ -28,6 +28,7 @@ __export(react_exports, {
28
28
  CONTEXT_POLICIES: () => CONTEXT_POLICIES,
29
29
  DELIVERIES: () => DELIVERIES,
30
30
  ENTITY_TOKENS: () => ENTITY_TOKENS,
31
+ FIELD_KINDS: () => FIELD_KINDS,
31
32
  FIELD_SENSITIVITIES: () => FIELD_SENSITIVITIES,
32
33
  FIELD_SOURCES: () => FIELD_SOURCES,
33
34
  FRESHNESS: () => FRESHNESS,
@@ -66,7 +67,11 @@ __export(react_exports, {
66
67
  choiceSlug: () => choiceSlug,
67
68
  choiceValuesOf: () => choiceValuesOf,
68
69
  choicesOf: () => choicesOf,
70
+ coerceTypedAnswer: () => coerceTypedAnswer,
71
+ coerceTypedAnswers: () => coerceTypedAnswers,
72
+ coercionLabel: () => coercionLabel,
69
73
  err: () => err,
74
+ fieldKindFor: () => fieldKindFor,
70
75
  forgetAllOps: () => forgetAllOps,
71
76
  highestLevel: () => highestLevel,
72
77
  holdsARetiredChoice: () => holdsARetiredChoice,
@@ -565,6 +570,9 @@ var STORE_DOORS = [
565
570
  { name: "parity_values", args: "p_organization_id uuid, p_record_id uuid", returns: "TABLE(field_key text, field_id uuid, parity_type text, behavior text, unit text, format text, value jsonb, value_version integer, actor text, written_at timestamp with time zone)", security: "invoker" },
566
571
  { name: "per_value_access_words", args: "", returns: "text[]", security: "invoker" },
567
572
  { name: "person_kernel_id", args: "", returns: "uuid", security: "invoker" },
573
+ { name: "phone_canonical", args: "p_text text", returns: "text", security: "invoker" },
574
+ { name: "phone_display", args: "p_text text", returns: "text", security: "invoker" },
575
+ { name: "phone_pattern", args: "", returns: "text", security: "invoker" },
568
576
  { name: "pin_agent_cells", args: "p_new jsonb, p_old jsonb, p_agent_keys text[]", returns: "jsonb", security: "invoker" },
569
577
  { name: "pipeline_board", args: "p_organization_id uuid, p_table_id uuid, p_measure text DEFAULT NULL::text", returns: "TABLE(stage_key text, stage_label text, stage_position integer, cards bigint, total numeric, wip_limit integer, over_limit boolean)", security: "definer" },
570
578
  { name: "pipeline_declare", args: "p_organization_id uuid, p_table_id uuid, p_spec jsonb", returns: "jsonb", security: "definer" },
@@ -586,6 +594,7 @@ var STORE_DOORS = [
586
594
  { name: "portal_record_title", args: "p_organization_id uuid, p_record_id uuid", returns: "text", security: "definer" },
587
595
  { name: "portal_revoke", args: "p_organization_id uuid, p_portal_id uuid, p_principal_id uuid", returns: "jsonb", security: "definer" },
588
596
  { name: "portal_slug", args: "p_organization_id uuid, p_title text, p_portal_id uuid DEFAULT NULL::uuid", returns: "text", security: "definer" },
597
+ { name: "portal_tables", args: "p_organization_id uuid", returns: "TABLE(portal_id uuid, table_id uuid, name text)", security: "definer" },
589
598
  { name: "portals", args: "p_organization_id uuid", returns: "TABLE(portal_id uuid, title text, slug text, client_table_id uuid, client_table text, is_active boolean, tables integer, invited integer, signed_in integer, sign_in_method text, opened_at timestamp with time zone)", security: "definer" },
590
599
  { name: "presentation_kernel_id", args: "", returns: "uuid", security: "invoker" },
591
600
  { name: "promote_field", args: "p_organization_id uuid, p_table_id uuid, p_field_id uuid", returns: "jsonb", security: "invoker" },
@@ -864,7 +873,7 @@ var STORE_REFUSAL_CODES = [
864
873
  { code: "22004", raised_by: "_entity_custom_fields_guard,_value_envelope,actor_word,agg_assert_key,agg_sql,anon_capture,anon_token_issue,anon_write,assert_client_may_reach,booking_declare,capture_submit,checklist_declare,context_resolve,dashboard_block_normalize,dashboard_declare,dashboard_window_sql,doc_render_write,doc_sign,doc_signature_write,doc_template_save,enrich_land,enrich_normalize,external_history_event,external_rows,external_source_declare,external_stub_upsert,external_write_through,external_writes_set,field_history,form_declare,form_submit,home_add,io_comment_write,io_import_begin,io_infer_column,io_outbox_drain,io_restore,migrate_choice_keys,migrate_purge,migrate_reclass,migrate_split,migrate_undo,organization_clear,organization_contents,pipeline_declare,pipeline_gate_preview,portal_declare,portal_invite,portal_principal_bind,provenance_retention_days,provenance_write,query_across_homes,query_by_coordinates,query_rollup,record_delete,record_reparent,record_resolve,record_restore,record_update,record_write,record_write_many,relation_carry,relation_own,rule_declare,share_grant,sign_request_create,sign_request_sign,subscription_declare,table_declare,table_share_outside_invite,value_restore,view_declare,work_approval_decide,work_approval_request,work_template_declare" },
865
874
  { code: "22007", raised_by: "dashboard_window_sql" },
866
875
  { code: "22012", raised_by: "rule_eval" },
867
- { code: "22023", raised_by: "_booking_availability,_entity_custom_fields_guard,_take_op_id,_value_envelope,_work_shape_guard,actor_word,agg_assert_key,agg_sql,anon_token_issue,booking_declare,containment_parent,dashboard_block_normalize,dashboard_window_sql,doc_render_body,doc_render_write,doc_signature_write,enrich_declare,enrich_land,enrich_normalize,enrich_pin,entity_value_write,external_history_event,external_source_declare,external_stub_upsert,external_write_through,field_history,io_export,io_import_begin,io_import_finish,io_import_plan,io_import_report,io_proposal_accept,migrate_merge,migrate_reclass,my_levels,organization_clear,page_size,pin_agent_cells,pipeline_move,portal_declare,provenance_write,query_by_coordinates,query_rollup,record_update,record_write_many,relation_carry,rule_eval,rule_members,share_grant,share_lane_set,sign_request_create,sign_request_sign,subscription_declare,table_rules,visibility_as_of,work_approval_request,work_list,work_slot_hold" },
876
+ { code: "22023", raised_by: "_booking_availability,_entity_custom_fields_guard,_take_op_id,_value_envelope,_work_shape_guard,actor_word,agg_assert_key,agg_sql,anon_token_issue,booking_declare,containment_parent,dashboard_block_normalize,dashboard_window_sql,doc_render_body,doc_render_write,doc_signature_write,enrich_declare,enrich_land,enrich_normalize,enrich_pin,entity_value_write,external_history_event,external_source_declare,external_stub_upsert,external_write_through,field_history,field_update,io_export,io_import_begin,io_import_finish,io_import_plan,io_import_report,io_proposal_accept,migrate_merge,migrate_reclass,my_levels,organization_clear,page_size,pin_agent_cells,pipeline_move,portal_declare,provenance_write,query_by_coordinates,query_rollup,record_update,record_write_many,relation_carry,rule_eval,rule_members,share_grant,share_lane_set,sign_request_create,sign_request_sign,subscription_declare,table_rules,visibility_as_of,work_approval_request,work_list,work_slot_hold" },
868
877
  { code: "23503", raised_by: "_booking_availability,_checklist_instantiate,_containment_guard,_display_spec_for,_field_document_for,_pipeline_stage_key,_record_rule_uses,agg_digest_assemble,anon_publish,anon_rate_take,anon_token_issue,anon_write,assert_organization_wall,booking_cancel,booking_confirm,booking_declare,booking_hold,booking_reschedule,capture_publish,checklist_declare,checklist_steps_table,comment_write,dashboard_declare,delete_rule,doc_sign,doc_template_save,enrich_declare,enrich_due,enrich_land,form_declare,form_submit,home_add,inbound_declare,inbound_mail_land,inbound_set,io_comment_write,io_import_begin,io_import_finish,io_import_report,io_import_rows,io_proposal_accept,migrate_demote,migrate_promote,pipeline_board,pipeline_declare,pipeline_move,pipeline_read,pipeline_transition_refusal,promote_field,promote_table,record_applicability,record_reparent,relation_carry,relation_own,resolve_first_match,rule_applies,rule_declare,rule_eval,rule_members,rule_run,sign_request_create,subscription_mute,subscription_preview,view_declare,work_set_state,work_take_assignment,work_template_instantiate" },
869
878
  { code: "23505", raised_by: "_unique_rule_holds,doc_sign,doc_signature_write,entity_field_declare,field_declare,home_add,inbound_declare,io_proposal_accept,relation_carry,relation_own,share_grant,sign_request_cancel,sign_request_create,table_share_outside_invite,table_share_outside_resend,work_approval_decide,work_slot_hold" },
870
879
  { code: "23514", raised_by: "_checklist_instantiate,_checklist_step_guard,_containment_guard,_dated_values_guard,_derived_fields,_display_spec_for,_entity_custom_fields_guard,_field_class_guard,_field_document_for,_field_shape_guard,_field_type_parity_guard,_merge_field_shape_guard,_merge_field_temporal_guard,_promoted_field_cap_guard,_record_rule_uses,_resolve_choice_words,_rule_shape_guard,_rule_topology_guard,_store_relation_edge_names_its_field,_table_columns_word_guard,_table_shape_guard,_undeclared_key_guard,_value_envelope,_with_display,_work_shape_guard,_workdoors_approval_guard,assert_columns_are_defined,assert_entity_is_organization_scoped,booking_declare,capture_publish,capture_sheet_declare,checklist_declare,checklist_step_complete,doc_sign,doc_signature_write,doc_template_save,entity_field_declare,entity_field_retire,entity_field_update,entity_records_find,entity_table,field_declare,field_retire,field_update,home_add,io_import_rows,migrate_merge,relation_carry,relation_own,sign_request_create,sign_request_sign,table_declare,validate_value_envelope,validate_values,work_set_state,work_slots_declare,work_take_assignment,work_template_instantiate" },
@@ -1764,6 +1773,7 @@ var ENTITY_TOKENS = [
1764
1773
  "template_context_items",
1765
1774
  "template_scope_types",
1766
1775
  "templates",
1776
+ "test_handset_inbox",
1767
1777
  "thread",
1768
1778
  "tool",
1769
1779
  "tool_binding",
@@ -2128,6 +2138,14 @@ var DOORS = {
2128
2138
  // pair (`custom.portal_public`, `custom.portal_invitation`, `custom.portal_me`)
2129
2139
  // is deliberately absent here: those belong to the outsider's own shell.
2130
2140
  portals: "portals",
2141
+ // WHICH TABLES EACH PORTAL SHOWS (TAILS-3). The Portals RAIL lives on a
2142
+ // Table's page beside Forms, Bookings and Dashboards, all of which are
2143
+ // table-scoped; `custom.portals` takes no table, so an organization with one
2144
+ // portal over Jobs saw that portal's cards on its Parts table, the empty
2145
+ // state never rendered, and the whole build/ask offer was unreachable
2146
+ // everywhere in that organization. This is the one question the rail has to
2147
+ // ask, under the same wall and visibility gate as `portals` itself.
2148
+ portalTables: "portal_tables",
2131
2149
  portalCard: "portal_card",
2132
2150
  portalDeclare: "portal_declare",
2133
2151
  portalInvite: "portal_invite",
@@ -3185,6 +3203,9 @@ function createRecordsClient(config) {
3185
3203
  async portals() {
3186
3204
  return callDoor(DOORS.portals, { p_organization_id: org }, "portals");
3187
3205
  },
3206
+ async portalTables() {
3207
+ return callDoor(DOORS.portalTables, { p_organization_id: org }, "portalTables");
3208
+ },
3188
3209
  async portalCard({ portal_id }) {
3189
3210
  return callDoor(
3190
3211
  DOORS.portalCard,
@@ -4821,6 +4842,209 @@ function looksLikeAnId(value) {
4821
4842
  return typeof value === "string" && UUID.test(value);
4822
4843
  }
4823
4844
 
4845
+ // src/coerce.ts
4846
+ var FIELD_KINDS = [
4847
+ "attachment",
4848
+ "checkbox",
4849
+ "currency",
4850
+ "datetime",
4851
+ "email",
4852
+ "formula",
4853
+ "json",
4854
+ "long_text",
4855
+ "lookup",
4856
+ "member",
4857
+ "multi_select",
4858
+ "number",
4859
+ "percent",
4860
+ "phone",
4861
+ "relation",
4862
+ "rollup",
4863
+ "select",
4864
+ "text",
4865
+ "url"
4866
+ ];
4867
+ function fieldKindFor(field, kernel) {
4868
+ const format = (field.format ?? "").toLowerCase();
4869
+ const config = field.config ?? {};
4870
+ switch (field.type) {
4871
+ case "boolean":
4872
+ return "checkbox";
4873
+ case "text":
4874
+ if (format === "email") return "email";
4875
+ if (format === "phone") return "phone";
4876
+ if (format === "url") return "url";
4877
+ if (format === "json" || config["json"] === true) return "json";
4878
+ if (format === "long" || config["multiline"] === true) return "long_text";
4879
+ return "text";
4880
+ case "range": {
4881
+ const kind = String(config["kind"] ?? "number");
4882
+ if (kind === "date" || kind === "datetime") return "datetime";
4883
+ if (format === "currency") return "currency";
4884
+ if (format === "percent" || field.unit === "%") return "percent";
4885
+ return "number";
4886
+ }
4887
+ case "list":
4888
+ return field.multi ? "multi_select" : "select";
4889
+ case "relation": {
4890
+ const parity = String(config["parity"] ?? "");
4891
+ if (parity === "attachment" || parity === "member") return parity;
4892
+ if (kernel?.file && field.relation_target === kernel.file) return "attachment";
4893
+ if (kernel?.person && field.relation_target === kernel.person) return "member";
4894
+ return "relation";
4895
+ }
4896
+ case "formula": {
4897
+ const via = config["via"];
4898
+ if (via && config["agg"]) return "rollup";
4899
+ if (via) return "lookup";
4900
+ return "formula";
4901
+ }
4902
+ default:
4903
+ return "text";
4904
+ }
4905
+ }
4906
+ var TRUE_WORDS = /* @__PURE__ */ new Set(["true", "yes", "y", "1", "\u2713", "x", "checked", "on"]);
4907
+ var FALSE_WORDS = /* @__PURE__ */ new Set(["false", "no", "n", "0", "", "unchecked", "off"]);
4908
+ function coercionLabel(field) {
4909
+ const label = (field.label ?? "").trim();
4910
+ if (label !== "") return label;
4911
+ const name = (field.name ?? "").trim();
4912
+ if (name !== "") return name;
4913
+ return field.key.replace(/[_-]+/g, " ").replace(/\b\w/g, (c) => c.toUpperCase()).trim();
4914
+ }
4915
+ function coerceTypedAnswer(field, raw, opts = {}) {
4916
+ const label = opts.label ?? coercionLabel(field);
4917
+ const text = raw.trim();
4918
+ const kind = fieldKindFor(field, opts.kernel);
4919
+ if (text === "" && kind !== "checkbox") return { value: null };
4920
+ switch (kind) {
4921
+ case "checkbox": {
4922
+ const word = text.toLowerCase();
4923
+ if (TRUE_WORDS.has(word)) return { value: true };
4924
+ if (FALSE_WORDS.has(word)) return { value: word === "" ? null : false };
4925
+ return {
4926
+ 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.`
4927
+ };
4928
+ }
4929
+ case "number":
4930
+ case "currency":
4931
+ case "percent": {
4932
+ const negative = /^\(.*\)$/.test(text);
4933
+ const bare = text.replace(/^\(|\)$/g, "").replace(/[\s,]/g, "").replace(/^[^\d.\-+]+/, "").replace(/[^\d.\-+eE]+$/, "");
4934
+ const n = Number(bare);
4935
+ if (bare === "" || Number.isNaN(n)) {
4936
+ return {
4937
+ 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.`
4938
+ };
4939
+ }
4940
+ return { value: negative ? -n : n };
4941
+ }
4942
+ case "datetime": {
4943
+ const wantsTime = String(field.config?.["kind"] ?? "date") === "datetime";
4944
+ const plainDay = /^\s*(\d{4})-(\d{2})-(\d{2})\s*$/.exec(raw);
4945
+ if (!wantsTime && plainDay) return { value: `${plainDay[1]}-${plainDay[2]}-${plainDay[3]}` };
4946
+ const at = Date.parse(text);
4947
+ if (Number.isNaN(at)) {
4948
+ return {
4949
+ 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.`
4950
+ };
4951
+ }
4952
+ const parsed = new Date(at);
4953
+ if (wantsTime) return { value: parsed.toISOString() };
4954
+ const y = parsed.getFullYear();
4955
+ const m = String(parsed.getMonth() + 1).padStart(2, "0");
4956
+ const d = String(parsed.getDate()).padStart(2, "0");
4957
+ return { value: `${y}-${m}-${d}` };
4958
+ }
4959
+ case "json": {
4960
+ try {
4961
+ return { value: JSON.parse(text) };
4962
+ } catch {
4963
+ return {
4964
+ refusal: `${label} holds a document, and \u201C${raw}\u201D is not valid JSON. Write the whole object, braces and all.`
4965
+ };
4966
+ }
4967
+ }
4968
+ case "select":
4969
+ case "multi_select": {
4970
+ if (!opts.options) {
4971
+ return {
4972
+ 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.`
4973
+ };
4974
+ }
4975
+ const wanted = kind === "multi_select" ? text.split(/[,;]/).map((w) => w.trim()).filter((w) => w.length > 0) : [text];
4976
+ if (wanted.length === 0) return { value: kind === "multi_select" ? [] : null };
4977
+ const matched = [];
4978
+ const unmatched = [];
4979
+ for (const word of wanted) {
4980
+ const hit = opts.options.byWord.get(word.toLowerCase());
4981
+ if (hit === void 0) unmatched.push(word);
4982
+ else matched.push(hit);
4983
+ }
4984
+ if (unmatched.length > 0) {
4985
+ const offered = opts.options.labels.length > 0 ? opts.options.labels.join(", ") : "it has none yet";
4986
+ return {
4987
+ refusal: `${label} does not have ${unmatched.map((w) => `\u201C${w}\u201D`).join(" or ")} among its options. It offers: ${offered}.`,
4988
+ unmatched
4989
+ };
4990
+ }
4991
+ return { value: kind === "multi_select" ? matched : matched[0] };
4992
+ }
4993
+ // AN IDENTITY IS NOT A WORD. Writing the word would put a string where an
4994
+ // id belongs, and every filter, colour and rollup built on that column
4995
+ // would stop agreeing with it. So it is refused and the person is told
4996
+ // which control does hold it.
4997
+ case "member":
4998
+ return {
4999
+ refusal: `${label} names a person in this organization, so it is chosen rather than typed. Open it and pick them.`
5000
+ };
5001
+ case "attachment":
5002
+ return {
5003
+ refusal: `${label} holds a file, which cannot arrive as typed-in text. Open it and attach the file.`
5004
+ };
5005
+ case "relation":
5006
+ return {
5007
+ refusal: `${label} points at another record, so it is chosen rather than typed. Open it and pick the record.`
5008
+ };
5009
+ case "formula":
5010
+ case "lookup":
5011
+ case "rollup":
5012
+ return {
5013
+ refusal: `${label} is worked out by the system, so nothing can be written into it.`
5014
+ };
5015
+ default:
5016
+ return { value: raw };
5017
+ }
5018
+ }
5019
+ function coerceTypedAnswers(fields, answers, opts = {}) {
5020
+ const byKey = new Map(fields.map((f) => [f.key, f]));
5021
+ const values = {};
5022
+ const refusals = {};
5023
+ const unknownKeys = [];
5024
+ for (const [key, given] of Object.entries(answers)) {
5025
+ const field = byKey.get(key);
5026
+ if (!field) {
5027
+ unknownKeys.push(key);
5028
+ values[key] = given;
5029
+ continue;
5030
+ }
5031
+ if (typeof given !== "string") {
5032
+ values[key] = given;
5033
+ continue;
5034
+ }
5035
+ const opt = {};
5036
+ const choices = opts.options?.get(key);
5037
+ if (choices) opt.options = choices;
5038
+ if (opts.kernel) opt.kernel = opts.kernel;
5039
+ const label = opts.labels?.[key];
5040
+ if (label) opt.label = label;
5041
+ const answered = coerceTypedAnswer(field, given, opt);
5042
+ if ("refusal" in answered) refusals[key] = answered.refusal;
5043
+ else values[key] = answered.value;
5044
+ }
5045
+ return { values, refusals, unknownKeys };
5046
+ }
5047
+
4824
5048
  // src/pipeline.ts
4825
5049
  var PIPELINE_RULE_KINDS = ["moves", "requires", "who", "limit", "entry"];
4826
5050
  var STAGE_RULE_ON_FAIL = ["refuse", "require_approval"];