@elementumai/edk 0.8.0-preview.11 → 0.8.1

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 (165) hide show
  1. package/README.md +378 -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 +65 -2
  6. package/dist-sdk/src/catalog/from-authoring.js +6 -1
  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 +83 -6
  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 +14 -0
  14. package/dist-sdk/src/catalog/types.d.ts +60 -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 +20 -1
  21. package/dist-sdk/src/engine/build/context.d.ts +18 -0
  22. package/dist-sdk/src/engine/build/source-context.d.ts +45 -0
  23. package/dist-sdk/src/engine/build/source-context.js +23 -0
  24. package/dist-sdk/src/engine/pull/context.d.ts +15 -0
  25. package/dist-sdk/src/engine/pull/org.d.ts +10 -0
  26. package/dist-sdk/src/engine/pull/org.js +34 -4
  27. package/dist-sdk/src/engine/tf/index.d.ts +1 -1
  28. package/dist-sdk/src/engine/tf/index.js +1 -1
  29. package/dist-sdk/src/engine/tf/labels.d.ts +45 -0
  30. package/dist-sdk/src/engine/tf/labels.js +71 -0
  31. package/dist-sdk/src/entities/access-policy/build.d.ts +26 -0
  32. package/dist-sdk/src/entities/access-policy/build.js +7 -0
  33. package/dist-sdk/src/entities/access-policy/filter.d.ts +61 -0
  34. package/dist-sdk/src/entities/access-policy/filter.js +83 -0
  35. package/dist-sdk/src/entities/access-policy/from-tf.d.ts +48 -0
  36. package/dist-sdk/src/entities/access-policy/from-tf.js +189 -0
  37. package/dist-sdk/src/entities/access-policy/module.d.ts +3 -0
  38. package/dist-sdk/src/entities/access-policy/module.js +17 -0
  39. package/dist-sdk/src/entities/access-policy/to-tf.d.ts +36 -0
  40. package/dist-sdk/src/entities/access-policy/to-tf.js +149 -0
  41. package/dist-sdk/src/entities/access-policy/types.d.ts +4 -0
  42. package/dist-sdk/src/entities/access-policy/types.js +10 -0
  43. package/dist-sdk/src/entities/agent/deserializer/print-agent-v2.d.ts +38 -3
  44. package/dist-sdk/src/entities/agent/deserializer/print-agent-v2.js +199 -32
  45. package/dist-sdk/src/entities/agent/from-tf.d.ts +18 -3
  46. package/dist-sdk/src/entities/agent/from-tf.js +135 -15
  47. package/dist-sdk/src/entities/agent/index.d.ts +1 -1
  48. package/dist-sdk/src/entities/agent/module.js +8 -2
  49. package/dist-sdk/src/entities/agent/to-tf.js +62 -10
  50. package/dist-sdk/src/entities/agent/tools.d.ts +88 -9
  51. package/dist-sdk/src/entities/agent/tools.js +46 -7
  52. package/dist-sdk/src/entities/agent-tool/from-tf.d.ts +51 -4
  53. package/dist-sdk/src/entities/agent-tool/from-tf.js +131 -35
  54. package/dist-sdk/src/entities/agent-tool/to-tf.d.ts +4 -2
  55. package/dist-sdk/src/entities/agent-tool/to-tf.js +103 -17
  56. package/dist-sdk/src/entities/agent-tool/types.d.ts +5 -4
  57. package/dist-sdk/src/entities/agent-tool/types.js +38 -6
  58. package/dist-sdk/src/entities/app/build.d.ts +63 -13
  59. package/dist-sdk/src/entities/app/build.js +48 -1
  60. package/dist-sdk/src/entities/app/deserializer/normalize.d.ts +3 -0
  61. package/dist-sdk/src/entities/app/from-tf.d.ts +1 -1
  62. package/dist-sdk/src/entities/app/from-tf.js +32 -15
  63. package/dist-sdk/src/entities/app/index.d.ts +4 -1
  64. package/dist-sdk/src/entities/app/index.js +2 -1
  65. package/dist-sdk/src/entities/app/module.js +9 -2
  66. package/dist-sdk/src/entities/app/to-tf.d.ts +5 -0
  67. package/dist-sdk/src/entities/app/to-tf.js +33 -4
  68. package/dist-sdk/src/entities/automation/build.d.ts +57 -13
  69. package/dist-sdk/src/entities/automation/build.js +48 -36
  70. package/dist-sdk/src/entities/automation/deserializer/ast.d.ts +25 -8
  71. package/dist-sdk/src/entities/automation/deserializer/print-automation.d.ts +16 -1
  72. package/dist-sdk/src/entities/automation/deserializer/print-automation.js +179 -43
  73. package/dist-sdk/src/entities/automation/filter.d.ts +15 -3
  74. package/dist-sdk/src/entities/automation/filter.js +12 -2
  75. package/dist-sdk/src/entities/automation/from-tf.d.ts +26 -27
  76. package/dist-sdk/src/entities/automation/from-tf.js +1229 -223
  77. package/dist-sdk/src/entities/automation/index.d.ts +4 -0
  78. package/dist-sdk/src/entities/automation/index.js +4 -0
  79. package/dist-sdk/src/entities/automation/module.js +29 -32
  80. package/dist-sdk/src/entities/automation/operators.d.ts +29 -2
  81. package/dist-sdk/src/entities/automation/operators.js +27 -1
  82. package/dist-sdk/src/entities/automation/output-key-wire-name.d.ts +19 -0
  83. package/dist-sdk/src/entities/automation/output-key-wire-name.js +24 -0
  84. package/dist-sdk/src/entities/automation/script-code.d.ts +10 -21
  85. package/dist-sdk/src/entities/automation/script-code.js +272 -5
  86. package/dist-sdk/src/entities/automation/tasks.d.ts +125 -19
  87. package/dist-sdk/src/entities/automation/tasks.js +83 -15
  88. package/dist-sdk/src/entities/automation/to-tf.d.ts +10 -29
  89. package/dist-sdk/src/entities/automation/to-tf.js +1134 -231
  90. package/dist-sdk/src/entities/automation/triggers.d.ts +9 -1
  91. package/dist-sdk/src/entities/automation/types.d.ts +29 -4
  92. package/dist-sdk/src/entities/automation/types.js +76 -46
  93. package/dist-sdk/src/entities/element/build.d.ts +23 -10
  94. package/dist-sdk/src/entities/element/build.js +1 -0
  95. package/dist-sdk/src/entities/element/from-tf.d.ts +1 -1
  96. package/dist-sdk/src/entities/element/from-tf.js +18 -3
  97. package/dist-sdk/src/entities/element/index.d.ts +4 -1
  98. package/dist-sdk/src/entities/element/index.js +2 -1
  99. package/dist-sdk/src/entities/element/module.js +3 -1
  100. package/dist-sdk/src/entities/element/to-tf.js +21 -3
  101. package/dist-sdk/src/entities/field/build.d.ts +56 -13
  102. package/dist-sdk/src/entities/field/build.js +10 -0
  103. package/dist-sdk/src/entities/field/calc/runtime.js +4 -0
  104. package/dist-sdk/src/entities/field/from-tf.d.ts +5 -3
  105. package/dist-sdk/src/entities/field/from-tf.js +118 -7
  106. package/dist-sdk/src/entities/field/module.js +3 -2
  107. package/dist-sdk/src/entities/field/to-tf.d.ts +27 -9
  108. package/dist-sdk/src/entities/field/to-tf.js +62 -17
  109. package/dist-sdk/src/entities/field/types.d.ts +26 -0
  110. package/dist-sdk/src/entities/field/types.js +34 -12
  111. package/dist-sdk/src/entities/file-reader/build.d.ts +108 -50
  112. package/dist-sdk/src/entities/file-reader/build.js +23 -23
  113. package/dist-sdk/src/entities/file-reader/deserializer/print-file-reader.d.ts +6 -0
  114. package/dist-sdk/src/entities/file-reader/deserializer/print-file-reader.js +109 -0
  115. package/dist-sdk/src/entities/file-reader/from-tf.d.ts +13 -0
  116. package/dist-sdk/src/entities/file-reader/from-tf.js +172 -0
  117. package/dist-sdk/src/entities/file-reader/index.d.ts +8 -3
  118. package/dist-sdk/src/entities/file-reader/index.js +7 -2
  119. package/dist-sdk/src/entities/file-reader/module.d.ts +3 -0
  120. package/dist-sdk/src/entities/file-reader/module.js +115 -0
  121. package/dist-sdk/src/entities/file-reader/to-tf.d.ts +14 -0
  122. package/dist-sdk/src/entities/file-reader/to-tf.js +84 -0
  123. package/dist-sdk/src/entities/file-reader/types.d.ts +32 -0
  124. package/dist-sdk/src/entities/file-reader/types.js +30 -0
  125. package/dist-sdk/src/entities/org/ai-provider.d.ts +13 -0
  126. package/dist-sdk/src/entities/org/ai-provider.js +46 -0
  127. package/dist-sdk/src/entities/org/connector.d.ts +8 -1
  128. package/dist-sdk/src/entities/org/connector.js +12 -14
  129. package/dist-sdk/src/entities/org/group.js +10 -1
  130. package/dist-sdk/src/entities/org/index.d.ts +1 -0
  131. package/dist-sdk/src/entities/org/index.js +1 -0
  132. package/dist-sdk/src/entities/org/user.js +10 -1
  133. package/dist-sdk/src/entities/shared/filters/ast.d.ts +44 -9
  134. package/dist-sdk/src/entities/shared/filters/types.d.ts +1 -1
  135. package/dist-sdk/src/entities/shared/tool-field-ref.d.ts +17 -0
  136. package/dist-sdk/src/entities/shared/tool-field-ref.js +31 -0
  137. package/dist-sdk/src/entities/shared/types/field-types.d.ts +10 -1
  138. package/dist-sdk/src/entities/shared/types/field-types.js +9 -0
  139. package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.d.ts +19 -0
  140. package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.js +36 -8
  141. package/dist-sdk/src/entities/skill/from-tf.js +4 -3
  142. package/dist-sdk/src/entities/skill/index.d.ts +1 -1
  143. package/dist-sdk/src/entities/skill/module.js +6 -1
  144. package/dist-sdk/src/entities/skill/to-tf.js +3 -2
  145. package/dist-sdk/src/entities/skill/tools.d.ts +25 -3
  146. package/dist-sdk/src/entities/skill/tools.js +17 -0
  147. package/dist-sdk/src/entities/skill-tool/from-tf.d.ts +15 -2
  148. package/dist-sdk/src/entities/skill-tool/from-tf.js +32 -11
  149. package/dist-sdk/src/entities/skill-tool/to-tf.d.ts +2 -0
  150. package/dist-sdk/src/entities/skill-tool/to-tf.js +32 -9
  151. package/dist-sdk/src/entities/view/from-tf.d.ts +6 -2
  152. package/dist-sdk/src/entities/view/from-tf.js +11 -9
  153. package/dist-sdk/src/entities/view/to-tf.d.ts +9 -7
  154. package/dist-sdk/src/entities/view/to-tf.js +33 -18
  155. package/dist-sdk/src/entities/view/types.d.ts +1 -1
  156. package/dist-sdk/src/entities/view/types.js +1 -1
  157. package/dist-sdk/src/graph/deferred-catalog-render.d.ts +0 -5
  158. package/dist-sdk/src/graph/deferred-catalog-render.js +1 -20
  159. package/dist-sdk/src/graph/index.d.ts +8 -0
  160. package/dist-sdk/src/graph/index.js +13 -9
  161. package/dist-sdk/src/graph/modules.js +6 -1
  162. package/dist-sdk/src/graph/types.d.ts +77 -3
  163. package/package.json +42 -14
  164. package/dist-sdk/src/entities/automation/value-ref-tf.d.ts +0 -76
  165. package/dist-sdk/src/entities/automation/value-ref-tf.js +0 -190
