@elementumai/edk 0.9.2 → 0.9.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 (296) hide show
  1. package/README.md +34 -26
  2. package/dist-sdk/elementum-entity-client/clients/automation/models/automation.d.ts +1 -1
  3. package/dist-sdk/elementum-entity-client/clients/skills/models/skill.d.ts +1 -1
  4. package/dist-sdk/elementum-entity-client/clients/skills/models/skill.js +1 -1
  5. package/dist-sdk/pull/util.d.ts +1 -1
  6. package/dist-sdk/pull/util.js +3 -3
  7. package/dist-sdk/src/catalog/factories.d.ts +49 -10
  8. package/dist-sdk/src/catalog/factories.js +94 -13
  9. package/dist-sdk/src/catalog/from-authoring.d.ts +5 -5
  10. package/dist-sdk/src/catalog/from-authoring.js +7 -7
  11. package/dist-sdk/src/catalog/index.d.ts +2 -2
  12. package/dist-sdk/src/catalog/index.js +1 -1
  13. package/dist-sdk/src/catalog/org.js +62 -4
  14. package/dist-sdk/src/catalog/render-helpers.d.ts +1 -1
  15. package/dist-sdk/src/catalog/render-helpers.js +1 -1
  16. package/dist-sdk/src/catalog/render.d.ts +1 -0
  17. package/dist-sdk/src/catalog/render.js +2 -2
  18. package/dist-sdk/src/catalog/token.js +11 -4
  19. package/dist-sdk/src/catalog/types.d.ts +33 -4
  20. package/dist-sdk/src/catalog/types.js +5 -0
  21. package/dist-sdk/src/core/index.d.ts +10 -10
  22. package/dist-sdk/src/core/index.js +9 -9
  23. package/dist-sdk/src/core/render-tree.d.ts +1 -1
  24. package/dist-sdk/src/core/render-tree.js +1 -2
  25. package/dist-sdk/src/core/types/ai-provider.d.ts +1 -1
  26. package/dist-sdk/src/core/types/ai-provider.js +1 -1
  27. package/dist-sdk/src/core/types/app.d.ts +66 -30
  28. package/dist-sdk/src/core/types/app.js +2 -2
  29. package/dist-sdk/src/core/types/aspect-category.d.ts +4 -4
  30. package/dist-sdk/src/core/types/aspect-category.js +4 -4
  31. package/dist-sdk/src/core/types/cloud-link.d.ts +3 -3
  32. package/dist-sdk/src/core/types/cloud-link.js +3 -3
  33. package/dist-sdk/src/core/types/display-block.d.ts +2 -2
  34. package/dist-sdk/src/core/types/display-block.js +2 -2
  35. package/dist-sdk/src/core/types/group.d.ts +1 -1
  36. package/dist-sdk/src/core/types/group.js +1 -1
  37. package/dist-sdk/src/core/types/user.d.ts +1 -1
  38. package/dist-sdk/src/core/types/user.js +1 -1
  39. package/dist-sdk/src/core/wire/ref-serializer.js +3 -3
  40. package/dist-sdk/src/engine/build/context.d.ts +20 -13
  41. package/dist-sdk/src/engine/entity.d.ts +1 -1
  42. package/dist-sdk/src/engine/pull/context.d.ts +34 -0
  43. package/dist-sdk/src/engine/pull/context.js +14 -0
  44. package/dist-sdk/src/engine/pull/org.d.ts +9 -0
  45. package/dist-sdk/src/engine/pull/org.js +33 -0
  46. package/dist-sdk/src/engine/pull/ref-only.d.ts +37 -0
  47. package/dist-sdk/src/engine/pull/ref-only.js +204 -0
  48. package/dist-sdk/src/engine/tf/expr.d.ts +6 -2
  49. package/dist-sdk/src/engine/tf/expr.js +18 -13
  50. package/dist-sdk/src/engine/tf/index.d.ts +1 -1
  51. package/dist-sdk/src/engine/tf/index.js +1 -1
  52. package/dist-sdk/src/engine/tf/model.d.ts +8 -1
  53. package/dist-sdk/src/engine/tf/model.js +4 -0
  54. package/dist-sdk/src/engine/tf/parse.js +13 -2
  55. package/dist-sdk/src/engine/tf/render.js +4 -0
  56. package/dist-sdk/src/entities/access-policy/build.d.ts +4 -5
  57. package/dist-sdk/src/entities/access-policy/build.js +1 -1
  58. package/dist-sdk/src/entities/access-policy/from-tf.d.ts +4 -4
  59. package/dist-sdk/src/entities/access-policy/from-tf.js +12 -13
  60. package/dist-sdk/src/entities/access-policy/principal.d.ts +14 -4
  61. package/dist-sdk/src/entities/access-policy/to-tf.d.ts +14 -2
  62. package/dist-sdk/src/entities/access-policy/to-tf.js +36 -6
  63. package/dist-sdk/src/entities/agent/build.d.ts +3 -3
  64. package/dist-sdk/src/entities/agent/build.js +5 -5
  65. package/dist-sdk/src/entities/agent/deserializer/print-agent-v2.d.ts +6 -1
  66. package/dist-sdk/src/entities/agent/deserializer/print-agent-v2.js +68 -18
  67. package/dist-sdk/src/entities/agent/from-tf.d.ts +3 -1
  68. package/dist-sdk/src/entities/agent/from-tf.js +12 -4
  69. package/dist-sdk/src/entities/agent/index.d.ts +2 -2
  70. package/dist-sdk/src/entities/agent/index.js +1 -1
  71. package/dist-sdk/src/entities/agent/module.js +2 -0
  72. package/dist-sdk/src/entities/agent/to-tf.d.ts +5 -2
  73. package/dist-sdk/src/entities/agent/to-tf.js +56 -8
  74. package/dist-sdk/src/entities/agent/tools.d.ts +38 -23
  75. package/dist-sdk/src/entities/agent/tools.js +21 -7
  76. package/dist-sdk/src/entities/agent-evaluation/build.d.ts +19 -0
  77. package/dist-sdk/src/entities/agent-evaluation/build.js +41 -0
  78. package/dist-sdk/src/entities/agent-evaluation/deserializer/print-agent-evaluation.d.ts +4 -0
  79. package/dist-sdk/src/entities/agent-evaluation/deserializer/print-agent-evaluation.js +30 -0
  80. package/dist-sdk/src/entities/agent-evaluation/from-tf.d.ts +12 -0
  81. package/dist-sdk/src/entities/agent-evaluation/from-tf.js +46 -0
  82. package/dist-sdk/src/entities/agent-evaluation/index.d.ts +1 -0
  83. package/dist-sdk/src/entities/agent-evaluation/index.js +1 -0
  84. package/dist-sdk/src/entities/agent-evaluation/module.d.ts +3 -0
  85. package/dist-sdk/src/entities/agent-evaluation/module.js +58 -0
  86. package/dist-sdk/src/entities/agent-evaluation/to-tf.d.ts +12 -0
  87. package/dist-sdk/src/entities/agent-evaluation/to-tf.js +35 -0
  88. package/dist-sdk/src/entities/agent-tool/from-tf.d.ts +8 -7
  89. package/dist-sdk/src/entities/agent-tool/from-tf.js +48 -6
  90. package/dist-sdk/src/entities/agent-tool/to-tf.d.ts +10 -2
  91. package/dist-sdk/src/entities/agent-tool/to-tf.js +76 -28
  92. package/dist-sdk/src/entities/agent-tool/types.d.ts +0 -1
  93. package/dist-sdk/src/entities/agent-tool/types.js +10 -37
  94. package/dist-sdk/src/entities/app/build.d.ts +97 -42
  95. package/dist-sdk/src/entities/app/build.js +25 -14
  96. package/dist-sdk/src/entities/app/deserializer/normalize.d.ts +32 -24
  97. package/dist-sdk/src/entities/app/deserializer/pulled-expression.d.ts +1 -1
  98. package/dist-sdk/src/entities/app/deserializer/pulled-expression.js +2 -0
  99. package/dist-sdk/src/entities/app/index.d.ts +11 -8
  100. package/dist-sdk/src/entities/app/index.js +10 -7
  101. package/dist-sdk/src/entities/app/module.js +2 -2
  102. package/dist-sdk/src/entities/app/to-tf.d.ts +1 -1
  103. package/dist-sdk/src/entities/app/to-tf.js +22 -6
  104. package/dist-sdk/src/entities/approval-chain/build.d.ts +1 -1
  105. package/dist-sdk/src/entities/approval-chain/build.js +5 -5
  106. package/dist-sdk/src/entities/approval-chain/index.d.ts +1 -1
  107. package/dist-sdk/src/entities/approval-chain/index.js +1 -1
  108. package/dist-sdk/src/entities/approval-chain/printer.js +2 -2
  109. package/dist-sdk/src/entities/automation/build.d.ts +172 -94
  110. package/dist-sdk/src/entities/automation/build.js +159 -68
  111. package/dist-sdk/src/entities/automation/calculation-expression.d.ts +191 -0
  112. package/dist-sdk/src/entities/automation/calculation-expression.js +57 -0
  113. package/dist-sdk/src/entities/automation/deserializer/ast.d.ts +11 -4
  114. package/dist-sdk/src/entities/automation/deserializer/print-automation.d.ts +18 -14
  115. package/dist-sdk/src/entities/automation/deserializer/print-automation.js +164 -198
  116. package/dist-sdk/src/entities/automation/from-tf.d.ts +26 -2
  117. package/dist-sdk/src/entities/automation/from-tf.js +510 -80
  118. package/dist-sdk/src/entities/automation/index.d.ts +9 -12
  119. package/dist-sdk/src/entities/automation/index.js +11 -13
  120. package/dist-sdk/src/entities/automation/module.js +74 -6
  121. package/dist-sdk/src/entities/automation/operators.d.ts +32 -48
  122. package/dist-sdk/src/entities/automation/operators.js +10 -37
  123. package/dist-sdk/src/entities/automation/output-key-wire-name.d.ts +1 -1
  124. package/dist-sdk/src/entities/automation/output-key-wire-name.js +1 -1
  125. package/dist-sdk/src/entities/automation/script-code.js +13 -22
  126. package/dist-sdk/src/entities/automation/serialize.js +1 -1
  127. package/dist-sdk/src/entities/automation/tasks.d.ts +153 -89
  128. package/dist-sdk/src/entities/automation/tasks.js +64 -11
  129. package/dist-sdk/src/entities/automation/to-tf.d.ts +10 -1
  130. package/dist-sdk/src/entities/automation/to-tf.js +342 -71
  131. package/dist-sdk/src/entities/automation/triggers.d.ts +17 -10
  132. package/dist-sdk/src/entities/automation/triggers.js +7 -1
  133. package/dist-sdk/src/entities/automation/types.d.ts +3 -2
  134. package/dist-sdk/src/entities/automation/types.js +3 -2
  135. package/dist-sdk/src/entities/chart/build.d.ts +95 -0
  136. package/dist-sdk/src/entities/chart/build.js +42 -0
  137. package/dist-sdk/src/entities/chart/from-tf.d.ts +55 -0
  138. package/dist-sdk/src/entities/chart/from-tf.js +269 -0
  139. package/dist-sdk/src/entities/chart/index.d.ts +4 -0
  140. package/dist-sdk/src/entities/chart/index.js +3 -0
  141. package/dist-sdk/src/entities/chart/module.d.ts +3 -0
  142. package/dist-sdk/src/entities/chart/module.js +61 -0
  143. package/dist-sdk/src/entities/chart/printer.d.ts +5 -0
  144. package/dist-sdk/src/entities/chart/printer.js +104 -0
  145. package/dist-sdk/src/entities/chart/to-tf.d.ts +15 -0
  146. package/dist-sdk/src/entities/chart/to-tf.js +150 -0
  147. package/dist-sdk/src/entities/chart/types.d.ts +53 -0
  148. package/dist-sdk/src/entities/chart/types.js +22 -0
  149. package/dist-sdk/src/entities/dashboard/build.d.ts +34 -7
  150. package/dist-sdk/src/entities/dashboard/build.js +16 -1
  151. package/dist-sdk/src/entities/dashboard/from-tf.js +22 -4
  152. package/dist-sdk/src/entities/dashboard/to-tf.d.ts +16 -3
  153. package/dist-sdk/src/entities/dashboard/to-tf.js +37 -10
  154. package/dist-sdk/src/entities/datamine/module.js +1 -1
  155. package/dist-sdk/src/entities/dynamic-layout/build.d.ts +4 -3
  156. package/dist-sdk/src/entities/dynamic-layout/build.js +1 -1
  157. package/dist-sdk/src/entities/dynamic-layout/dialect.d.ts +2 -0
  158. package/dist-sdk/src/entities/dynamic-layout/dialect.js +43 -5
  159. package/dist-sdk/src/entities/dynamic-layout/from-tf.js +12 -6
  160. package/dist-sdk/src/entities/dynamic-layout/to-tf.js +10 -10
  161. package/dist-sdk/src/entities/element/build.d.ts +25 -32
  162. package/dist-sdk/src/entities/element/build.js +15 -32
  163. package/dist-sdk/src/entities/element/index.d.ts +7 -5
  164. package/dist-sdk/src/entities/element/index.js +7 -4
  165. package/dist-sdk/src/entities/element/module.js +7 -3
  166. package/dist-sdk/src/entities/element/to-tf.d.ts +2 -1
  167. package/dist-sdk/src/entities/element/to-tf.js +7 -3
  168. package/dist-sdk/src/entities/field/build.d.ts +51 -24
  169. package/dist-sdk/src/entities/field/build.js +31 -6
  170. package/dist-sdk/src/entities/field/calc/aggregate.d.ts +52 -21
  171. package/dist-sdk/src/entities/field/calc/aggregate.js +59 -29
  172. package/dist-sdk/src/entities/field/calc/date.d.ts +78 -29
  173. package/dist-sdk/src/entities/field/calc/date.js +120 -29
  174. package/dist-sdk/src/entities/field/calc/index.d.ts +80 -85
  175. package/dist-sdk/src/entities/field/calc/index.js +82 -139
  176. package/dist-sdk/src/entities/field/calc/numeric.d.ts +8 -12
  177. package/dist-sdk/src/entities/field/calc/numeric.js +10 -27
  178. package/dist-sdk/src/entities/field/calc/runtime.d.ts +19 -2
  179. package/dist-sdk/src/entities/field/calc/runtime.js +79 -8
  180. package/dist-sdk/src/entities/field/calc/text.d.ts +32 -23
  181. package/dist-sdk/src/entities/field/calc/text.js +43 -31
  182. package/dist-sdk/src/entities/field/calc/types.d.ts +36 -81
  183. package/dist-sdk/src/entities/field/calc/types.js +1 -22
  184. package/dist-sdk/src/entities/field/calc.d.ts +71 -48
  185. package/dist-sdk/src/entities/field/calc.js +5 -15
  186. package/dist-sdk/src/entities/field/calculation-types.js +10 -3
  187. package/dist-sdk/src/entities/field/from-tf.js +156 -10
  188. package/dist-sdk/src/entities/field/locals.d.ts +11 -3
  189. package/dist-sdk/src/entities/field/locals.js +15 -2
  190. package/dist-sdk/src/entities/field/module.js +23 -6
  191. package/dist-sdk/src/entities/field/record-shape.d.ts +24 -10
  192. package/dist-sdk/src/entities/field/to-tf.d.ts +40 -1
  193. package/dist-sdk/src/entities/field/to-tf.js +293 -35
  194. package/dist-sdk/src/entities/field/types.d.ts +1 -1
  195. package/dist-sdk/src/entities/field/types.js +7 -1
  196. package/dist-sdk/src/entities/file-reader/build.d.ts +5 -0
  197. package/dist-sdk/src/entities/file-reader/deserializer/print-file-reader.js +1 -1
  198. package/dist-sdk/src/entities/org/cloud-link.js +4 -5
  199. package/dist-sdk/src/entities/org/email-alias.d.ts +2 -0
  200. package/dist-sdk/src/entities/org/email-alias.js +12 -0
  201. package/dist-sdk/src/entities/org/index.d.ts +1 -0
  202. package/dist-sdk/src/entities/org/index.js +1 -0
  203. package/dist-sdk/src/entities/org/phone-provider.js +4 -7
  204. package/dist-sdk/src/entities/organization/build.d.ts +214 -2
  205. package/dist-sdk/src/entities/organization/build.js +145 -4
  206. package/dist-sdk/src/entities/organization/index.d.ts +1 -1
  207. package/dist-sdk/src/entities/organization/index.js +1 -1
  208. package/dist-sdk/src/entities/organization/module.js +4 -1
  209. package/dist-sdk/src/entities/organization/to-tf.d.ts +1 -0
  210. package/dist-sdk/src/entities/organization/to-tf.js +118 -1
  211. package/dist-sdk/src/entities/phone-service/build.d.ts +1 -1
  212. package/dist-sdk/src/entities/phone-service/build.js +2 -2
  213. package/dist-sdk/src/entities/phone-service/deserializer/print-phone-service.js +5 -3
  214. package/dist-sdk/src/entities/phone-service/index.d.ts +1 -1
  215. package/dist-sdk/src/entities/phone-service/index.js +1 -1
  216. package/dist-sdk/src/entities/phone-service/types.js +1 -1
  217. package/dist-sdk/src/entities/record-create-form/to-tf.js +9 -1
  218. package/dist-sdk/src/entities/relationship/to-tf.d.ts +1 -0
  219. package/dist-sdk/src/entities/relationship/to-tf.js +5 -13
  220. package/dist-sdk/src/entities/search-table/build.d.ts +4 -4
  221. package/dist-sdk/src/entities/search-table/build.js +13 -13
  222. package/dist-sdk/src/entities/search-table/index.d.ts +2 -2
  223. package/dist-sdk/src/entities/search-table/index.js +2 -2
  224. package/dist-sdk/src/entities/search-table/printer.js +5 -5
  225. package/dist-sdk/src/entities/service-account/to-tf.d.ts +21 -0
  226. package/dist-sdk/src/entities/service-account/to-tf.js +32 -1
  227. package/dist-sdk/src/entities/shared/context.d.ts +25 -24
  228. package/dist-sdk/src/entities/shared/context.js +36 -35
  229. package/dist-sdk/src/entities/shared/filters/ast.d.ts +10 -3
  230. package/dist-sdk/src/entities/shared/filters/ast.js +1 -0
  231. package/dist-sdk/src/entities/shared/filters/index.d.ts +2 -1
  232. package/dist-sdk/src/entities/shared/filters/index.js +11 -4
  233. package/dist-sdk/src/entities/shared/foreign-ref.d.ts +14 -0
  234. package/dist-sdk/src/entities/shared/foreign-ref.js +12 -0
  235. package/dist-sdk/src/entities/shared/ref.d.ts +26 -11
  236. package/dist-sdk/src/entities/shared/ref.js +8 -3
  237. package/dist-sdk/src/entities/shared/types/common.d.ts +2 -2
  238. package/dist-sdk/src/entities/shared/types/field-types.d.ts +1 -1
  239. package/dist-sdk/src/entities/shared/types/field-types.js +1 -1
  240. package/dist-sdk/src/entities/skill/build.d.ts +2 -2
  241. package/dist-sdk/src/entities/skill/build.js +3 -3
  242. package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.d.ts +1 -1
  243. package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.js +26 -7
  244. package/dist-sdk/src/entities/skill/index.d.ts +1 -1
  245. package/dist-sdk/src/entities/skill/index.js +1 -1
  246. package/dist-sdk/src/entities/skill/module.js +2 -2
  247. package/dist-sdk/src/entities/skill/to-tf.d.ts +1 -1
  248. package/dist-sdk/src/entities/skill/to-tf.js +2 -2
  249. package/dist-sdk/src/entities/skill/tools.d.ts +4 -3
  250. package/dist-sdk/src/entities/skill/tools.js +4 -3
  251. package/dist-sdk/src/entities/skill-tool/from-tf.d.ts +13 -0
  252. package/dist-sdk/src/entities/skill-tool/from-tf.js +42 -0
  253. package/dist-sdk/src/entities/skill-tool/to-tf.js +6 -6
  254. package/dist-sdk/src/entities/table/build.d.ts +1 -1
  255. package/dist-sdk/src/entities/table/build.js +9 -9
  256. package/dist-sdk/src/entities/table/catalog-render.js +9 -1
  257. package/dist-sdk/src/entities/table/deserializer/normalize.js +4 -4
  258. package/dist-sdk/src/entities/table/fields/types.d.ts +1 -1
  259. package/dist-sdk/src/entities/table/fields/types.js +1 -1
  260. package/dist-sdk/src/entities/table/index.d.ts +2 -2
  261. package/dist-sdk/src/entities/table/index.js +3 -3
  262. package/dist-sdk/src/entities/table/module.js +1 -1
  263. package/dist-sdk/src/entities/table/to-tf.js +22 -30
  264. package/dist-sdk/src/entities/task/build.d.ts +1 -1
  265. package/dist-sdk/src/entities/task/build.js +5 -5
  266. package/dist-sdk/src/entities/task/index.d.ts +1 -1
  267. package/dist-sdk/src/entities/task/index.js +1 -1
  268. package/dist-sdk/src/entities/view/build.d.ts +46 -0
  269. package/dist-sdk/src/entities/view/build.js +19 -0
  270. package/dist-sdk/src/entities/view/from-tf.js +68 -18
  271. package/dist-sdk/src/entities/view/module.d.ts +6 -13
  272. package/dist-sdk/src/entities/view/module.js +7 -14
  273. package/dist-sdk/src/entities/view/sharing.d.ts +10 -0
  274. package/dist-sdk/src/entities/view/sharing.js +6 -0
  275. package/dist-sdk/src/entities/view/to-tf.d.ts +21 -19
  276. package/dist-sdk/src/entities/view/to-tf.js +61 -26
  277. package/dist-sdk/src/entities/view/types.js +3 -0
  278. package/dist-sdk/src/entities/visual-flow/build.d.ts +1 -1
  279. package/dist-sdk/src/entities/visual-flow/build.js +8 -8
  280. package/dist-sdk/src/entities/visual-flow/index.d.ts +1 -1
  281. package/dist-sdk/src/entities/visual-flow/index.js +1 -1
  282. package/dist-sdk/src/entities/visual-flow/module.js +1 -1
  283. package/dist-sdk/src/entities/visual-flow/printer.js +3 -3
  284. package/dist-sdk/src/entities/visual-flow/to-tf.d.ts +1 -0
  285. package/dist-sdk/src/entities/visual-flow/to-tf.js +3 -7
  286. package/dist-sdk/src/entities/widget/to-tf.d.ts +1 -0
  287. package/dist-sdk/src/entities/widget/to-tf.js +9 -7
  288. package/dist-sdk/src/graph/index.d.ts +5 -1
  289. package/dist-sdk/src/graph/index.js +4 -1
  290. package/dist-sdk/src/graph/modules.js +6 -1
  291. package/dist-sdk/src/graph/types.d.ts +52 -1
  292. package/dist-sdk/src/internal/safe-identifier.d.ts +4 -0
  293. package/dist-sdk/src/internal/safe-identifier.js +13 -0
  294. package/dist-sdk/src/internal/sdk-package.d.ts +1 -1
  295. package/dist-sdk/src/internal/sdk-package.js +1 -0
  296. package/package.json +12 -3
