@elementumai/edk 0.8.0-preview.11 → 0.8.2-alpha.5

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.
Files changed (180) hide show
  1. package/README.md +510 -324
  2. package/dist-sdk/src/catalog/ai-provider-models.d.ts +30 -0
  3. package/dist-sdk/src/catalog/ai-provider-models.js +63 -0
  4. package/dist-sdk/src/catalog/factories.d.ts +56 -3
  5. package/dist-sdk/src/catalog/factories.js +83 -4
  6. package/dist-sdk/src/catalog/from-authoring.js +19 -2
  7. package/dist-sdk/src/catalog/index.d.ts +3 -2
  8. package/dist-sdk/src/catalog/index.js +3 -2
  9. package/dist-sdk/src/catalog/org.d.ts +1 -1
  10. package/dist-sdk/src/catalog/org.js +134 -50
  11. package/dist-sdk/src/catalog/render.d.ts +13 -1
  12. package/dist-sdk/src/catalog/render.js +21 -4
  13. package/dist-sdk/src/catalog/token.js +18 -0
  14. package/dist-sdk/src/catalog/types.d.ts +75 -3
  15. package/dist-sdk/src/catalog/types.js +3 -0
  16. package/dist-sdk/src/core/system-fields.d.ts +2 -0
  17. package/dist-sdk/src/core/system-fields.js +2 -0
  18. package/dist-sdk/src/core/types/ai-provider.d.ts +10 -23
  19. package/dist-sdk/src/core/types/ai-provider.js +1 -25
  20. package/dist-sdk/src/core/types/app.d.ts +26 -1
  21. package/dist-sdk/src/engine/build/context.d.ts +58 -0
  22. package/dist-sdk/src/engine/build/context.js +11 -1
  23. package/dist-sdk/src/engine/build/source-context.d.ts +45 -0
  24. package/dist-sdk/src/engine/build/source-context.js +23 -0
  25. package/dist-sdk/src/engine/pull/context.d.ts +15 -0
  26. package/dist-sdk/src/engine/pull/org.d.ts +10 -0
  27. package/dist-sdk/src/engine/pull/org.js +34 -4
  28. package/dist-sdk/src/engine/tf/index.d.ts +2 -2
  29. package/dist-sdk/src/engine/tf/index.js +2 -2
  30. package/dist-sdk/src/engine/tf/labels.d.ts +54 -0
  31. package/dist-sdk/src/engine/tf/labels.js +75 -0
  32. package/dist-sdk/src/engine/tf/parse.d.ts +15 -0
  33. package/dist-sdk/src/engine/tf/parse.js +162 -0
  34. package/dist-sdk/src/entities/access-policy/build.d.ts +26 -0
  35. package/dist-sdk/src/entities/access-policy/build.js +7 -0
  36. package/dist-sdk/src/entities/access-policy/filter.d.ts +61 -0
  37. package/dist-sdk/src/entities/access-policy/filter.js +83 -0
  38. package/dist-sdk/src/entities/access-policy/from-tf.d.ts +66 -0
  39. package/dist-sdk/src/entities/access-policy/from-tf.js +187 -0
  40. package/dist-sdk/src/entities/access-policy/module.d.ts +3 -0
  41. package/dist-sdk/src/entities/access-policy/module.js +17 -0
  42. package/dist-sdk/src/entities/access-policy/to-tf.d.ts +43 -0
  43. package/dist-sdk/src/entities/access-policy/to-tf.js +164 -0
  44. package/dist-sdk/src/entities/access-policy/types.d.ts +4 -0
  45. package/dist-sdk/src/entities/access-policy/types.js +10 -0
  46. package/dist-sdk/src/entities/agent/build.d.ts +2 -0
  47. package/dist-sdk/src/entities/agent/build.js +1 -0
  48. package/dist-sdk/src/entities/agent/deserializer/print-agent-v2.d.ts +38 -3
  49. package/dist-sdk/src/entities/agent/deserializer/print-agent-v2.js +257 -32
  50. package/dist-sdk/src/entities/agent/from-tf.d.ts +29 -9
  51. package/dist-sdk/src/entities/agent/from-tf.js +146 -19
  52. package/dist-sdk/src/entities/agent/index.d.ts +1 -1
  53. package/dist-sdk/src/entities/agent/module.js +14 -4
  54. package/dist-sdk/src/entities/agent/to-tf.d.ts +46 -1
  55. package/dist-sdk/src/entities/agent/to-tf.js +160 -14
  56. package/dist-sdk/src/entities/agent/tools.d.ts +88 -9
  57. package/dist-sdk/src/entities/agent/tools.js +46 -7
  58. package/dist-sdk/src/entities/agent-a2a-skill/from-tf.d.ts +17 -0
  59. package/dist-sdk/src/entities/agent-a2a-skill/from-tf.js +64 -0
  60. package/dist-sdk/src/entities/agent-a2a-skill/module.d.ts +4 -0
  61. package/dist-sdk/src/entities/agent-a2a-skill/module.js +14 -0
  62. package/dist-sdk/src/entities/agent-a2a-skill/to-tf.d.ts +40 -0
  63. package/dist-sdk/src/entities/agent-a2a-skill/to-tf.js +47 -0
  64. package/dist-sdk/src/entities/agent-tool/from-tf.d.ts +51 -4
  65. package/dist-sdk/src/entities/agent-tool/from-tf.js +131 -35
  66. package/dist-sdk/src/entities/agent-tool/to-tf.d.ts +11 -4
  67. package/dist-sdk/src/entities/agent-tool/to-tf.js +176 -30
  68. package/dist-sdk/src/entities/agent-tool/types.d.ts +5 -4
  69. package/dist-sdk/src/entities/agent-tool/types.js +38 -6
  70. package/dist-sdk/src/entities/app/build.d.ts +91 -13
  71. package/dist-sdk/src/entities/app/build.js +63 -5
  72. package/dist-sdk/src/entities/app/deserializer/normalize.d.ts +3 -0
  73. package/dist-sdk/src/entities/app/from-tf.d.ts +1 -1
  74. package/dist-sdk/src/entities/app/from-tf.js +32 -22
  75. package/dist-sdk/src/entities/app/index.d.ts +4 -1
  76. package/dist-sdk/src/entities/app/index.js +2 -1
  77. package/dist-sdk/src/entities/app/module.js +10 -2
  78. package/dist-sdk/src/entities/app/to-tf.d.ts +5 -0
  79. package/dist-sdk/src/entities/app/to-tf.js +45 -4
  80. package/dist-sdk/src/entities/automation/build.d.ts +57 -13
  81. package/dist-sdk/src/entities/automation/build.js +62 -42
  82. package/dist-sdk/src/entities/automation/deserializer/ast.d.ts +25 -8
  83. package/dist-sdk/src/entities/automation/deserializer/print-automation.d.ts +16 -1
  84. package/dist-sdk/src/entities/automation/deserializer/print-automation.js +206 -47
  85. package/dist-sdk/src/entities/automation/filter.d.ts +37 -3
  86. package/dist-sdk/src/entities/automation/filter.js +54 -3
  87. package/dist-sdk/src/entities/automation/from-tf.d.ts +26 -27
  88. package/dist-sdk/src/entities/automation/from-tf.js +1344 -222
  89. package/dist-sdk/src/entities/automation/index.d.ts +4 -0
  90. package/dist-sdk/src/entities/automation/index.js +4 -0
  91. package/dist-sdk/src/entities/automation/module.js +29 -32
  92. package/dist-sdk/src/entities/automation/operators.d.ts +29 -2
  93. package/dist-sdk/src/entities/automation/operators.js +27 -1
  94. package/dist-sdk/src/entities/automation/output-key-wire-name.d.ts +19 -0
  95. package/dist-sdk/src/entities/automation/output-key-wire-name.js +24 -0
  96. package/dist-sdk/src/entities/automation/script-code.d.ts +10 -21
  97. package/dist-sdk/src/entities/automation/script-code.js +272 -5
  98. package/dist-sdk/src/entities/automation/tasks.d.ts +125 -19
  99. package/dist-sdk/src/entities/automation/tasks.js +83 -15
  100. package/dist-sdk/src/entities/automation/to-tf.d.ts +10 -29
  101. package/dist-sdk/src/entities/automation/to-tf.js +1291 -230
  102. package/dist-sdk/src/entities/automation/triggers.d.ts +9 -1
  103. package/dist-sdk/src/entities/automation/types.d.ts +29 -4
  104. package/dist-sdk/src/entities/automation/types.js +76 -46
  105. package/dist-sdk/src/entities/element/build.d.ts +44 -13
  106. package/dist-sdk/src/entities/element/build.js +45 -3
  107. package/dist-sdk/src/entities/element/from-tf.d.ts +1 -1
  108. package/dist-sdk/src/entities/element/from-tf.js +28 -14
  109. package/dist-sdk/src/entities/element/index.d.ts +4 -1
  110. package/dist-sdk/src/entities/element/index.js +2 -1
  111. package/dist-sdk/src/entities/element/module.js +4 -1
  112. package/dist-sdk/src/entities/element/to-tf.js +26 -3
  113. package/dist-sdk/src/entities/field/build.d.ts +61 -14
  114. package/dist-sdk/src/entities/field/build.js +10 -0
  115. package/dist-sdk/src/entities/field/calc/runtime.js +4 -0
  116. package/dist-sdk/src/entities/field/from-tf.d.ts +5 -3
  117. package/dist-sdk/src/entities/field/from-tf.js +118 -7
  118. package/dist-sdk/src/entities/field/module.js +3 -2
  119. package/dist-sdk/src/entities/field/to-tf.d.ts +46 -11
  120. package/dist-sdk/src/entities/field/to-tf.js +166 -17
  121. package/dist-sdk/src/entities/field/types.d.ts +26 -0
  122. package/dist-sdk/src/entities/field/types.js +34 -12
  123. package/dist-sdk/src/entities/file-reader/build.d.ts +108 -50
  124. package/dist-sdk/src/entities/file-reader/build.js +23 -23
  125. package/dist-sdk/src/entities/file-reader/deserializer/print-file-reader.d.ts +6 -0
  126. package/dist-sdk/src/entities/file-reader/deserializer/print-file-reader.js +109 -0
  127. package/dist-sdk/src/entities/file-reader/from-tf.d.ts +13 -0
  128. package/dist-sdk/src/entities/file-reader/from-tf.js +172 -0
  129. package/dist-sdk/src/entities/file-reader/index.d.ts +8 -3
  130. package/dist-sdk/src/entities/file-reader/index.js +7 -2
  131. package/dist-sdk/src/entities/file-reader/module.d.ts +3 -0
  132. package/dist-sdk/src/entities/file-reader/module.js +115 -0
  133. package/dist-sdk/src/entities/file-reader/to-tf.d.ts +14 -0
  134. package/dist-sdk/src/entities/file-reader/to-tf.js +84 -0
  135. package/dist-sdk/src/entities/file-reader/types.d.ts +32 -0
  136. package/dist-sdk/src/entities/file-reader/types.js +30 -0
  137. package/dist-sdk/src/entities/org/ai-provider.d.ts +13 -0
  138. package/dist-sdk/src/entities/org/ai-provider.js +46 -0
  139. package/dist-sdk/src/entities/org/connector.d.ts +8 -1
  140. package/dist-sdk/src/entities/org/connector.js +12 -14
  141. package/dist-sdk/src/entities/org/group.js +10 -1
  142. package/dist-sdk/src/entities/org/index.d.ts +1 -0
  143. package/dist-sdk/src/entities/org/index.js +1 -0
  144. package/dist-sdk/src/entities/org/user.js +10 -1
  145. package/dist-sdk/src/entities/shared/filters/ast.d.ts +59 -9
  146. package/dist-sdk/src/entities/shared/filters/types.d.ts +1 -1
  147. package/dist-sdk/src/entities/shared/tool-field-ref.d.ts +17 -0
  148. package/dist-sdk/src/entities/shared/tool-field-ref.js +46 -0
  149. package/dist-sdk/src/entities/shared/types/field-types.d.ts +10 -1
  150. package/dist-sdk/src/entities/shared/types/field-types.js +9 -0
  151. package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.d.ts +19 -0
  152. package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.js +128 -11
  153. package/dist-sdk/src/entities/skill/from-tf.js +4 -3
  154. package/dist-sdk/src/entities/skill/index.d.ts +1 -1
  155. package/dist-sdk/src/entities/skill/module.js +7 -1
  156. package/dist-sdk/src/entities/skill/to-tf.d.ts +26 -0
  157. package/dist-sdk/src/entities/skill/to-tf.js +65 -4
  158. package/dist-sdk/src/entities/skill/tools.d.ts +37 -8
  159. package/dist-sdk/src/entities/skill/tools.js +43 -1
  160. package/dist-sdk/src/entities/skill-tool/from-tf.d.ts +34 -4
  161. package/dist-sdk/src/entities/skill-tool/from-tf.js +128 -18
  162. package/dist-sdk/src/entities/skill-tool/to-tf.d.ts +13 -1
  163. package/dist-sdk/src/entities/skill-tool/to-tf.js +164 -25
  164. package/dist-sdk/src/entities/skill-tool/types.d.ts +5 -7
  165. package/dist-sdk/src/entities/skill-tool/types.js +47 -11
  166. package/dist-sdk/src/entities/view/from-tf.d.ts +6 -2
  167. package/dist-sdk/src/entities/view/from-tf.js +11 -9
  168. package/dist-sdk/src/entities/view/to-tf.d.ts +9 -7
  169. package/dist-sdk/src/entities/view/to-tf.js +39 -18
  170. package/dist-sdk/src/entities/view/types.d.ts +1 -1
  171. package/dist-sdk/src/entities/view/types.js +1 -1
  172. package/dist-sdk/src/graph/deferred-catalog-render.d.ts +0 -5
  173. package/dist-sdk/src/graph/deferred-catalog-render.js +1 -20
  174. package/dist-sdk/src/graph/index.d.ts +8 -0
  175. package/dist-sdk/src/graph/index.js +13 -9
  176. package/dist-sdk/src/graph/modules.js +8 -1
  177. package/dist-sdk/src/graph/types.d.ts +98 -4
  178. package/package.json +49 -16
  179. package/dist-sdk/src/entities/automation/value-ref-tf.d.ts +0 -76
  180. package/dist-sdk/src/entities/automation/value-ref-tf.js +0 -190