@@ -6,8 +6,8 @@
6
6
  */
7
7
  import { EntityTfError } from "../../engine/entity.js";
8
8
  import { optionalString, resourceRefLabel } from "../../engine/pull/context.js";
9
- import { isTfExpr, tfLabelToTsLabel } from "../../engine/tf/index.js";
10
- import { SUB_TYPE_BY_RESOURCE as FIELD_SUB_TYPE_BY_RESOURCE } from "../field/types.js";
9
+ import { isTfExpr, scopedLabelToBagKey } from "../../engine/tf/index.js";
10
+ import { resolveToolFieldKey } from "../shared/tool-field-ref.js";
11
11
  import { KNOWN_TOOL_ATTRS, TOOL_SUB_TYPE_BY_RESOURCE } from "./types.js";
12
12
  /**
13
13
  * One tool block → its normalized shape; undefined = skipped with a
@@ -41,58 +41,146 @@ export function toolFromTf(block, owner, ctx) {
41
41
  return undefined;
42
42
  }
43
43
  if (subType.typeKey === "executeWorkflow") {
44
- return executeWorkflowFromTf(block, name, where, ctx);
44
+ return executeWorkflowFromTf(block, name, where, owner, ctx);
45
45
  }
46
46
  // Target: the owning app's resource ref = self; a data.elementum_app ref =
47
47
  // a foreign app, resolved to its display name via the stream's data block.
48
+ // searchTable is the one sub-type where target_id can be absent entirely
49
+ // (confirmed live: a search table tool searches the table itself, with no
50
+ // "live record" target required) — every other implemented sub-type
51
+ // requires it.
48
52
  const targetAttr = attrs["target_id"];
49
53
  let target;
50
- const selfLabel = resourceRefLabel(targetAttr, owner.appType);
51
- if (selfLabel === owner.appLabel) {
52
- target = { kind: "self" };
53
- }
54
- else if (isTfExpr(targetAttr) && targetAttr.$expr === "data-attr" && targetAttr.type === "elementum_app") {
55
- const appName = ctx.dataName("elementum_app", targetAttr.label);
56
- if (appName === undefined) {
57
- ctx.diagnostics.push(`${where}: target_id references data.elementum_app.${targetAttr.label} but the stream has no such data block; skipped`);
54
+ // The tool's fields are scoped under the RESOLVED target aspect, not
55
+ // always the owning app (board issue #169) — a field on a foreign
56
+ // app/element carries that aspect's own label, mirroring the
57
+ // foreignApp/foreignElement resolution below.
58
+ let fieldScopeLabel = owner.ownerTfLabel;
59
+ let fieldScope = "app";
60
+ if (targetAttr === undefined) {
61
+ if (subType.typeKey !== "searchTable") {
62
+ ctx.diagnostics.push(`${where}: target_id is missing; skipped`);
58
63
  return undefined;
59
64
  }
60
- target = { kind: "foreignApp", name: appName };
61
65
  }
62
66
  else {
63
- ctx.diagnostics.push(`${where}: target_id has an unrecognized shape (${JSON.stringify(targetAttr)}); skipped`);
64
- return undefined;
67
+ const selfLabel = resourceRefLabel(targetAttr, owner.appType);
68
+ if (selfLabel === owner.appLabel) {
69
+ target = { kind: "self" };
70
+ }
71
+ else if (isTfExpr(targetAttr) && targetAttr.$expr === "data-attr" && targetAttr.type === "elementum_app") {
72
+ const appName = ctx.dataName("elementum_app", targetAttr.label);
73
+ if (appName === undefined) {
74
+ ctx.diagnostics.push(`${where}: target_id references data.elementum_app.${targetAttr.label} but the stream has no such data block; skipped`);
75
+ return undefined;
76
+ }
77
+ target = { kind: "foreignApp", name: appName };
78
+ fieldScopeLabel = targetAttr.label;
79
+ }
80
+ else if (isTfExpr(targetAttr) && targetAttr.$expr === "data-attr" && targetAttr.type === "elementum_element") {
81
+ // Board issue #42: an aspect target can be an element, not just an app —
82
+ // same shape as the foreignApp branch, just the other data type.
83
+ const elementName = ctx.dataName("elementum_element", targetAttr.label);
84
+ if (elementName === undefined) {
85
+ ctx.diagnostics.push(`${where}: target_id references data.elementum_element.${targetAttr.label} but the stream has no such data block; skipped`);
86
+ return undefined;
87
+ }
88
+ target = { kind: "foreignElement", name: elementName };
89
+ fieldScopeLabel = targetAttr.label;
90
+ fieldScope = "element";
91
+ }
92
+ else {
93
+ ctx.diagnostics.push(`${where}: target_id has an unrecognized shape (${JSON.stringify(targetAttr)}); skipped`);
94
+ return undefined;
95
+ }
65
96
  }
66
97
  let fields;
98
+ let searchTableFields;
67
99
  const rawFields = attrs["fields"];
68
100
  if (Array.isArray(rawFields)) {
69
- fields = [];
70
- for (const item of rawFields) {
101
+ if (subType.typeKey === "searchTable") {
102
+ searchTableFields = [];
103
+ for (const item of rawFields) {
104
+ if (typeof item !== "object" || item === null || Array.isArray(item) || isTfExpr(item)) {
105
+ ctx.diagnostics.push(`${where}: fields item has an unrecognized shape; dropped`);
106
+ continue;
107
+ }
108
+ const rec = item;
109
+ if (typeof rec["field_id"] !== "string" || rec["field_id"] === "") {
110
+ ctx.diagnostics.push(`${where}: fields item has no field_id; dropped`);
111
+ continue;
112
+ }
113
+ searchTableFields.push({
114
+ fieldId: rec["field_id"],
115
+ ...(typeof rec["name"] === "string" && { name: rec["name"] }),
116
+ ...(typeof rec["description"] === "string" && { description: rec["description"] }),
117
+ });
118
+ }
119
+ if (searchTableFields.length === 0)
120
+ searchTableFields = undefined;
121
+ }
122
+ else {
123
+ fields = [];
124
+ for (const item of rawFields) {
125
+ if (typeof item !== "object" || item === null || Array.isArray(item) || isTfExpr(item)) {
126
+ ctx.diagnostics.push(`${where}: fields item has an unrecognized shape; dropped`);
127
+ continue;
128
+ }
129
+ const rec = item;
130
+ const fieldKey = resolveToolFieldKey(rec["field_id"], fieldScopeLabel, fieldScope);
131
+ // A field on a foreign/out-of-scope aspect has no field resource ei
132
+ // can reference — it falls back to a bare platform id, confirmed
133
+ // live (board issue #169). Preserve it (raw-id TODO placeholder)
134
+ // rather than silently dropping the field.
135
+ const rawFieldId = fieldKey === undefined && typeof rec["field_id"] === "string" && rec["field_id"] !== "" ? rec["field_id"] : undefined;
136
+ if (fieldKey === undefined && rawFieldId === undefined) {
137
+ ctx.diagnostics.push(`${where}: fields item's field_id is not a field resource reference; dropped`);
138
+ continue;
139
+ }
140
+ fields.push({
141
+ ...(fieldKey !== undefined ? { key: fieldKey } : { rawFieldId: rawFieldId }),
142
+ ...(typeof rec["name"] === "string" && { name: rec["name"] }),
143
+ ...(typeof rec["description"] === "string" && { description: rec["description"] }),
144
+ ...(typeof rec["required"] === "boolean" && { required: rec["required"] }),
145
+ });
146
+ }
147
+ if (fields.length === 0)
148
+ fields = undefined;
149
+ }
150
+ }
151
+ else if (rawFields !== undefined) {
152
+ ctx.diagnostics.push(`${where}: fields is not a list; dropped`);
153
+ }
154
+ // searchTable's field_id is ALWAYS a raw platform id, never a resource
155
+ // reference (confirmed live, 161/161 real captures) — these describe
156
+ // columns of the search table's own schema, not app/element fields, so
157
+ // there's no elementum_*_field resource (and no catalog bag key) to
158
+ // resolve them to.
159
+ let returnFields;
160
+ const rawReturnFields = attrs["return_fields"];
161
+ if (Array.isArray(rawReturnFields)) {
162
+ returnFields = [];
163
+ for (const item of rawReturnFields) {
71
164
  if (typeof item !== "object" || item === null || Array.isArray(item) || isTfExpr(item)) {
72
- ctx.diagnostics.push(`${where}: fields item has an unrecognized shape; dropped`);
165
+ ctx.diagnostics.push(`${where}: return_fields item has an unrecognized shape; dropped`);
73
166
  continue;
74
167
  }
75
168
  const rec = item;
76
- const fieldRef = rec["field_id"];
77
- const fieldLabel = isTfExpr(fieldRef) && fieldRef.$expr === "resource-attr" && FIELD_SUB_TYPE_BY_RESOURCE.has(fieldRef.type)
78
- ? fieldRef.label
79
- : undefined;
80
- if (fieldLabel === undefined) {
81
- ctx.diagnostics.push(`${where}: fields item's field_id is not a field resource reference; dropped`);
169
+ if (typeof rec["field_id"] !== "string" || rec["field_id"] === "") {
170
+ ctx.diagnostics.push(`${where}: return_fields item has no field_id; dropped`);
82
171
  continue;
83
172
  }
84
- fields.push({
85
- key: tfLabelToTsLabel(fieldLabel),
173
+ returnFields.push({
174
+ fieldId: rec["field_id"],
86
175
  ...(typeof rec["name"] === "string" && { name: rec["name"] }),
87
- ...(typeof rec["description"] === "string" && { description: rec["description"] }),
88
- ...(typeof rec["required"] === "boolean" && { required: rec["required"] }),
176
+ ...(typeof rec["display_name"] === "string" && { displayName: rec["display_name"] }),
89
177
  });
90
178
  }
91
- if (fields.length === 0)
92
- fields = undefined;
179
+ if (returnFields.length === 0)
180
+ returnFields = undefined;
93
181
  }
94
- else if (rawFields !== undefined) {
95
- ctx.diagnostics.push(`${where}: fields is not a list; dropped`);
182
+ else if (rawReturnFields !== undefined) {
183
+ ctx.diagnostics.push(`${where}: return_fields is not a list; dropped`);
96
184
  }
97
185
  if (subType.typeKey === "searchAspect" && attrs["filter_variables"] !== undefined) {
98
186
  ctx.diagnostics.push(`${where}: filter_variables are not supported on the terraform path yet; a rebuilt stream will omit them`);
@@ -103,12 +191,20 @@ export function toolFromTf(block, owner, ctx) {
103
191
  name,
104
192
  description: optionalString(attrs, "description", where, ctx) ?? "",
105
193
  ...(startMessage !== undefined && { startMessage }),
106
- target,
194
+ ...(target !== undefined && { target }),
107
195
  ...(fields !== undefined && { fields }),
108
- ...(subType.typeKey === "searchAspect" && {
196
+ ...(searchTableFields !== undefined && { searchTableFields }),
197
+ ...(returnFields !== undefined && { returnFields }),
198
+ ...((subType.typeKey === "searchAspect" || subType.typeKey === "searchTable") && {
109
199
  queryDescription: optionalString(attrs, "query_description", where, ctx) ?? "",
110
200
  }),
111
201
  ...(typeof attrs["result_limit"] === "number" && { limit: attrs["result_limit"] }),
202
+ ...(subType.typeKey === "updateRecord" && {
203
+ handleDescription: optionalString(attrs, "handle_description", where, ctx) ?? "",
204
+ }),
205
+ ...(subType.typeKey === "searchTable" && typeof attrs["search_table_id"] === "string" && {
206
+ searchTableId: attrs["search_table_id"],
207
+ }),
112
208
  };
113
209
  }
114
210
  /**
@@ -117,7 +213,7 @@ export function toolFromTf(block, owner, ctx) {
117
213
  * generated after the automation's, and both carry the same platform id) or
118
214
  * the automation resource itself; both labels are the automation's.
119
215
  */