@@ -1,13 +1,16 @@
1
- import { EntityTfError } from "../../engine/entity.js";
1
+ import { EntityTfError, recordFieldContextOf } from "../../engine/entity.js";
2
2
  import { DEPLOY_DEFAULT_SYSTEM_FIELD_NAMES, SYSTEM_FIELD_KEY_SET } from "../../core/system-fields.js";
3
3
  import { appResourceLabelPrefix, appScopedResourceLabel, expr, isTfExpr, isUuid, sanitizeName, scopedResourceLabel, snakeSlug, tsLabelToTfLabel, } from "../../engine/tf/index.js";
4
4
  import { workspaceAspectKey } from "../../engine/build/context.js";
5
5
  import { SEARCH_TABLE_RESOURCE_TYPES } from "../search-table/types.js";
6
6
  import { fieldRefToTfValue } from "../field/to-tf.js";
7
7
  import { SERVICE_ACCOUNT_RESOURCE_TYPE } from "../service-account/module.js";
8
+ import { resolveForeignRef } from "../shared/foreign-ref.js";
8
9
  import { filterToTf as serializeFilterToTf } from "../shared/filters/tf-codec.js";
9
10
  import { CONNECTOR_ORG_BAG } from "../org/connector.js";
10
11
  import { FILE_READER_SUB_TYPE_BY_RESOURCE, FILE_READER_SUB_TYPE_BY_TYPE_KEY } from "../file-reader/types.js";