@@ -15,7 +15,7 @@
15
15
  * AppField is accepted.
16
16
  */
17
17
  import type { AppField, AppPicklistConfig, PicklistOption } from "../app/build.js";
18
- import type { FieldRef } from "../../catalog/index.js";
18
+ import type { AspectRef, FieldRef } from "../../catalog/index.js";
19
19
  import type { Expr, ExprType } from "./calc/types.js";
20
20
  /** Options every field kind accepts (everything on {@link AppField} that isn't kind-specific). */
21
21
  export interface BaseFieldOptions {
@@ -47,23 +47,70 @@ export interface PicklistFieldOptions extends BaseFieldOptions {
47
47
  /** Extra options for the numeric kinds (`numberField` / `decimalField`). */
48
48
  export interface NumericFieldOptions extends BaseFieldOptions {
49
49
  readonly numericFormat?: Record<string, unknown>;
50
+ /** `decimalField` only — display/storage precision. */
51
+ readonly decimalPlaces?: number;
50
52
  }
51
53
  /** Extra options for `textField`. */
52
54
  export interface TextFieldOptions extends BaseFieldOptions {
53
55
  readonly qrFormat?: Record<string, unknown>;
54
56
  }
55
- export declare function textField(opts: TextFieldOptions): AppField;
56
- export declare function htmlField(opts: BaseFieldOptions): AppField;
57
- export declare function numberField(opts: NumericFieldOptions): AppField;
58
- export declare function decimalField(opts: NumericFieldOptions): AppField;
59
- export declare function booleanField(opts: BaseFieldOptions): AppField;
60
- export declare function dateField(opts: BaseFieldOptions): AppField;
61
- export declare function dateTimeField(opts: BaseFieldOptions): AppField;
62
- export declare function userField(opts: BaseFieldOptions): AppField;
63
- export declare function groupField(opts: BaseFieldOptions): AppField;
64
- export declare function attachmentField(opts: BaseFieldOptions): AppField;
65
- export declare function jsonField(opts: BaseFieldOptions): AppField;
66
- export declare function picklistField(opts: PicklistFieldOptions): AppField;
67
- export declare function multiPicklistField(opts: PicklistFieldOptions): AppField;
57
+ /**
58
+ * Options for `relationField` — a dynamic dropdown sourced from another
59
+ * App/Element/Task's records (`elementum_relation_field`), distinct from a
60
+ * static `picklistField`. `relatedObject`/`relatedLabelField` (see
61
+ * {@link RelationAppField}) accept a catalog token or a raw platform id. A
62
+ * token naming a SIBLING app/element authored in the same build resolves
63
+ * to a direct resource reference and does not need to already exist on the
64
+ * platform; only a token naming something genuinely outside this
65
+ * workspace needs that.
66
+ */
67
+ export interface RelationFieldOptions extends BaseFieldOptions {
68
+ readonly relatedObject: AspectRef | string;
69
+ readonly relatedLabelField: FieldRef | string;
70
+ /** Defaults to `true` on the platform. */
71
+ readonly autoRelate?: boolean;
72
+ }
73
+ export declare function textField(opts: TextFieldOptions): AppField & {
74
+ readonly type: "TEXT";
75
+ };
76
+ export declare function htmlField(opts: BaseFieldOptions): AppField & {
77
+ readonly type: "HTML";
78
+ };
79
+ export declare function numberField(opts: NumericFieldOptions): AppField & {
80
+ readonly type: "NUMBER";
81
+ };
82
+ export declare function decimalField(opts: NumericFieldOptions): AppField & {
83
+ readonly type: "DECIMAL";
84
+ };
85
+ export declare function booleanField(opts: BaseFieldOptions): AppField & {
86
+ readonly type: "BOOLEAN";
87
+ };
88
+ export declare function dateField(opts: BaseFieldOptions): AppField & {
89
+ readonly type: "DATE";
90
+ };
91
+ export declare function dateTimeField(opts: BaseFieldOptions): AppField & {
92
+ readonly type: "DATETIME";
93
+ };
94
+ export declare function userField(opts: BaseFieldOptions): AppField & {
95
+ readonly type: "USER";
96
+ };
97
+ export declare function groupField(opts: BaseFieldOptions): AppField & {
98
+ readonly type: "GROUP";
99
+ };
100
+ export declare function attachmentField(opts: BaseFieldOptions): AppField & {
101
+ readonly type: "ATTACHMENT";
102
+ };
103
+ export declare function jsonField(opts: BaseFieldOptions): AppField & {
104
+ readonly type: "JSON";
105
+ };
106
+ export declare function picklistField(opts: PicklistFieldOptions): AppField & {
107
+ readonly type: "PICKLIST";
108
+ };
109
+ export declare function multiPicklistField(opts: PicklistFieldOptions): AppField & {
110
+ readonly type: "MULTI_PICKLIST";
111
+ };
112
+ export declare function relationField(opts: RelationFieldOptions): AppField & {
113
+ readonly type: "RELATION";
114
+ };
68
115
  /** Authoring field type → its factory's exported name (the printer's emission table). */
69
116
  export declare const FIELD_FACTORY_BY_TYPE: ReadonlyMap<string, string>;
@@ -14,6 +14,12 @@
14
14
  * literal form (`{ name, type: "TEXT" }`) remains valid where ever an
15
15
  * AppField is accepted.
16
16
  */
17
+ // Generic over the literal type discriminator so a field factory's return
18
+ // type carries e.g. `type: "PICKLIST"`, not the widened `WorkflowFieldType`
19
+ // union — required for the live catalog path (`defineApp`'s `.ref()`) to
20
+ // preserve per-field literal typing all the way to `RecordShape`
21
+ // (`entities/field/record-shape.ts`), the same way the old baked
22
+ // `fieldRef({ fieldType: "PICKLIST" })` literal did.
17
23
  function field(type, opts) {
18
24
  return { ...opts, type };
19
25
  }
@@ -56,6 +62,9 @@ export function picklistField(opts) {
56
62
  export function multiPicklistField(opts) {
57
63
  return field("MULTI_PICKLIST", opts);
58
64
  }
65
+ export function relationField(opts) {
66
+ return field("RELATION", opts);
67
+ }
59
68
  /** Authoring field type → its factory's exported name (the printer's emission table). */
60
69
  export const FIELD_FACTORY_BY_TYPE = new Map([
61
70
  ["TEXT", "textField"],
@@ -71,4 +80,5 @@ export const FIELD_FACTORY_BY_TYPE = new Map([
71
80
  ["JSON", "jsonField"],
72
81
  ["PICKLIST", "picklistField"],
73
82
  ["MULTI_PICKLIST", "multiPicklistField"],
83
+ ["RELATION", "relationField"],
74
84
  ]);
@@ -107,6 +107,10 @@ function appFieldExprType(field) {
107
107
  `calculation — referring to a calculated field's value needs the server ` +
108
108
  `to infer its output type, which the EDK doesn't replicate locally. ` +
109
109
  `Inline the upstream expression or split the work across automation tasks.`);
110
+ case "RELATION":
111
+ throw new Error(`calc: cannot reference relation field "${field.name}" in a calculation — ` +
112
+ `it has no scalar/comparable value of its own (relatedObject/relatedLabelField ` +
113
+ `are a catalog token or raw platform id, not an expression-typed value).`);
110
114
  default: {
111
115
  const _exhaustive = field;
112
116
  void _exhaustive;
@@ -6,13 +6,15 @@
6
6
  * import blocks and stay data-side.
7
7
  */
8
8
  import { type FromTfContext } from "../../engine/pull/context.js";
9
- import { type TfBlock, type TfValue } from "../../engine/tf/index.js";
9
+ import { type ChildResourceScope, type TfBlock, type TfValue } from "../../engine/tf/index.js";
10
10
  import type { NormalizedField } from "../app/deserializer/normalize.js";
11
11
  export interface FieldOwner {
12
12
  /** The owner's resource type ("elementum_app") — what `object_id` must reference. */
13
13
  readonly type: string;
14
- /** The owner's tfLabel. */
14
+ /** The owner's tfLabel (namespace). */
15
15
  readonly label: string;
16
+ /** Scope for stripping child label prefixes (defaults to app). */
17
+ readonly scope?: ChildResourceScope;
16
18
  }
17
19
  /**
18
20
  * One field block → its normalized entry; undefined = skipped with a
@@ -26,4 +28,4 @@ export declare function fieldFromTf(block: TfBlock, owner: FieldOwner, ctx: From
26
28
  * fields, view field_ids/columns). Undefined for any other shape or an
27
29
  * unknown resource type.
28
30
  */
29
- export declare function fieldKeyFromTfRef(v: TfValue): string | undefined;
31
+ export declare function fieldKeyFromTfRef(v: TfValue, ownerTfLabel?: string, scope?: ChildResourceScope): string | undefined;
@@ -6,10 +6,27 @@
6
6
  * import blocks and stay data-side.
7
7
  */
8
8
  import { EntityTfError } from "../../engine/entity.js";
9
- import { optionalString, resourceRefLabel } from "../../engine/pull/context.js";
10
- import { isTfExpr, tfLabelToTsLabel } from "../../engine/tf/index.js";
9
+ import { dataRefLabel, optionalString, resourceRefLabel } from "../../engine/pull/context.js";
10
+ import { bareChildResourceLabel, isTfExpr, scopedLabelToBagKey, tfLabelToTsLabel } from "../../engine/tf/index.js";
11
11
  import { optionsFromTf } from "./options.js";
12
12
  import { KNOWN_FIELD_ATTRS, SUB_TYPE_BY_RESOURCE } from "./types.js";
13
+ /**
14
+ * A related_object_id/related_label_field_id value → its raw platform id, if
15
+ * recoverable: a literal string as-is, or a `<type>.<label>.id` resource
16
+ * reference resolved through this stream's own import blocks (the same
17
+ * lookup adoption uses - `ctx.importId` is keyed by every resource address
18
+ * the stream imports, not just this field's). `data-attr` foreign lookups
19
+ * and `local` computed attributes carry no import block for their target
20
+ * and stay unresolvable.
21
+ */
22
+ function resolveRelatedId(v, ctx) {
23
+ if (typeof v === "string")
24
+ return v;
25
+ if (v !== undefined && isTfExpr(v) && v.$expr === "resource-attr" && v.path.length === 1 && v.path[0] === "id") {
26
+ return ctx.importId(`${v.type}.${v.label}`);
27
+ }
28
+ return undefined;
29
+ }
13
30
  /**
14
31
  * One field block → its normalized entry; undefined = skipped with a
15
32
  * diagnostic (unknown resource type, or a field belonging to a different
@@ -25,6 +42,18 @@ export function fieldFromTf(block, owner, ctx) {
25
42
  if (fieldLabel === undefined || fieldLabel === "") {
26
43
  throw new EntityTfError(`${block.type} block is missing its label`);
27
44
  }
45
+ if (subType.implemented !== true) {
46
+ // Declared but not yet round-tripped - see FIELD_SUB_TYPES for the
47
+ // current list of not-yet-implemented sub-types, if any.
48
+ // Reported to `skipped`, not just a diagnostic: this address still has a
49
+ // real import block in the stream and would otherwise get swept into
50
+ // adoption's targeted plan as a side effect of something else's
51
+ // reference to it (e.g. a layout's field_ids), even though nothing in
52
+ // authored source can ever reproduce it.
53
+ ctx.skipped.push(`${block.type}.${fieldLabel}`);
54
+ ctx.diagnostics.push(`${block.type}.${fieldLabel}: ${subType.typeKey} fields are not yet supported; skipped`);
55
+ return undefined;
56
+ }
28
57
  const attrs = block.attrs;
29
58
  const fieldName = optionalString(attrs, "name", `field "${fieldLabel}"`, ctx) ?? "";
30
59
  const where = `field "${fieldName}" (${block.type}.${fieldLabel})`;
@@ -39,8 +68,80 @@ export function fieldFromTf(block, owner, ctx) {
39
68
  return undefined;
40
69
  }
41
70
  const options = subType.hasOptions === true ? optionsFromTf(attrs["options"], `${where} options`, ctx) : undefined;
71
+ let decimalPlaces;
72
+ if (subType.typeKey === "decimal" && attrs["decimal_places"] !== undefined) {
73
+ if (typeof attrs["decimal_places"] === "number")
74
+ decimalPlaces = attrs["decimal_places"];
75
+ else
76
+ ctx.diagnostics.push(`${where}: decimal_places is not a number; dropped`);
77
+ }
78
+ // related_object_id/related_label_field_id are raw platform ids on the
79
+ // wire when ei's beautifier finds nothing to resolve them against - a
80
+ // non-recursive export never discovers the related aspect's own identity,
81
+ // so this is the common case (confirmed live) and they stay literal
82
+ // strings. But whenever the beautifier CAN resolve one, it prefers a
83
+ // readable terraform reference over the raw id: a self-relation prints
84
+ // `related_object_id = elementum_app.<self>.id`, a relation into a
85
+ // resource this same stream also imports (e.g. a field on a recursively
86
+ // discovered dependency) prints `<type>.<label>.id` the same way. Both are
87
+ // `resource-attr` expressions whose target carries its own import block in
88
+ // this stream, so the raw id IS recoverable - via the same `ctx.importId`
89
+ // lookup adoption itself uses, keyed by the referenced resource's address,
90
+ // not this field's own. What's NOT recoverable: a `data-attr` foreign
91
+ // lookup (`data.elementum_element.<label>.id` - data blocks are never
92
+ // imported) or a `local` computed attribute (`local.<app>_id_field_id` -
93
+ // an app/element's system field ids are read-only outputs of that
94
+ // resource, never their own resource address). Both properties are
95
+ // required by RelationFieldOptions, so printing relationField(...) with
96
+ // either missing would be invalid TypeScript (item 117) - skip the whole
97
+ // field when either side can't be resolved, same convention as an
98
+ // unimplemented sub-type.
99
+ let relatedObject;
100
+ let relatedLabelField;
101
+ let autoRelate;
102
+ if (subType.hasRelation === true) {
103
+ relatedObject = resolveRelatedId(attrs["related_object_id"], ctx);
104
+ relatedLabelField = resolveRelatedId(attrs["related_label_field_id"], ctx);
105
+ if (typeof attrs["auto_relate"] === "boolean")
106
+ autoRelate = attrs["auto_relate"];
107
+ if (relatedObject === undefined || relatedLabelField === undefined) {
108
+ const missing = [
109
+ relatedObject === undefined && "related_object_id",
110
+ relatedLabelField === undefined && "related_label_field_id",
111
+ ].filter((v) => v !== false);
112
+ ctx.diagnostics.push(`${where}: ${missing.join(" and ")} does not resolve to a raw platform id - a RELATION field needs both to reconstruct relatedObject/relatedLabelField; field skipped`);
113
+ ctx.skipped.push(`${block.type}.${fieldLabel}`);
114
+ return undefined;
115
+ }
116
+ }
117
+ const fieldRole = attrs["field_role"];
118
+ const tags = Array.isArray(fieldRole) && fieldRole.every((t) => typeof t === "string")
119
+ ? fieldRole
120
+ : undefined;
121
+ // A group/user field's default_value is a `data.elementum_group|user.<label>.id`
122
+ // EXPRESSION on the wire (the platform has no stable raw id to hand
123
+ // authored source, and the field is Optional-without-Computed — omitting
124
+ // it entirely would plan a destroy of the live default on every build).
125
+ // Round-trip the data block's own captured label + lookup value verbatim
126
+ // (see OrgFieldRefValue/fieldToTf's `rawLookup`) rather than resolving
127
+ // through the org.ts catalog: this is a pulled reference, not a
128
+ // hand-authored one, so there's no "does it still exist in the org"
129
+ // check to make.
130
+ let orgFieldRefDefault;
131
+ const orgRefDataType = subType.typeKey === "group" ? "elementum_group" : subType.typeKey === "user" ? "elementum_user" : undefined;
132
+ if (orgRefDataType !== undefined && isTfExpr(attrs["default_value"])) {
133
+ const label = dataRefLabel(attrs["default_value"], orgRefDataType);
134
+ const value = label !== undefined ? ctx.dataName(orgRefDataType, label) : undefined;
135
+ if (label !== undefined && value !== undefined) {
136
+ orgFieldRefDefault = { __kind: "orgFieldRef", dataType: orgRefDataType, label, value };
137
+ }
138
+ else {
139
+ ctx.diagnostics.push(`${where}: default_value references an unresolvable ${orgRefDataType} data lookup; dropped`);
140
+ }
141
+ }
142
+ const scope = owner.scope ?? "app";
42
143
  return {
43
- key: tfLabelToTsLabel(fieldLabel),
144
+ key: scopedLabelToBagKey(owner.label, fieldLabel, scope),
44
145
  field: {
45
146
  id: ctx.importId(`${block.type}.${fieldLabel}`) ?? "",
46
147
  name: fieldName,
@@ -54,9 +155,16 @@ export function fieldFromTf(block, owner, ctx) {
54
155
  ...(attrs["lock_on_create"] === true && { lockOnCreate: true }),
55
156
  ...(typeof attrs["description"] === "string" && { description: attrs["description"] }),
56
157
  ...(typeof attrs["column_name"] === "string" && { columnName: attrs["column_name"] }),
57
- ...(attrs["default_value"] !== undefined &&
58
- !isTfExpr(attrs["default_value"]) && { defaultValue: attrs["default_value"] }),
158
+ ...(orgFieldRefDefault !== undefined
159
+ ? { defaultValue: orgFieldRefDefault }
160
+ : attrs["default_value"] !== undefined &&
161
+ !isTfExpr(attrs["default_value"]) && { defaultValue: attrs["default_value"] }),
59
162
  ...(options !== undefined && { options }),
163
+ ...(tags !== undefined && tags.length > 0 && { tags }),
164
+ ...(relatedObject !== undefined && { relatedObject }),
165
+ ...(relatedLabelField !== undefined && { relatedLabelField }),
166
+ ...(autoRelate !== undefined && { autoRelate }),
167
+ ...(decimalPlaces !== undefined && { decimalPlaces }),
60
168
  },
61
169
  };
62
170
  }
@@ -66,8 +174,11 @@ export function fieldFromTf(block, owner, ctx) {
66
174
  * fields, view field_ids/columns). Undefined for any other shape or an
67
175
  * unknown resource type.
68
176
  */
69
- export function fieldKeyFromTfRef(v) {
177
+ export function fieldKeyFromTfRef(v, ownerTfLabel, scope = "app") {
70
178
  if (!isTfExpr(v) || v.$expr !== "resource-attr" || v.path.length !== 1 || v.path[0] !== "id")
71
179
  return undefined;
72
- return SUB_TYPE_BY_RESOURCE.has(v.type) ? tfLabelToTsLabel(v.label) : undefined;
180
+ if (!SUB_TYPE_BY_RESOURCE.has(v.type))
181
+ return undefined;
182
+ const bare = ownerTfLabel !== undefined ? bareChildResourceLabel(ownerTfLabel, v.label, scope) : v.label;
183
+ return ownerTfLabel !== undefined ? scopedLabelToBagKey(ownerTfLabel, v.label, scope) : tfLabelToTsLabel(bare);
73
184
  }
@@ -12,11 +12,10 @@ export const fieldModule = {
12
12
  ],
13
13
  resource: {
14
14
  shape: "subTypes",
15
- // All field rows round-trip today — no implemented? flag on FieldSubType.
16
15
  rows: FIELD_SUB_TYPES.map((t) => ({
17
16
  key: t.typeKey,
18
17
  resourceType: t.resourceType,
19
- implemented: true,
18
+ implemented: t.implemented === true,
20
19
  })),
21
20
  },
22
21
  data: NO_DATA,
@@ -35,6 +34,8 @@ export const fieldModule = {
35
34
  const parts = [`fieldType: ${JSON.stringify(field.fieldType)}`];
36
35
  if (field.tags !== undefined)
37
36
  parts.push(`tags: ${JSON.stringify(field.tags)}`);
37
+ if (field.options !== undefined)
38
+ parts.push(`options: ${JSON.stringify(field.options)}`);
38
39
  return parts;
39
40
  }),
40
41
  stubRef: (refName, parent) => ({
@@ -9,8 +9,23 @@
9
9
  * never silent, never thrown (plan §4.3). The one hard error is a
10
10
  * non-canonical bag key: it has no well-defined terraform address.
11
11
  */
12
- import { type TfBlock, type TfExpr, type TfValue } from "../../engine/tf/index.js";
12
+ import { type OrgRefLookup, type WorkspaceAspect } from "../../engine/build/context.js";
13
+ import { type ChildResourceScope, type TfBlock, type TfExpr, type TfValue } from "../../engine/tf/index.js";
13
14
  import { type PicklistValueWire } from "./options.js";
15
+ /** Slim `AspectRef` token shape as it appears in a def() payload (post-`slimTokens`). */
16
+ interface AspectTokenWire {
17
+ readonly __kind?: string;
18
+ readonly refName?: string;
19
+ readonly name?: string;
20
+ }
21
+ /** Slim `FieldRef` token shape as it appears in a def() payload. */
22
+ interface FieldTokenWire {
23
+ readonly __kind?: string;
24
+ readonly refName?: string;
25
+ readonly name?: string;
26
+ readonly parent?: string;
27
+ readonly fieldType?: string;
28
+ }
14
29
  /** The authored field shape (post-`slimTokens` compose body). Deliberately loose — degrade, don't assert. */
15
30
  export interface FieldInnerWire {
16
31
  readonly name?: string;
@@ -27,12 +42,40 @@ export interface FieldInnerWire {
27
42
  readonly config?: unknown;
28
43
  readonly format?: unknown;
29
44
  readonly calculation?: unknown;
45
+ /**
46
+ * "relation" fields only (see RelationAppField) — a catalog token when the
47
+ * target object/field has already been pulled into this workspace, or a
48
+ * raw platform id otherwise. A pulled RELATION field always has raw
49
+ * strings here; tokens only ever come from freshly authored source.
50
+ */
51
+ readonly relatedObject?: AspectTokenWire | string;
52
+ readonly relatedLabelField?: FieldTokenWire | string;
53
+ readonly autoRelate?: boolean;
54
+ /** "decimal" fields only. */
55
+ readonly decimalPlaces?: number;
30
56
  }
31
57
  export interface FieldToTfOptions {
58
+ /** The owning stream root's terraform label (namespace). */
59
+ readonly ownerTfLabel: string;
60
+ /** The owning stream root's refName — needed to detect a self-targeting RELATION field. */
61
+ readonly appRefName: string;
62
+ /** App children use `apps_<ns>_`; element children use `<ns>_`. */
63
+ readonly scope?: ChildResourceScope;
32
64
  /** The owner's id reference, e.g. `elementum_app.<ns>.id` — computed by the owner's to-tf. */
33
65
  readonly objectId: TfValue;
34
66
  /** Degradations land here — reported by the caller, never silent. */
35
67
  readonly diagnostics: string[];
68
+ /** Needed to re-emit a group/user field's `default_value` as a `data.elementum_group|user` lookup (see `OrgFieldRefValue`). */
69
+ readonly orgRefs?: OrgRefLookup;
70
+ /**
71
+ * Every app/element stream root in THIS build, keyed by
72
+ * `workspaceAspectKey` (kind+refName — an app and an element MAY share a
73
+ * refName) — lets a RELATION field's `relatedObject` tell a SIBLING being
74
+ * authored/created in this same build (a direct resource reference,
75
+ * sequenced automatically) apart from a genuinely foreign object (a
76
+ * `data` lookup, which can only ever find something already live).
77
+ */
78
+ readonly workspaceAspects?: ReadonlyMap<string, WorkspaceAspect>;
36
79
  }
37
80
  export interface FieldToTfResult {
38
81
  readonly block: TfBlock;
@@ -43,13 +86,5 @@ export interface FieldToTfResult {
43
86
  }
44
87
  /** One custom field → its resource block; undefined = degraded (diagnostic already pushed). */
45
88
  export declare function fieldToTf(typeKey: string, inner: FieldInnerWire, opts: FieldToTfOptions): FieldToTfResult | undefined;
46
- /**
47
- * A custom field reference `{fieldType, refName}` (the shape a `FieldRef`
48
- * token slims to) → `<resourceType>.<tfLabel>.id` — shared by anything that
49
- * NAMES a field by typed ref rather than owning it directly (agent-tool
50
- * fields, view layout/list-view field refs). Undefined when the authoring
51
- * type has no terraform mapping or the bag key isn't canonical; callers own
52
- * the diagnostic wording since the two failure cases usually read
53
- * differently at each call site.
54
- */
55
- export declare function fieldRefToTfValue(fieldType: string, refName: string): TfExpr | undefined;
89
+ export declare function fieldRefToTfValue(fieldType: string, refName: string, ownerTfLabel: string, scope?: ChildResourceScope): TfExpr | undefined;
90
+ export {};
@@ -10,12 +10,112 @@
10
10
  * non-canonical bag key: it has no well-defined terraform address.
11
11
  */
12
12
  import { EntityTfError } from "../../engine/entity.js";
13
- import { expr, isCanonicalTsLabel, snakeSlug, tsLabelToTfLabel } from "../../engine/tf/index.js";
13
+ import { workspaceAspectKey } from "../../engine/build/context.js";
14
+ import { expr, isCanonicalTsLabel, sanitizeName, scopedResourceLabel, snakeSlug, tsLabelToTfLabel } from "../../engine/tf/index.js";
15
+ import { SYSTEM_FIELD_KEY_SET } from "../../core/system-fields.js";
14
16
  import { picklistLabel } from "./options.js";
15
17
  import { SUB_TYPE_BY_AUTHORING_TYPE, SUB_TYPE_BY_TYPE_KEY } from "./types.js";
18
+ /** `unknown` → the tagged org-field-ref shape, or undefined for anything else. */
19
+ function asOrgFieldRef(v) {
20
+ if (typeof v !== "object" || v === null || v.__kind !== "orgFieldRef")
21
+ return undefined;
22
+ return v;
23
+ }
24
+ /**
25
+ * `relatedObject` (RELATION fields only) → its `related_object_id` value. A
26
+ * raw string passes through literally (unchanged, backward-compatible
27
+ * behavior). An `AspectRef` token resolves, in order:
28
+ * 1. self (names the owner itself) — reuses the already-resolved `objectId`;
29
+ * 2. a SIBLING stream root in this same build (`opts.workspaceAspects`,
30
+ * keyed by kind+refName since an app and an element MAY share a
31
+ * refName — see `workspaceAspectKey`) — a direct resource reference,
32
+ * so Terraform sequences its creation automatically even if it
33
+ * doesn't exist on the platform yet;
34
+ * 3. otherwise genuinely foreign — a `data.elementum_app`/
35
+ * `data.elementum_element` lookup, registered via `opts.orgRefs`
36
+ * (mirrors agent-tool/to-tf.ts's `targetIdValue`, board #42); this is
37
+ * the ONE case that requires the target to already exist live.
38
+ */
39
+ function resolveRelatedObject(token, opts, where) {
40
+ if (typeof token === "string")
41
+ return { value: token };
42
+ const refName = token.refName ?? "";
43
+ const kind = token.__kind === "element" ? "element" : "app";
44
+ if (kind === (opts.scope ?? "app") && refName === opts.appRefName) {
45
+ return { value: opts.objectId, refName, kind, target: { kind: opts.scope ?? "app", tfLabel: opts.ownerTfLabel } };
46
+ }
47
+ const sibling = opts.workspaceAspects?.get(workspaceAspectKey(kind, refName));
48
+ if (sibling !== undefined) {
49
+ const resourceType = kind === "element" ? "elementum_element" : "elementum_app";
50
+ return { value: expr.resourceAttr(resourceType, sibling.tfLabel, ["id"]), refName, kind, target: sibling };
51
+ }
52
+ const name = token.name ?? refName;
53
+ if (name === "") {
54
+ opts.diagnostics.push(`${where}: relatedObject token has no name/refName; dropped`);
55
+ return undefined;
56
+ }
57
+ if (opts.orgRefs === undefined) {
58
+ opts.diagnostics.push(`${where}: relatedObject references a foreign object but no org context is available; dropped`);
59
+ return undefined;
60
+ }
61
+ const tfLabel = sanitizeName(name);
62
+ if (kind === "element") {
63
+ opts.orgRefs.foreignElement(tfLabel, name);
64
+ return { value: expr.dataAttr("elementum_element", tfLabel, ["id"]), refName, kind };
65
+ }
66
+ opts.orgRefs.foreignApp(tfLabel, name);
67
+ return { value: expr.dataAttr("elementum_app", tfLabel, ["id"]), refName, kind };
68
+ }
69
+ /**
70
+ * `relatedLabelField` (RELATION fields only) → its `related_label_field_id`
71
+ * value. A raw string passes through literally. A `FieldRef` token resolves
72
+ * via the shared field resolver (`fieldRefToTfValue`) when the (already-
73
+ * resolved) `relatedObject` target is known — the owner itself or a
74
+ * SIBLING stream root in this build, both real resources — a direct
75
+ * resource reference either way; otherwise it's a genuinely foreign field,
76
+ * resolved via a `data.elementum_field` lookup keyed by `object_id` (the
77
+ * already-resolved `related_object_id` value, reused verbatim) + `name` —
78
+ * the one case agent-tool/to-tf.ts's `fieldRefValue` explicitly declines
79
+ * ("cross-app field refs are not supported... yet"). The lookup LABEL
80
+ * includes the resolved object's kind (when known) — `elementum_field` is
81
+ * one shared data-source type for app- and element-owned fields alike (see
82
+ * the provider's `field_data_source.go`), so an app and an element sharing
83
+ * a refName would otherwise both register the same label and silently
84
+ * overwrite each other's `object_id` (`loadOrgRefs.record` dedupes by
85
+ * `type.tfLabel`).
86
+ */
87
+ function resolveRelatedLabelField(token, object, opts, where) {
88
+ if (typeof token === "string")
89
+ return token;
90
+ const refName = token.refName ?? "";
91
+ if (refName === "") {
92
+ opts.diagnostics.push(`${where}: relatedLabelField token has no refName; dropped`);
93
+ return undefined;
94
+ }
95
+ if (object.refName !== undefined && token.parent !== undefined && token.parent !== object.refName) {
96
+ opts.diagnostics.push(`${where}: relatedLabelField token "${refName}" belongs to "${token.parent}", not relatedObject's target "${object.refName}"; dropped`);
97
+ return undefined;
98
+ }
99
+ if (object.target !== undefined) {
100
+ const value = fieldRefToTfValue(token.fieldType ?? "", refName, object.target.tfLabel, object.target.kind);
101
+ if (value === undefined) {
102
+ opts.diagnostics.push(`${where}: relatedLabelField token "${refName}" has no terraform field resource mapping (fieldType ${JSON.stringify(token.fieldType ?? "")}); dropped`);
103
+ }
104
+ return value;
105
+ }
106
+ if (opts.orgRefs === undefined) {
107
+ opts.diagnostics.push(`${where}: relatedLabelField references a foreign field but no org context is available; dropped`);
108
+ return undefined;
109
+ }
110
+ const name = token.name ?? refName;
111
+ const parentLabel = token.parent !== undefined ? tsLabelToTfLabel(token.parent) : "related";
112
+ const tfLabel = object.kind !== undefined ? `${object.kind}_${parentLabel}_${tsLabelToTfLabel(refName)}` : `${parentLabel}_${tsLabelToTfLabel(refName)}`;
113
+ opts.orgRefs.foreignField("elementum_field", tfLabel, object.value, name);
114
+ return expr.dataAttr("elementum_field", tfLabel, ["id"]);
115
+ }
16
116
  /** One custom field → its resource block; undefined = degraded (diagnostic already pushed). */
17
117
  export function fieldToTf(typeKey, inner, opts) {
18
- const { objectId, diagnostics } = opts;
118
+ const { ownerTfLabel, scope = "app", objectId, diagnostics } = opts;
19
119
  const name = inner.name ?? "";
20
120
  if (typeKey === "calculated" || inner.calculation !== undefined) {
21
121
  diagnostics.push(`field "${name}": calculated fields are not supported on the terraform path yet; dropped`);
@@ -40,7 +140,7 @@ export function fieldToTf(typeKey, inner, opts) {
40
140
  if (!isCanonicalTsLabel(bagKey)) {
41
141
  throw new EntityTfError(`field bag key "${bagKey}" is not a canonical tsLabel (see src/engine/tf/labels.ts) — rename the key in fields: {}`);
42
142
  }
43
- const tfLabel = tsLabelToTfLabel(bagKey);
143
+ const tfLabel = scopedResourceLabel(ownerTfLabel, tsLabelToTfLabel(bagKey), scope);
44
144
  const attrs = {
45
145
  object_id: objectId,
46
146
  name,
@@ -58,14 +158,50 @@ export function fieldToTf(typeKey, inner, opts) {
58
158
  if (inner.columnName !== undefined)
59
159
  attrs["column_name"] = inner.columnName;
60
160
  if (inner.defaultValue !== undefined) {
61
- const rendered = scalarOrUndefined(inner.defaultValue);
62
- if (rendered === undefined) {
63
- diagnostics.push(`field "${name}": default value has an unsupported (non-scalar) shape; dropped`);
161
+ const orgRef = asOrgFieldRef(inner.defaultValue);
162
+ if (orgRef !== undefined) {
163
+ if (opts.orgRefs === undefined) {
164
+ diagnostics.push(`field "${name}": default value references a ${orgRef.dataType} lookup but no org context is available; dropped`);
165
+ }
166
+ else {
167
+ opts.orgRefs.rawLookup(orgRef.dataType, orgRef.label, orgRef.value);
168
+ attrs["default_value"] = expr.dataAttr(orgRef.dataType, orgRef.label, ["id"]);
169
+ }
64
170
  }
65
171
  else {
66
- attrs["default_value"] = rendered;
172
+ const rendered = scalarOrUndefined(inner.defaultValue);
173
+ if (rendered === undefined) {
174
+ diagnostics.push(`field "${name}": default value has an unsupported (non-scalar) shape; dropped`);
175
+ }
176
+ else {
177
+ attrs["default_value"] = rendered;
178
+ }
67
179
  }
68
180
  }
181
+ if (inner.tags !== undefined && inner.tags.length > 0) {
182
+ attrs["field_role"] = [...inner.tags];
183
+ }
184
+ if (typeKey === "decimal" && inner.decimalPlaces !== undefined) {
185
+ attrs["decimal_places"] = inner.decimalPlaces;
186
+ }
187
+ if (subType.hasRelation === true) {
188
+ if (inner.relatedObject === undefined || inner.relatedLabelField === undefined) {
189
+ diagnostics.push(`field "${name}": relation fields require relatedObject and relatedLabelField; dropped`);
190
+ return undefined;
191
+ }
192
+ const where = `field "${name}"`;
193
+ const resolvedObject = resolveRelatedObject(inner.relatedObject, opts, where);
194
+ if (resolvedObject === undefined)
195
+ return undefined;
196
+ const relatedLabelFieldId = resolveRelatedLabelField(inner.relatedLabelField, resolvedObject, opts, where);
197
+ if (relatedLabelFieldId === undefined)
198
+ return undefined;
199
+ const relatedObjectId = resolvedObject.value;
200
+ attrs["related_object_id"] = relatedObjectId;
201
+ attrs["related_label_field_id"] = relatedLabelFieldId;
202
+ if (inner.autoRelate !== undefined)
203
+ attrs["auto_relate"] = inner.autoRelate;
204
+ }
69
205
  if (subType.hasOptions === true && inner.picklistValues !== undefined) {
70
206
  if (inner.picklistValues.some((pv) => pv.relatedParentValues !== undefined)) {
71
207
  diagnostics.push(`field "${name}": dependent option links (parentValues) are not supported by the terraform provider yet; dropped`);
@@ -81,17 +217,30 @@ function scalarOrUndefined(v) {
81
217
  return typeof v === "string" || typeof v === "number" || typeof v === "boolean" ? v : undefined;
82
218
  }
83
219
  /**
84
- * A custom field reference `{fieldType, refName}` (the shape a `FieldRef`
85
- * token slims to) → `<resourceType>.<tfLabel>.id` — shared by anything that
86
- * NAMES a field by typed ref rather than owning it directly (agent-tool
87
- * fields, view layout/list-view field refs). Undefined when the authoring
88
- * type has no terraform mapping or the bag key isn't canonical; callers own
89
- * the diagnostic wording since the two failure cases usually read
90
- * differently at each call site.
220
+ * A field reference `{fieldType, refName}` (the shape a `FieldRef` token
221
+ * slims to) → its terraform expression — shared by anything that NAMES a
222
+ * field by typed ref rather than owning it directly (skill-tool fields,
223
+ * view layout/list-view field refs). A CUSTOM field resolves to
224
+ * `<resourceType>.<tfLabel>.id`; a SYSTEM field (title/status/closedBy/…)
225
+ * has no `elementum_<type>_field` resource of its own, only a computed
226
+ * `local.<app>_<key>_field_id` alias — checked first, since a system
227
+ * field's fieldType (e.g. "USER" for closedBy) can coincidentally match a
228
+ * real custom-field resource type. Undefined when the authoring type has no
229
+ * terraform mapping or the bag key isn't canonical; callers own the
230
+ * diagnostic wording since the two failure cases usually read differently
231
+ * at each call site.
91
232
  */
92
- export function fieldRefToTfValue(fieldType, refName) {
233
+ /** Elements only have `id`/`title` as real system fields (no status/stage/audit fields) — see `element/locals.ts`. */
234
+ const ELEMENT_SYSTEM_FIELD_KEYS = new Set(["id", "title"]);
235
+ export function fieldRefToTfValue(fieldType, refName, ownerTfLabel, scope = "app") {
236
+ if (!isCanonicalTsLabel(refName))
237
+ return undefined;
238
+ const isSystemField = scope === "element" ? ELEMENT_SYSTEM_FIELD_KEYS.has(refName) : SYSTEM_FIELD_KEY_SET.has(refName);
239
+ if (isSystemField) {
240
+ return expr.local(`${ownerTfLabel}_${tsLabelToTfLabel(refName)}_field_id`);
241
+ }
93
242
  const subType = SUB_TYPE_BY_AUTHORING_TYPE.get(fieldType);
94
- if (subType === undefined || !isCanonicalTsLabel(refName))
243
+ if (subType === undefined)
95
244
  return undefined;
96
- return expr.resourceAttr(subType.resourceType, tsLabelToTfLabel(refName), ["id"]);
245
+ return expr.resourceAttr(subType.resourceType, scopedResourceLabel(ownerTfLabel, tsLabelToTfLabel(refName), scope), ["id"]);
97
246
  }