120
- function executeWorkflowFromTf(block, name, where, ctx) {
216
+ function executeWorkflowFromTf(block, name, where, owner, ctx) {
121
217
  const attrs = block.attrs;
122
218
  const autoRef = attrs["automation_id"];
123
219
  const autoLabel = isTfExpr(autoRef) &&
@@ -162,7 +258,7 @@ function executeWorkflowFromTf(block, name, where, ctx) {
162
258
  name,
163
259
  description: optionalString(attrs, "description", where, ctx) ?? "",
164
260
  ...(startMessage !== undefined && { startMessage }),
165
- automationKey: tfLabelToTsLabel(autoLabel),
261
+ automationKey: scopedLabelToBagKey(owner.ownerTfLabel, autoLabel),
166
262
  ...(inputs.length > 0 && { inputs }),
167
263
  ...(outputs.length > 0 && { outputs }),
168
264
  };
@@ -17,10 +17,12 @@ export interface ToolToTfContext {
17
17
  /** The owning app's refName + tfLabel (namespace) — self-target detection + app_id. */
18
18
  readonly appRefName: string;
19
19
  readonly appTfLabel: string;
20
- /** The owning agent's tfLabel. */
21
- readonly agentTfLabel: string;
20
+ /** The owning agent's bare tfLabel (before apps_ prefix). */
21
+ readonly agentBareTfLabel: string;
22
22
  /** Records a foreign-app data lookup for data.tf; returns its tfLabel. */
23
23
  foreignAppRef(tfLabel: string, name: string): void;
24
+ /** Records a foreign-ELEMENT data lookup for data.tf (board issue #42). */
25
+ foreignElementRef(tfLabel: string, name: string): void;
24
26
  /** Degradations land here — reported by the caller, never silent. */
25
27
  readonly diagnostics: string[];
26
28
  }
@@ -12,32 +12,46 @@
12
12
  * - tool fields → the field resource's ref (`elementum_*_field.<label>.id`),
13
13
  * derivable purely from the FieldRef token (fieldType + refName).
14
14
  */
15
- import { expr, sanitizeName, tsLabelToTfLabel } from "../../engine/tf/index.js";
16
- import { SUB_TYPE_BY_TYPE_KEY as FIELD_SUB_TYPE_BY_TYPE_KEY } from "../field/types.js";
15
+ import { appScopedResourceLabel, expr, sanitizeName, tsLabelToTfLabel } from "../../engine/tf/index.js";
16
+ import { EntityTfError } from "../../engine/entity.js";
17
+ import { fieldRefToTfValue } from "../field/to-tf.js";
17
18
  import { TOOL_SUB_TYPE_BY_TYPE_KEY } from "./types.js";
18
19
  /** authoring FieldRef token → the field resource address, derived purely from the token. */
19
20
  function fieldRefValue(token, where, ctx) {
20
21
  const refName = token.refName ?? "";
21
- const authoringType = token.fieldType ?? "";
22
- const subType = [...FIELD_SUB_TYPE_BY_TYPE_KEY.values()].find((t) => t.authoringType === authoringType);
23
- if (refName === "" || subType === undefined) {
24
- ctx.diagnostics.push(`${where}: field token "${refName}" has no terraform field resource mapping (fieldType ${JSON.stringify(authoringType)}); dropped`);
22
+ if (refName === "") {
23
+ ctx.diagnostics.push(`${where}: field token has no refName; dropped`);
25
24
  return undefined;
26
25
  }
27
26
  if (token.parent !== undefined && token.parent !== ctx.appRefName) {
28
27
  ctx.diagnostics.push(`${where}: field token "${refName}" belongs to app "${token.parent}", not this workspace's config — cross-app field refs are not supported on the terraform path yet; dropped`);
29
28
  return undefined;
30
29
  }
31
- return expr.resourceAttr(subType.resourceType, tsLabelToTfLabel(refName), ["id"]);
30
+ // Delegates to field/to-tf.ts's shared resolver (system-field local alias
31
+ // vs. real field resource, by authoring type) rather than reimplementing
32
+ // it here — see fieldRefToTfValue's doc comment for the system-field
33
+ // ordering rationale.
34
+ const value = fieldRefToTfValue(token.fieldType ?? "", refName, ctx.appTfLabel);
35
+ if (value === undefined) {
36
+ ctx.diagnostics.push(`${where}: field token "${refName}" has no terraform field resource mapping (fieldType ${JSON.stringify(token.fieldType ?? "")}); dropped`);
37
+ }
38
+ return value;
32
39
  }
33
40
  /** authoring AspectRef token → target_id value (self resource ref or foreign data ref). */
34
41
  function targetIdValue(token, ctx) {
35
- if ((token.refName ?? "") === ctx.appRefName) {
42
+ if (token.__kind !== "element" && (token.refName ?? "") === ctx.appRefName) {
36
43
  return expr.resourceAttr("elementum_app", ctx.appTfLabel, ["id"]);
37
44
  }
38
- // ei derives foreign-app data labels from the display NAME.
45
+ // ei derives foreign-app/foreign-element data labels from the display NAME.
39
46
  const name = token.name ?? token.refName ?? "";
40
47
  const tfLabel = sanitizeName(name);
48
+ // Board issue #42: an aspect token can be an element, not just an app —
49
+ // must emit `data.elementum_element`, never `data.elementum_app`, or the
50
+ // provider looks up the wrong resource type entirely.
51
+ if (token.__kind === "element") {
52
+ ctx.foreignElementRef(tfLabel, name);
53
+ return expr.dataAttr("elementum_element", tfLabel, ["id"]);
54
+ }
41
55
  ctx.foreignAppRef(tfLabel, name);
42
56
  return expr.dataAttr("elementum_app", tfLabel, ["id"]);
43
57
  }
@@ -73,9 +87,10 @@ export function toolToTf(entry, ctx) {
73
87
  ctx.diagnostics.push(`agent tool "${name}": runAs is not supported on the terraform path yet; dropped`);
74
88
  }
75
89
  const where = `agent tool "${name}"`;
76
- const tfLabel = sanitizeName(`${ctx.agentTfLabel}_${name}`);
90
+ const agentTfLabel = appScopedResourceLabel(ctx.appTfLabel, ctx.agentBareTfLabel);
91
+ const tfLabel = appScopedResourceLabel(ctx.appTfLabel, sanitizeName(`${ctx.agentBareTfLabel}_${name}`));
77
92
  const attrs = {
78
- agent_id: expr.resourceAttr("elementum_agent", ctx.agentTfLabel, ["id"]),
93
+ agent_id: expr.resourceAttr("elementum_agent", agentTfLabel, ["id"]),
79
94
  app_id: expr.resourceAttr("elementum_app", ctx.appTfLabel, ["id"]),
80
95
  name,
81
96
  description: inner.description ?? "",
@@ -105,7 +120,7 @@ export function toolToTf(entry, ctx) {
105
120
  // until the provider has published the workflow whose bindings it reads.
106
121
  // ei export normalizes this back to elementum_automation.<label>.id; the
107
122
  // live round-trip comparator treats only those two references as equal.
108
- attrs["automation_id"] = expr.resourceAttr("elementum_workflow_publish", autoTfLabel, ["id"]);
123
+ attrs["automation_id"] = expr.resourceAttr("elementum_workflow_publish", appScopedResourceLabel(ctx.appTfLabel, autoTfLabel), ["id"]);
109
124
  if (inner.inputs !== undefined && inner.inputs.length > 0) {
110
125
  attrs["inputs"] = inner.inputs.map((i) => ({
111
126
  parameter_name: i.triggerParameterId?.name ?? "",
@@ -126,12 +141,25 @@ export function toolToTf(entry, ctx) {
126
141
  const block = { kind: "resource", type: subType.resourceType, label: tfLabel, attrs };
127
142
  return { block, address: `${subType.resourceType}.${tfLabel}` };
128
143
  }
129
- if (inner.aspectId === undefined) {
144
+ // searchTable is the one sub-type where a target aspect is optional
145
+ // (confirmed live — see toolFromTf's matching comment).
146
+ if (inner.aspectId === undefined && typeKey !== "searchTable") {
130
147
  ctx.diagnostics.push(`${where}: no target aspect token; dropped`);
131
148
  return undefined;
132
149
  }
133
- attrs["target_id"] = targetIdValue(inner.aspectId, ctx);
134
- if (typeKey === "searchAspect") {
150
+ if (inner.aspectId !== undefined)
151
+ attrs["target_id"] = targetIdValue(inner.aspectId, ctx);
152
+ if (typeKey === "updateRecord") {
153
+ if (inner.handleDescription === undefined || inner.handleDescription === "") {
154
+ // The provider requires a non-empty handle_description and rejects an
155
+ // empty one at apply with an opaque "Unable to validate" error — fail
156
+ // loud at build time instead (same discipline as the tool-name check).
157
+ throw new EntityTfError(`${where}: agentUpdateRecord requires a non-empty \`handleDescription\` — the platform ` +
158
+ `uses it to identify which record to update and rejects an empty value at apply.`);
159
+ }
160
+ attrs["handle_description"] = inner.handleDescription;
161
+ }
162
+ if (typeKey === "searchAspect" || typeKey === "searchTable") {
135
163
  attrs["query_description"] = inner.queryDescription ?? "";
136
164
  if (inner.limit !== undefined)
137
165
  attrs["result_limit"] = inner.limit;
@@ -139,10 +167,27 @@ export function toolToTf(entry, ctx) {
139
167
  ctx.diagnostics.push(`${where}: filter/sort are not supported on the terraform path yet; dropped`);
140
168
  }
141
169
  }
170
+ if (typeKey === "searchTable") {
171
+ if (inner.searchTableId === undefined || inner.searchTableId === "") {
172
+ ctx.diagnostics.push(`${where}: no searchTableId; dropped`);
173
+ return undefined;
174
+ }
175
+ // Always a raw platform id — see ReturnFieldWire's sibling comment on
176
+ // searchTableId. There is no data-source/reference form to resolve
177
+ // against; ei itself never exports one for this attribute.
178
+ attrs["search_table_id"] = inner.searchTableId;
179
+ }
142
180
  if (inner.fields !== undefined && inner.fields.length > 0) {
143
181
  const fieldValues = [];
144
182
  for (const f of inner.fields) {
145
- const ref = f.fieldId === undefined ? undefined : fieldRefValue(f.fieldId, where, ctx);
183
+ // A raw-id fallback field (board issue #169) is echoed straight
184
+ // through — ei never resolves it to a reference either, so there's
185
+ // nothing to re-derive.
186
+ const ref = typeof f.fieldId === "string"
187
+ ? f.fieldId
188
+ : f.fieldId === undefined
189
+ ? undefined
190
+ : fieldRefValue(f.fieldId, where, ctx);
146
191
  if (ref === undefined)
147
192
  continue;
148
193
  fieldValues.push({
@@ -150,7 +195,13 @@ export function toolToTf(entry, ctx) {
150
195
  // The wire duplicates the display name; the dialect carries one.
151
196
  ...(f.name !== undefined && { name: f.name }),
152
197
  ...(f.description !== undefined && { description: f.description }),
153
- ...(f.required !== undefined && { required: f.required }),
198
+ // Always emit `required` (defaulting false) rather than omitting it:
199
+ // the provider's own Create/Read round trip normalizes a missing
200
+ // value to `false` and returns it, so a config that omits the key
201
+ // plans an eternal `required: false -> null` diff on every apply
202
+ // after the first (confirmed live 2026-08-05). Pin it, like
203
+ // executeWorkflow's `timeout`, so config and state agree.
204
+ required: f.required ?? false,
154
205
  });
155
206
  if (f.defaultValue !== undefined) {
156
207
  ctx.diagnostics.push(`${where}: field default values are not supported on the terraform path yet; dropped`);
@@ -159,6 +210,41 @@ export function toolToTf(entry, ctx) {
159
210
  if (fieldValues.length > 0)
160
211
  attrs["fields"] = fieldValues;
161
212
  }
213
+ // searchTable's own field shapes: field_id is always a raw platform id
214
+ // (see SearchTableFieldWire/ReturnFieldWire's comments) - no
215
+ // fieldRefValue resolution, unlike the shared `fields` block above.
216
+ if (inner.searchTableFields !== undefined && inner.searchTableFields.length > 0) {
217
+ const values = [];
218
+ for (const f of inner.searchTableFields) {
219
+ if (f.fieldId === undefined || f.fieldId === "") {
220
+ ctx.diagnostics.push(`${where}: fields item has no fieldId; dropped`);
221
+ continue;
222
+ }
223
+ values.push({
224
+ field_id: f.fieldId,
225
+ ...(f.name !== undefined && { name: f.name }),
226
+ ...(f.description !== undefined && { description: f.description }),
227
+ });
228
+ }
229
+ if (values.length > 0)
230
+ attrs["fields"] = values;
231
+ }
232
+ if (inner.returnFields !== undefined && inner.returnFields.length > 0) {
233
+ const returnFieldValues = [];
234
+ for (const f of inner.returnFields) {
235
+ if (f.fieldId === undefined || f.fieldId === "") {
236
+ ctx.diagnostics.push(`${where}: return_fields item has no fieldId; dropped`);
237
+ continue;
238
+ }
239
+ returnFieldValues.push({
240
+ field_id: f.fieldId,
241
+ ...(f.name !== undefined && { name: f.name }),
242
+ ...(f.displayName !== undefined && { display_name: f.displayName }),
243
+ });
244
+ }
245
+ if (returnFieldValues.length > 0)
246
+ attrs["return_fields"] = returnFieldValues;
247
+ }
162
248
  const block = { kind: "resource", type: subType.resourceType, label: tfLabel, attrs };
163
249
  return { block, address: `${subType.resourceType}.${tfLabel}` };
164
250
  }
@@ -4,10 +4,11 @@
4
4
  * (agent_id + app_id + name/description) — so per-sub-type knowledge is one
5
5
  * ROW here, exactly like `entities/field/types.ts`.
6
6
  *
7
- * M3 implements createRecord + searchRecords end to end; the other rows are
8
- * declared so streams REPORT them by name (plan §6) instead of "unknown
9
- * resource", and adoption scopes ONLY the implemented ones — an adopted
10
- * type the printer can't reconstruct would plan as a destroy on rebuild.
7
+ * M3 implements createRecord, searchAspect, executeWorkflow, and updateRecord
8
+ * end to end; the other rows are declared so streams REPORT them by name
9
+ * (plan §6) instead of "unknown resource", and adoption scopes ONLY the
10
+ * implemented ones — an adopted type the printer can't reconstruct would
11
+ * plan as a destroy on rebuild.
11
12
  */
12
13
  /** One agent-tool sub-type: authoring variant key ↔ resource type. */
13
14
  export interface AgentToolSubType {
@@ -4,17 +4,18 @@
4
4
  * (agent_id + app_id + name/description) — so per-sub-type knowledge is one
5
5
  * ROW here, exactly like `entities/field/types.ts`.
6
6
  *
7
- * M3 implements createRecord + searchRecords end to end; the other rows are
8
- * declared so streams REPORT them by name (plan §6) instead of "unknown
9
- * resource", and adoption scopes ONLY the implemented ones — an adopted
10
- * type the printer can't reconstruct would plan as a destroy on rebuild.
7
+ * M3 implements createRecord, searchAspect, executeWorkflow, and updateRecord
8
+ * end to end; the other rows are declared so streams REPORT them by name
9
+ * (plan §6) instead of "unknown resource", and adoption scopes ONLY the
10
+ * implemented ones — an adopted type the printer can't reconstruct would
11
+ * plan as a destroy on rebuild.
11
12
  */
12
13
  export const RUN_AUTOMATION_TOOL_RESOURCE_TYPE = "elementum_agent_run_automation_tool";
13
14
  export const AGENT_TOOL_SUB_TYPES = [
14
15
  { typeKey: "createRecord", resourceType: "elementum_agent_create_record_tool", implemented: true },
15
16
  { typeKey: "searchAspect", resourceType: "elementum_agent_search_records_tool", implemented: true },
16
- { typeKey: "updateRecord", resourceType: "elementum_agent_update_record_tool" },
17
- { typeKey: "searchTable", resourceType: "elementum_agent_ai_search_tool" },
17
+ { typeKey: "updateRecord", resourceType: "elementum_agent_update_record_tool", implemented: true },
18
+ { typeKey: "searchTable", resourceType: "elementum_agent_ai_search_tool", implemented: true },
18
19
  { typeKey: "relateRecord", resourceType: "elementum_agent_relate_record_tool" },
19
20
  { typeKey: "executeWorkflow", resourceType: RUN_AUTOMATION_TOOL_RESOURCE_TYPE, implemented: true },
20
21
  { typeKey: "runAgent", resourceType: "elementum_agent_run_agent_tool" },
@@ -67,4 +68,35 @@ export const KNOWN_TOOL_ATTRS = {
67
68
  "fields",
68
69
  "filter_variables",
69
70
  ]),
71
+ updateRecord: new Set([
72
+ "agent_id",
73
+ "app_id",
74
+ "name",
75
+ "description",
76
+ "start_message",
77
+ "target_id",
78
+ "handle_description",
79
+ "fields",
80
+ ]),
81
+ // target_id is OPTIONAL here (unlike the other three) - confirmed live,
82
+ // 5 of 18 real elementum_agent_ai_search_tool blocks have no target_id at
83
+ // all. return_fields (field_id/name/display_name) is the dialect ei's own
84
+ // export still emits on most tools; a minority have already migrated to
85
+ // the newer `fields` (field_id/name/description) shape the provider
86
+ // schema is deprecating return_fields in favor of - both must round-trip,
87
+ // since re-emitting the wrong one on an already-migrated tool would plan
88
+ // a spurious diff on every apply.
89
+ searchTable: new Set([
90
+ "agent_id",
91
+ "app_id",
92
+ "name",
93
+ "description",
94
+ "start_message",
95
+ "target_id",
96
+ "search_table_id",
97
+ "query_description",
98
+ "result_limit",
99
+ "fields",
100
+ "return_fields",
101
+ ]),
70
102
  };