12
+ import { UNRESOLVED_FIELD_MARKER } from "../shared/ref.js";
13
+ import { TABLE_RESOURCE_TYPE } from "../table/module.js";
11
14
  import { AUTOMATION_RESOURCE_TYPE, WORKFLOW_PUBLISH_RESOURCE_TYPE } from "./types.js";
12
15
  function scopedLabel(appTfLabel, label) {
13
16
  return appScopedResourceLabel(appTfLabel, label);
@@ -199,7 +202,7 @@ function addressExpr(address, path = ["id"]) {
199
202
  * this automation belongs to (e.g. a `recordSearch` task's filter against
200
203
  * another app's field) — resolved via a `data.elementum_field` lookup keyed
201
204
  * by the owner's id + the field's display name, mirroring
202
- * `field/to-tf.ts`'s `resolveRelatedLabelField` for a relation field's
205
+ * `field/to-tf.ts`'s `resolveFieldOnObject` for a relation field's
203
206
  * `relatedLabelField`. Self-contained: `parentKind`/`parentName` (stamped by
204
207
  * `appCatalog`/`elementCatalog` — see `FieldRef`) carry everything needed to
205
208
  * register the owner's own `data.elementum_app`/`data.elementum_element`
@@ -209,6 +212,24 @@ function addressExpr(address, path = ["id"]) {
209
212
  * its own foreign-field label: an app and an element can share a refName,
210
213
  * and `elementum_field` is one shared data-source type for both.
211
214
  */
215
+ /** App/element root lookup attrs when orgRefs is unavailable (mirrors graph rootNamespace strategy). */
216
+ function foreignRootLookup(kind, identity) {
217
+ const resourceType = kind === "element" ? "elementum_element" : "elementum_app";
218
+ if (identity.namespace !== undefined && identity.namespace !== "") {
219
+ return {
220
+ resourceType,
221
+ tfLabel: sanitizeName(identity.namespace),
222
+ lookupAttr: "namespace",
223
+ lookupValue: identity.namespace,
224
+ };
225
+ }
226
+ return {
227
+ resourceType,
228
+ tfLabel: sanitizeName(identity.name),
229
+ lookupAttr: "name",
230
+ lookupValue: identity.name,
231
+ };
232
+ }
212
233
  /**
213
234
  * Validates and derives the shared naming for a cross-app/element field
214
235
  * token — the `data.elementum_field` label {@link crossAppFieldToTf} emits,
@@ -235,8 +256,13 @@ function foreignFieldNaming(value) {
235
256
  throw new EntityTfError(`field ref "${value.refName}" has a parent ("${parent}") but no parentName — incomplete foreign-field provenance; the owner lookup would key on the refName instead of its real display name and silently miss at apply`);
236
257
  }
237
258
  const fieldName = value.name ?? value.refName;
238
- const ownerType = value.parentKind === "element" ? "elementum_element" : "elementum_app";
239
- const ownerLabel = tsLabelToTfLabel(parent);
259
+ const ownerKind = value.parentKind === "element" ? "element" : "app";
260
+ const resolved = foreignRootLookup(ownerKind, {
261
+ name: value.parentName,
262
+ namespace: value.parentNamespace,
263
+ });
264
+ const ownerType = resolved.resourceType;
265
+ const ownerLabel = resolved.tfLabel;
240
266
  const fieldLabel = `${value.parentKind}_${ownerLabel}_${tsLabelToTfLabel(value.refName)}`;
241
267
  return { fieldName, ownerType, ownerLabel, ownerParentName: value.parentName, fieldLabel };
242
268
  }
@@ -279,31 +305,23 @@ function crossAppFieldToTf(value, ctx, dataBlocks) {
279
305
  return fieldValue;
280
306
  }
281
307
  }
282
- const { fieldName, ownerType, ownerLabel, ownerParentName, fieldLabel } = foreignFieldNaming(value);
283
- const objectId = expr.dataAttr(ownerType, ownerLabel, ["id"]);
284
- // Route through the build-wide `orgRefs` registry (same one `agent-tool`/
285
- // `agent`/`skill`/`field` use) instead of this call's own local
286
- // `dataBlocks` map, so two DIFFERENT automation files referencing the same
287
- // foreign app/element/field collapse to one `data.tf` entry instead of
288
- // each emitting its own identically-addressed `data` block into its own
289
- // per-automation `.tf` file — a duplicate resource address OpenTofu
290
- // rejects at `validate`/`plan`. `dataBlocks` remains the fallback for
291
- // callers that don't supply `orgRefs` (unit tests).
308
+ const { fieldName, fieldLabel } = foreignFieldNaming(value);
309
+ const ownerKind = value.parentKind === "element" ? "element" : "app";
292
310
  if (ctx.orgRefs !== undefined) {
293
- if (value.parentKind === "element") {
294
- ctx.orgRefs.foreignElement(ownerLabel, ownerParentName);
295
- }
296
- else {
297
- ctx.orgRefs.foreignApp(ownerLabel, ownerParentName);
298
- }
299
- ctx.orgRefs.foreignField("elementum_field", fieldLabel, objectId, fieldName);
311
+ const owner = resolveForeignRef(ownerKind, { name: value.parentName, namespace: value.parentNamespace }, ctx.orgRefs, ctx.diagnostics, `automation field "${value.refName}" owner`);
312
+ ctx.orgRefs.foreignField("elementum_field", fieldLabel, owner.value, fieldName);
300
313
  }
301
314
  else {
302
- dataBlocks.set(`${ownerType}.${ownerLabel}`, {
315
+ const resolved = foreignRootLookup(ownerKind, {
316
+ name: value.parentName,
317
+ namespace: value.parentNamespace,
318
+ });
319
+ const objectId = expr.dataAttr(resolved.resourceType, resolved.tfLabel, ["id"]);
320
+ dataBlocks.set(`${resolved.resourceType}.${resolved.tfLabel}`, {
303
321
  kind: "data",
304
- type: ownerType,
305
- label: ownerLabel,
306
- attrs: { name: ownerParentName },
322
+ type: resolved.resourceType,
323
+ label: resolved.tfLabel,
324
+ attrs: { [resolved.lookupAttr]: resolved.lookupValue },
307
325
  });
308
326
  dataBlocks.set(`elementum_field.${fieldLabel}`, {
309
327
  kind: "data",
@@ -348,29 +366,18 @@ function tokenToTf(value, ctx, dataBlocks) {
348
366
  }
349
367
  if (!value.name)
350
368
  throw new EntityTfError(`cross-aspect ref "${value.refName}" has no lookup name`);
351
- const type = value.__kind === "element" ? "elementum_element" : "elementum_app";
352
- const label = tsLabelToTfLabel(value.refName);
353
- // See `crossAppFieldToTf`'s comment: route through the build-wide
354
- // `orgRefs` registry so multiple automations referencing the same
355
- // foreign app/element collapse to one `data.tf` entry instead of
356
- // each stamping a duplicate `data` block into its own file.
369
+ const kind = value.__kind === "element" ? "element" : "app";
357
370
  if (ctx.orgRefs !== undefined) {
358
- if (value.__kind === "element") {
359
- ctx.orgRefs.foreignElement(label, value.name);
360
- }
361
- else {
362
- ctx.orgRefs.foreignApp(label, value.name);
363
- }
364
- }
365
- else {
366
- dataBlocks.set(`${type}.${label}`, {
367
- kind: "data",
368
- type,
369
- label,
370
- attrs: { name: value.name },
371
- });
371
+ return resolveForeignRef(kind, { name: value.name, namespace: value.namespace }, ctx.orgRefs, ctx.diagnostics, `automation ${value.refName}`).value;
372
372
  }
373
- return expr.dataAttr(type, label, ["id"]);
373
+ const resolved = foreignRootLookup(kind, { name: value.name, namespace: value.namespace });
374
+ dataBlocks.set(`${resolved.resourceType}.${resolved.tfLabel}`, {
375
+ kind: "data",
376
+ type: resolved.resourceType,
377
+ label: resolved.tfLabel,
378
+ attrs: { [resolved.lookupAttr]: resolved.lookupValue },
379
+ });
380
+ return expr.dataAttr(resolved.resourceType, resolved.tfLabel, ["id"]);
374
381
  }
375
382
  return expr.resourceAttr("elementum_app", ctx.appTfLabel, ["id"]);
376
383
  case "field": {
@@ -424,6 +431,28 @@ function tokenToTf(value, ctx, dataBlocks) {
424
431
  throw new EntityTfError(`datamine ref "${value.refName}" is missing its owning table`);
425
432
  }
426
433
  return expr.resourceAttr("elementum_datamine", `${tsLabelToTfLabel(value.parent)}_${tsLabelToTfLabel(value.refName)}`, ["id"]);
434
+ case "table": {
435
+ const sibling = ctx.workspaceAspects?.get(workspaceAspectKey("table", value.refName));
436
+ if (sibling !== undefined) {
437
+ return expr.resourceAttr(TABLE_RESOURCE_TYPE, sibling.tfLabel, ["id"]);
438
+ }
439
+ const name = value.name ?? value.refName;
440
+ if (name === "")
441
+ throw new EntityTfError(`table ref "${value.refName}" has no lookup name`);
442
+ const label = tsLabelToTfLabel(value.refName);
443
+ if (ctx.orgRefs !== undefined) {
444
+ ctx.orgRefs.rawLookup(TABLE_RESOURCE_TYPE, label, name);
445
+ }
446
+ else {
447
+ dataBlocks.set(`${TABLE_RESOURCE_TYPE}.${label}`, {
448
+ kind: "data",
449
+ type: TABLE_RESOURCE_TYPE,
450
+ label,
451
+ attrs: { name },
452
+ });
453
+ }
454
+ return expr.dataAttr(TABLE_RESOURCE_TYPE, label, ["id"]);
455
+ }
427
456
  case "searchTable": {
428
457
  // Ownership + variant contract:
429
458
  // snowflake / linked -> owned by an ELEMENT
@@ -460,7 +489,7 @@ function tokenToTf(value, ctx, dataBlocks) {
460
489
  if (variant === "table") {
461
490
  if (table === undefined) {
462
491
  throw new EntityTfError(`search table "${owner}.${value.refName}" is table-owned, but "${owner}" is not a table in this build` +
463
- `${element !== undefined ? ` (it is an element — use defineSearchTable, not defineTableSearchTable)` : ""}`);
492
+ `${element !== undefined ? ` (it is an element — use searchTable, not tableSearchTable)` : ""}`);
464
493
  }
465
494
  // Label shape mirrors search-table/to-tf.ts's own table branch.
466
495
  return expr.resourceAttr(SEARCH_TABLE_RESOURCE_TYPES.table, `tables_${table.tfLabel}_${tsLabelToTfLabel(value.refName)}`, ["id"]);
@@ -470,7 +499,7 @@ function tokenToTf(value, ctx, dataBlocks) {
470
499
  }
471
500
  if (table !== undefined) {
472
501
  throw new EntityTfError(`search table "${owner}.${value.refName}" is a ${variant} search table owned by table "${owner}" — ` +
473
- `a table's search table is created with defineTableSearchTable`);
502
+ `a table's search table is created with tableSearchTable`);
474
503
  }
475
504
  if (aspects.get(workspaceAspectKey("app", owner)) !== undefined) {
476
505
  throw new EntityTfError(`search table "${owner}.${value.refName}" is owned by app "${owner}" — a search table is owned by an ` +
@@ -529,6 +558,145 @@ function templateToTf(value, refs) {
529
558
  parts.push(template.slice(cursor));
530
559
  return expr.template(parts);
531
560
  }
561
+ /**
562
+ * Calculation `formula` is the calc-engine language, not a task-input
563
+ * ValueReference. A bare `trigger.refs["Field"]` interpolates to a JSON
564
+ * envelope; the API rejects that with `Unable to validate` (edk#107, live
565
+ * apply 2026-08-21 on portertest). ei export and the accepted write shape
566
+ * are both `{{trigger name="record.<field-id>"}}`.
567
+ */
568
+ function calculationFormulaToTf(template, refs) {
569
+ const parts = flattenAdjacentFormulaParts(calculationTemplateToParts(template, refs));
570
+ if (parts.length === 0)
571
+ return "";
572
+ if (parts.length === 1 && typeof parts[0] === "string")
573
+ return parts[0];
574
+ return expr.template(parts);
575
+ }
576
+ function calculationTemplateToParts(value, refs) {
577
+ const template = value.template;
578
+ const parameters = value.parameters;
579
+ if (typeof template !== "string" || !Array.isArray(parameters)) {
580
+ throw new EntityTfError("invalid calculation templateReference");
581
+ }
582
+ const byName = new Map();
583
+ for (const parameter of parameters) {
584
+ if (!isObject(parameter) || !isObject(parameter.value) || typeof parameter.value.name !== "string") {
585
+ throw new EntityTfError("nested/list calculation template parameters are not supported by the Terraform contract");
586
+ }
587
+ byName.set(parameter.value.name, calculationFormulaRefToParts(parameter.value.value, refs));
588
+ }
589
+ const parts = [];
590
+ let cursor = 0;
591
+ const pattern = /\{\{\{([^}]+)\}\}\}/g;
592
+ for (let match = pattern.exec(template); match !== null; match = pattern.exec(template)) {
593
+ if (match.index > cursor)
594
+ parts.push(template.slice(cursor, match.index));
595
+ const paramParts = byName.get(match[1]);
596
+ if (paramParts === undefined) {
597
+ throw new EntityTfError(`calculation template parameter "${match[1]}" is missing`);
598
+ }
599
+ parts.push(...paramParts);
600
+ cursor = match.index + match[0].length;
601
+ }
602
+ if (cursor < template.length)
603
+ parts.push(template.slice(cursor));
604
+ return parts;
605
+ }
606
+ function calculationFormulaRefToParts(value, refs) {
607
+ if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
608
+ return [String(value)];
609
+ }
610
+ if (!isObject(value))
611
+ throw new EntityTfError(`unsupported calculation formula value ${String(value)}`);
612
+ if ("value" in value &&
613
+ !("triggerReference" in value) &&
614
+ !("taskReference" in value) &&
615
+ !("variableReference" in value) &&
616
+ !("templateReference" in value) &&
617
+ Object.keys(value).length === 1) {
618
+ return calculationFormulaRefToParts(value.value, refs);
619
+ }
620
+ if (isObject(value.triggerReference) && typeof value.triggerReference.name === "string") {
621
+ return calculationTriggerMustache(value.triggerReference.name, refs);
622
+ }
623
+ if (isObject(value.taskReference) &&
624
+ typeof value.taskReference.taskRefKey === "string" &&
625
+ typeof value.taskReference.name === "string") {
626
+ return calculationTaskMustache(value.taskReference.taskRefKey, value.taskReference.name, refs);
627
+ }
628
+ if (isObject(value.variableReference) && typeof value.variableReference.id === "string") {
629
+ return [`variable.${value.variableReference.id}`];
630
+ }
631
+ if (isObject(value.templateReference))
632
+ return calculationTemplateToParts(value.templateReference, refs);
633
+ if (typeof value.kind === "string") {
634
+ if (value.kind === "unresolved" && typeof value.uuid === "string") {
635
+ return [`{{trigger name="${value.uuid}"}}`];
636
+ }
637
+ if (value.kind === "literal" && "value" in value)
638
+ return calculationFormulaRefToParts(value.value, refs);
639
+ }
640
+ throw new EntityTfError("calculation formula is not a trigger/task/variable ref, literal, or template");
641
+ }
642
+ /**
643
+ * Calc-engine mustache for a prior task output. Matches the live dialect
644
+ * `{{task name="<wire output>" id="<task-id>"}}` (name is the refs-map key,
645
+ * not the task display name). Task id interpolates from the task resource.
646
+ */
647
+ function calculationTaskMustache(taskRefKey, wireName, refs) {
648
+ const target = refs.tasks.get(taskRefKey);
649
+ if (target === undefined)
650
+ throw new EntityTfError(`unresolved task ref "${taskRefKey}" in calculation formula`);
651
+ if (wireName.includes('"')) {
652
+ throw new EntityTfError(`calculation task output name "${wireName}" contains a double quote`);
653
+ }
654
+ return [
655
+ `{{task name="${wireName}" id="`,
656
+ expr.resourceAttr(target.type, target.label, ["id"]),
657
+ `"}}`,
658
+ ];
659
+ }
660
+ function calculationTriggerMustache(name, refs) {
661
+ const parts = name.split(".");
662
+ if (parts[0] !== "record" || parts.length < 2) {
663
+ return [`{{trigger name="${name}"}}`];
664
+ }
665
+ const fieldKey = parts[1];
666
+ const suffix = parts.slice(2).join(".");
667
+ const suffixPart = suffix.length > 0 ? `.${suffix}` : "";
668
+ return [
669
+ '{{trigger name="record.',
670
+ calculationRecordFieldId(fieldKey, refs),
671
+ `${suffixPart}"}}`,
672
+ ];
673
+ }
674
+ function calculationRecordFieldId(fieldKey, refs) {
675
+ if (fieldKey.startsWith(UNRESOLVED_FIELD_MARKER))
676
+ return fieldKey.slice(UNRESOLVED_FIELD_MARKER.length);
677
+ if (isUuid(fieldKey))
678
+ return fieldKey;
679
+ const address = refs.owner.fieldAddresses.get(fieldKey);
680
+ if (address !== undefined)
681
+ return addressExpr(address, ["id"]);
682
+ if (SYSTEM_FIELD_KEY_SET.has(fieldKey)) {
683
+ return expr.local(`${refs.owner.appTfLabel}_${tsLabelToTfLabel(fieldKey)}_field_id`);
684
+ }
685
+ throw new EntityTfError(`automation calculation targets field "${fieldKey}" which does not exist in app "${refs.owner.appRefName}"`);
686
+ }
687
+ function flattenAdjacentFormulaParts(parts) {
688
+ const out = [];
689
+ for (const part of parts) {
690
+ const last = out[out.length - 1];
691
+ if (typeof part === "string" && typeof last === "string") {
692
+ out[out.length - 1] = last + part;
693
+ }
694
+ else {
695
+ out.push(part);
696
+ }
697
+ }
698
+ return out;
699
+ }
532
700
  const TS_REF_SEGMENT_TO_WIRE = {
533
701
  record: "Record",
534
702
  changedFieldIds: "Changed Fields",
@@ -538,6 +706,15 @@ const TS_REF_SEGMENT_TO_WIRE = {
538
706
  title: "Title",
539
707
  };
540
708
  const TS_TRIGGER_REF_TO_WIRE = {
709
+ attachment: "Attachment",
710
+ // Full-name entry required: the live attachment-trigger ATTACHMENT_ID key is
711
+ // "Attachment Id" (triggerReference.name = "attachment.id"). Do NOT fold this
712
+ // onto FIELD_REF_SUFFIX_TO_WIRE — that table's `id` suffix is " ID", which
713
+ // would emit the wrong wire key "Attachment ID" and fail live validation.
714
+ "attachment.id": "Attachment Id",
715
+ "triggeredBy.id": "Triggered By's Id",
716
+ "triggeredBy.name": "Triggered By's Name",
717
+ "triggeredBy.email": "Triggered By's Email",
541
718
  createdByEmail: "Created by Email",
542
719
  updatedByManagerEmail: "Updated by Manager Email",
543
720
  slackThreadId: "Slack Thread ID",
@@ -616,6 +793,51 @@ function wireRefName(name, ctx) {
616
793
  parts[0] = TS_REF_SEGMENT_TO_WIRE[parts[0]] ?? parts[0];
617
794
  return parts.join(".");
618
795
  }
796
+ function forEachWireRefName(name, ctx) {
797
+ // A recordSearch collection's whole item and branded record identity both
798
+ // point at the loop's ASPECT_RECORD_ID reference. Live provider state
799
+ // exposes that reference under "Record"; "ID" is a distinct TEXT handle.
800
+ if (name === "item" || name === "recordId")
801
+ return "Record";
802
+ // Record fields are keyed by their platform display names, not their typed
803
+ // catalog handles. Reuse the same field/suffix mapping as trigger record
804
+ // paths, while preserving the generic forEach keys used by non-record
805
+ // collections (File, Iteration, etc.).
806
+ const fieldKey = name.split(".")[0];
807
+ if (fieldKey !== undefined &&
808
+ (ctx.fieldNames.has(fieldKey) || SYSTEM_FIELD_KEY_SET.has(fieldKey))) {
809
+ return wireRefName(`record.${name}`, ctx);
810
+ }
811
+ return wireRefName(name, ctx);
812
+ }
813
+ /**
814
+ * Authoring `records[N].<field>` accumulates as `records.N.<field>`. The
815
+ * platform's search-task refs map is flat display-name keys (same as
816
+ * `.record.<field>`), not dotted paths under "All found records" (#490).
817
+ * Collapse the indexed form onto the record-field path before wire lookup.
818
+ */
819
+ function normalizeIndexedRecordsFieldPath(name) {
820
+ const match = /^records\.\d+\.(.+)$/.exec(name);
821
+ return match !== null ? `record.${match[1]}` : name;
822
+ }
823
+ /**
824
+ * Resolve a task output ref name against the task's record-bearing target
825
+ * app/element when known (cross-app recordSearch / createRecord / …), falling
826
+ * back to the automation owner for non-record tasks and same-app searches.
827
+ */
828
+ function taskWireRefName(name, taskRefKey, refs) {
829
+ const normalized = normalizeIndexedRecordsFieldPath(name);
830
+ const taskApp = refs.recordFieldContext?.taskAppRefNameByRefKey?.[taskRefKey];
831
+ if (taskApp === undefined || taskApp === refs.owner.appRefName) {
832
+ return wireRefName(normalized, refs.owner);
833
+ }
834
+ const foreignNames = refs.fieldNamesByAppRefName?.get(taskApp);
835
+ return wireRefName(normalized, {
836
+ ...refs.owner,
837
+ appRefName: taskApp,
838
+ fieldNames: foreignNames ?? new Map(),
839
+ });
840
+ }
619
841
  function valueToTf(value, refs) {
620
842
  const catalog = token(value);
621
843
  if (catalog !== undefined)
@@ -626,8 +848,10 @@ function valueToTf(value, refs) {
626
848
  return value.map((item) => valueToTf(item, refs));
627
849
  if (!isObject(value))
628
850
  throw new EntityTfError(`unsupported automation value ${String(value)}`);
629
- if (value.__unsafe === "platformId" && typeof value.value === "string")
851
+ if (value.__unsafe === "platformId" && typeof value.value === "string") {
852
+ refs.owner.diagnostics.push(`automation raw UUID fallback ${JSON.stringify(value.value)} is source-org-only and non-portable`);
630
853
  return value.value;
854
+ }
631
855
  if ("value" in value && Object.keys(value).length === 1)
632
856
  return valueToTf(value.value, refs);
633
857
  if (isObject(value.variableReference) && typeof value.variableReference.id === "string") {
@@ -653,14 +877,24 @@ function valueToTf(value, refs) {
653
877
  // resources' actual refs map - confirmed live via a real apply's
654
878
  // "Invalid index" error. Their field names pass through verbatim.
655
879
  const isFileReaderTask = target.type === "elementum_ai_file_read_task" || target.type === "elementum_file_reader_task";
656
- const refKey = isFileReaderTask ? value.taskReference.name : wireRefName(value.taskReference.name, refs.owner);
880
+ // aiSearchTable's collection is `refs["All Records"]`, not recordSearch's
881
+ // globally-mapped `refs["All found records"]` (TS_REF_SEGMENT_TO_WIRE).
882
+ // Accept both the authoring segment and the already-rewritten wire name
883
+ // from rewriteForwardTaskRefName so either path emits the same key.
884
+ const isAiSearchTableRecords = target.type === "elementum_ai_search_table_task" &&
885
+ (value.taskReference.name === "records" || value.taskReference.name === "All Records");
886
+ const refKey = isFileReaderTask
887
+ ? value.taskReference.name
888
+ : isAiSearchTableRecords
889
+ ? "All Records"
890
+ : taskWireRefName(value.taskReference.name, value.taskReference.taskRefKey, refs);
657
891
  return expr.resourceAttr(target.type, target.label, ["refs", { index: refKey }]);
658
892
  }
659
893
  if (isObject(value.forEachReference) && typeof value.forEachReference.forEachTaskRefKey === "string" && typeof value.forEachReference.name === "string") {
660
894
  const target = refs.tasks.get(value.forEachReference.forEachTaskRefKey);
661
895
  if (target === undefined)
662
896
  throw new EntityTfError(`unresolved forEach ref "${value.forEachReference.forEachTaskRefKey}"`);
663
- return expr.resourceAttr(target.type, target.label, ["refs", { index: wireRefName(value.forEachReference.name, refs.owner) }]);
897
+ return expr.resourceAttr(target.type, target.label, ["inner", "refs", { index: forEachWireRefName(value.forEachReference.name, refs.owner) }]);
664
898
  }
665
899
  if (isObject(value.templateReference))
666
900
  return templateToTf(value.templateReference, refs);
@@ -771,7 +1005,7 @@ function filterReferenceToTf(value, refs) {
771
1005
  const task = refs.tasks.get(value.taskRefKey);
772
1006
  if (task === undefined)
773
1007
  throw new EntityTfError(`unresolved task filter ref "${value.taskRefKey}"`);
774
- return expr.resourceAttr(task.type, task.label, ["refs", { index: value.name }]);
1008
+ return expr.resourceAttr(task.type, task.label, ["refs", { index: taskWireRefName(value.name, value.taskRefKey, refs) }]);
775
1009
  }
776
1010
  case "FOR_EACH": {
777
1011
  if (typeof value.forEachTaskRefKey !== "string" || typeof value.name !== "string")
@@ -779,7 +1013,7 @@ function filterReferenceToTf(value, refs) {
779
1013
  const task = refs.tasks.get(value.forEachTaskRefKey);
780
1014
  if (task === undefined)
781
1015
  throw new EntityTfError(`unresolved forEach filter ref "${value.forEachTaskRefKey}"`);
782
- return expr.resourceAttr(task.type, task.label, ["refs", { index: value.name }]);
1016
+ return expr.resourceAttr(task.type, task.label, ["inner", "refs", { index: forEachWireRefName(value.name, refs.owner) }]);
783
1017
  }
784
1018
  case "VALUE":
785
1019
  return valueToTf(value.value, refs);
@@ -793,6 +1027,11 @@ function picklistFilterValueToTf(value, field, refs) {
793
1027
  field.fieldType !== "PICKLIST" ||
794
1028
  field.options === undefined)
795
1029
  return undefined;
1030
+ // An empty value is the platform's explicit "clear this field" payload,
1031
+ // not a picklist option label. Preserve it verbatim instead of trying to
1032
+ // resolve it through the option-id lookup.
1033
+ if (value === "")
1034
+ return undefined;
796
1035
  if (!field.options.includes(value)) {
797
1036
  // Status/dropdown options can be deleted and recreated (new ids)
798
1037
  // without the platform updating automations that already filtered on
@@ -980,20 +1219,39 @@ function configAttrs(kind, config, refs) {
980
1219
  const reference = item.calculationReference;
981
1220
  if (!isObject(reference.templateReference))
982
1221
  throw new EntityTfError("calculation expression is not a template reference");
983
- const template = reference.templateReference;
984
- const parameters = template.parameters;
985
- let formula;
986
- if (template.template === "{{{p1}}}" && Array.isArray(parameters) && parameters.length === 1) {
987
- const parameter = parameters[0];
988
- if (!isObject(parameter) || !isObject(parameter.value) || parameter.value.name !== "p1") {
989
- throw new EntityTfError("invalid calculation expression parameter");
1222
+ let includeType = "AUTO";
1223
+ let picklistFieldId;
1224
+ if (Array.isArray(item.includeTypes)) {
1225
+ let hasAuto = false;
1226
+ let hasManual = false;
1227
+ for (const include of item.includeTypes) {
1228
+ if (!isObject(include))
1229
+ continue;
1230
+ if (isObject(include.auto))
1231
+ hasAuto = true;
1232
+ if (isObject(include.manual))
1233
+ hasManual = true;
1234
+ if (isObject(include.picklist)) {
1235
+ includeType = "PICKLIST";
1236
+ if (include.picklist.picklistFieldId !== undefined) {
1237
+ picklistFieldId = valueToTf(include.picklist.picklistFieldId, refs);
1238
+ }
1239
+ }
1240
+ }
1241
+ if (includeType !== "PICKLIST") {
1242
+ includeType = hasAuto && hasManual ? "BOTH" : hasManual ? "MANUAL" : "AUTO";
990
1243
  }
991
- formula = valueToTf(parameter.value.value, refs);
992
- }
993
- else {
994
- formula = templateToTf(template, refs);
995
1244
  }
996
- return { variable_name: item.name, formula };
1245
+ return {
1246
+ ...(typeof item.id === "string" ? { id: item.id } : {}),
1247
+ variable_name: item.name,
1248
+ formula: calculationFormulaToTf(reference.templateReference, refs),
1249
+ ...(includeType !== "AUTO" ? { include_type: includeType } : {}),
1250
+ ...(picklistFieldId !== undefined ? { picklist_field_id: picklistFieldId } : {}),
1251
+ ...(item.recordId !== null && item.recordId !== undefined
1252
+ ? { record_id: valueToTf(item.recordId, refs) }
1253
+ : {}),
1254
+ };
997
1255
  });
998
1256
  continue;
999
1257
  }
@@ -1212,7 +1470,7 @@ function taskLabel(automationLabel, refKey) {
1212
1470
  const normalized = sanitizeName(refKey);
1213
1471
  return normalized.startsWith(`${automationLabel}_`) ? normalized : `${automationLabel}_${normalized}`;
1214
1472
  }
1215
- /** App-owned `defineAutomation` entity → provider HCL blocks. */
1473
+ /** App- or element-owned `automation` entity → provider HCL blocks. */
1216
1474
  export function automationToTf(entity, ctx) {
1217
1475
  const owner = {
1218
1476
  appRefName: ctx.appRefName ?? entity.parent,
@@ -1223,6 +1481,7 @@ export function automationToTf(entity, ctx) {
1223
1481
  orgRefs: ctx.orgRefs,
1224
1482
  localsAttrs: ctx.localsAttrs,
1225
1483
  workspaceAspects: ctx.workspaceAspects,
1484
+ diagnostics: ctx.diagnostics ?? [],
1226
1485
  };
1227
1486
  if (entity.kind !== "automation")
1228
1487
  throw new EntityTfError(`automationToTf expects kind "automation"; got "${entity.kind}"`);
@@ -1240,7 +1499,7 @@ export function automationToTf(entity, ctx) {
1240
1499
  type: AUTOMATION_RESOURCE_TYPE,
1241
1500
  label,
1242
1501
  attrs: {
1243
- app_id: expr.resourceAttr("elementum_app", ctx.appTfLabel, ["id"]),
1502
+ app_id: expr.resourceAttr(ctx.parentScope === "element" ? "elementum_element" : "elementum_app", ctx.appTfLabel, ["id"]),
1244
1503
  name: payload.name ?? "",
1245
1504
  ...(payload.description !== undefined ? { description: payload.description } : {}),
1246
1505
  ...(payload.triggersOtherAutomations !== undefined ? { triggers_other_automations: payload.triggersOtherAutomations } : {}),
@@ -1304,7 +1563,16 @@ export function automationToTf(entity, ctx) {
1304
1563
  };
1305
1564
  preRegister(payload.tasks ?? []);
1306
1565
  const dataBlocks = new Map();
1307
- const refs = { trigger: { type: triggerType, label }, tasks: taskRefs, owner, dataBlocks, variableById };
1566
+ const recordFieldContext = recordFieldContextOf(entity);
1567
+ const refs = {
1568
+ trigger: { type: triggerType, label },
1569
+ tasks: taskRefs,
1570
+ owner,
1571
+ dataBlocks,
1572
+ variableById,
1573
+ ...(recordFieldContext !== undefined ? { recordFieldContext } : {}),
1574
+ ...(ctx.fieldNamesByAppRefName !== undefined ? { fieldNamesByAppRefName: ctx.fieldNamesByAppRefName } : {}),
1575
+ };
1308
1576
  let triggerAttrs;
1309
1577
  try {
1310
1578
  triggerAttrs = configAttrs(triggerType, triggerConfig, refs);
@@ -1405,9 +1673,12 @@ export function automationToTf(entity, ctx) {
1405
1673
  const children = task.children ?? [];
1406
1674
  if (children.length !== 1)
1407
1675
  throw new EntityTfError(`forEach task "${refKey}" must have exactly one child body`);
1408
- const childResult = emitTasks(children[0].tasks ?? [], expr.resourceAttr(resourceType, tfLabel, ["inner"]));
1409
- if (childResult !== undefined)
1410
- parent = childResult.terminal;
1676
+ emitTasks(children[0].tasks ?? [], expr.resourceAttr(resourceType, tfLabel, ["inner"]));
1677
+ // The loop body is a separate operator-owned chain. A fluent sibling
1678
+ // authored after `forEach` continues from the for_each task itself,
1679
+ // not from the body's terminal task; the latter silently moves that
1680
+ // sibling into the loop and produces an invalid workflow graph.
1681
+ parent = address;
1411
1682
  }
1412
1683
  else if (wireType === "switch" || wireType === "forkJoin") {
1413
1684
  const children = task.children ?? [];
@@ -18,8 +18,8 @@ import { type TriggerChain } from "./build.js";
18
18
  /**
19
19
  * The record a record-trigger fires on. `A` is the app/element ref passed as
20
20
  * `app:` — when it's a real catalog ref (carries a `fields` bag), `record` is
21
- * typed per-field via `RecordShape`; otherwise it falls back to loose
22
- * `Record<string, unknown>` (see `RecordShape`'s doc comment).
21
+ * typed per-field via `RecordShape`; otherwise only the synthetic `id` and
22
+ * `recordId` keys are available (see `RecordShape`'s doc comment).
23
23
  */
24
24
  export interface RecordTriggerOutput<A extends AppOrElementRef = AppOrElementRef> {
25
25
  readonly record: RecordShape<A>;
@@ -99,22 +99,22 @@ export declare function webHook(opts?: WebhookTriggerOptions): TriggerChain<Webh
99
99
  /** @deprecated Use {@link webHook}. */
100
100
  export declare function webhook(opts?: WebhookTriggerOptions): TriggerChain<WebhookTriggerOutput>;
101
101
  export type TemporalUnit = "MINUTES" | "HOURS" | "DAYS";
102
- export interface TemporalTriggerOptions {
103
- readonly app: AppOrElementRef;
102
+ export interface TemporalTriggerOptions<A extends AppOrElementRef = AppOrElementRef> {
103
+ readonly app: A;
104
104
  readonly field: FieldRef;
105
105
  readonly temporalUnit: TemporalUnit;
106
106
  readonly value: number;
107
107
  }
108
108
  /** Fires relative to a date/datetime field on records in the given aspect. */
109
- export declare function temporal(opts: TemporalTriggerOptions): TriggerChain<RecordTriggerOutput>;
109
+ export declare function temporal<A extends AppOrElementRef>(opts: TemporalTriggerOptions<A>): TriggerChain<RecordTriggerOutput<A>>;
110
110
  export type approvalProcessStatusUpdatedStatus = "ACTIVE" | "STEP_APPROVED" | "CANCELLED" | "DENIED" | "APPROVED";
111
- export interface approvalProcessStatusUpdatedTriggerOptions {
112
- readonly app: AppOrElementRef;
111
+ export interface approvalProcessStatusUpdatedTriggerOptions<A extends AppOrElementRef = AppOrElementRef> {
112
+ readonly app: A;
113
113
  readonly approvalChain: string;
114
114
  readonly status: approvalProcessStatusUpdatedStatus;
115
115
  }
116
116
  /** Fires when an approval chain on a record changes to the configured status. */
117
- export declare function approvalProcessStatusUpdated(opts: approvalProcessStatusUpdatedTriggerOptions): TriggerChain<RecordTriggerOutput>;
117
+ export declare function approvalProcessStatusUpdated<A extends AppOrElementRef>(opts: approvalProcessStatusUpdatedTriggerOptions<A>): TriggerChain<RecordTriggerOutput<A>>;
118
118
  export interface AttachmentTriggerOptions<A extends AppOrElementRef = AppOrElementRef> {
119
119
  readonly app: A;
120
120
  readonly filter?: Filter;
@@ -122,8 +122,15 @@ export interface AttachmentTriggerOptions<A extends AppOrElementRef = AppOrEleme
122
122
  }
123
123
  export interface AttachmentTriggerOutput<A extends AppOrElementRef = AppOrElementRef> {
124
124
  readonly record: RecordShape<A>;
125
- /** Attachment sub-fields (name/url/mediaType/…) aren't typed yet — follow-up. */
126
- readonly attachment: Record<string, unknown>;
125
+ /**
126
+ * Trigger attachment payload. `id` is the live ATTACHMENT_ID wire key
127
+ * (`refs["Attachment Id"]`); remaining sub-fields (name/url/mediaType/…)
128
+ * aren't typed yet — follow-up.
129
+ */
130
+ readonly attachment: {
131
+ readonly id: string;
132
+ readonly [key: string]: unknown;
133
+ };
127
134
  }
128
135
  /** Fires when an attachment is added to a record on the given aspect. */
129
136
  export declare function attachment<A extends AppOrElementRef>(opts: AttachmentTriggerOptions<A>): TriggerChain<AttachmentTriggerOutput<A>>;