@elementumai/edk 0.9.7 → 0.9.9

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 (148) hide show
  1. package/README.md +3 -2
  2. package/dist-sdk/src/catalog/factories.d.ts +14 -3
  3. package/dist-sdk/src/catalog/factories.js +32 -0
  4. package/dist-sdk/src/catalog/from-authoring.js +27 -33
  5. package/dist-sdk/src/catalog/index.d.ts +2 -2
  6. package/dist-sdk/src/catalog/index.js +2 -2
  7. package/dist-sdk/src/catalog/org.js +67 -29
  8. package/dist-sdk/src/catalog/token.js +10 -0
  9. package/dist-sdk/src/catalog/types.d.ts +20 -2
  10. package/dist-sdk/src/catalog/types.js +5 -0
  11. package/dist-sdk/src/core/index.d.ts +1 -1
  12. package/dist-sdk/src/core/index.js +1 -1
  13. package/dist-sdk/src/core/platform-id.d.ts +2 -0
  14. package/dist-sdk/src/core/platform-id.js +4 -0
  15. package/dist-sdk/src/core/system-fields.d.ts +14 -0
  16. package/dist-sdk/src/core/system-fields.js +6 -0
  17. package/dist-sdk/src/core/triggers/on-demand.d.ts +27 -24
  18. package/dist-sdk/src/core/triggers/on-demand.js +33 -12
  19. package/dist-sdk/src/core/types/aspect-category.d.ts +1 -0
  20. package/dist-sdk/src/core/types/aspect-category.js +1 -0
  21. package/dist-sdk/src/engine/build/context.d.ts +20 -8
  22. package/dist-sdk/src/engine/build/source-context.d.ts +14 -17
  23. package/dist-sdk/src/engine/build/source-context.js +1 -1
  24. package/dist-sdk/src/engine/pull/context.d.ts +9 -0
  25. package/dist-sdk/src/engine/pull/org.d.ts +4 -0
  26. package/dist-sdk/src/engine/pull/org.js +17 -2
  27. package/dist-sdk/src/engine/pull/procedure-refs.d.ts +16 -0
  28. package/dist-sdk/src/engine/pull/procedure-refs.js +69 -0
  29. package/dist-sdk/src/engine/tf/expr.js +1 -1
  30. package/dist-sdk/src/engine/workspace/authoring-layout.d.ts +15 -0
  31. package/dist-sdk/src/engine/workspace/authoring-layout.js +33 -1
  32. package/dist-sdk/src/engine/workspace/loader.d.ts +40 -0
  33. package/dist-sdk/src/engine/workspace/loader.js +328 -0
  34. package/dist-sdk/src/entities/access-policy/from-tf.d.ts +4 -0
  35. package/dist-sdk/src/entities/access-policy/from-tf.js +10 -2
  36. package/dist-sdk/src/entities/access-policy/to-tf.d.ts +1 -0
  37. package/dist-sdk/src/entities/access-policy/to-tf.js +7 -2
  38. package/dist-sdk/src/entities/agent/build.d.ts +54 -8
  39. package/dist-sdk/src/entities/agent/build.js +36 -1
  40. package/dist-sdk/src/entities/agent/deserializer/print-agent-v2.js +126 -2
  41. package/dist-sdk/src/entities/agent/from-tf.d.ts +7 -6
  42. package/dist-sdk/src/entities/agent/from-tf.js +176 -5
  43. package/dist-sdk/src/entities/agent/index.d.ts +2 -2
  44. package/dist-sdk/src/entities/agent/index.js +1 -1
  45. package/dist-sdk/src/entities/agent/to-tf.d.ts +4 -4
  46. package/dist-sdk/src/entities/agent/to-tf.js +185 -11
  47. package/dist-sdk/src/entities/agent/tools.d.ts +16 -5
  48. package/dist-sdk/src/entities/agent/tools.js +13 -1
  49. package/dist-sdk/src/entities/agent-tool/from-tf.d.ts +7 -0
  50. package/dist-sdk/src/entities/agent-tool/from-tf.js +29 -4
  51. package/dist-sdk/src/entities/agent-tool/to-tf.js +10 -9
  52. package/dist-sdk/src/entities/agent-tool/types.js +1 -0
  53. package/dist-sdk/src/entities/app/build.d.ts +2 -2
  54. package/dist-sdk/src/entities/app/build.js +6 -3
  55. package/dist-sdk/src/entities/app/deserializer/normalize.d.ts +5 -0
  56. package/dist-sdk/src/entities/app/display-blocks-to-layout.d.ts +45 -0
  57. package/dist-sdk/src/entities/app/display-blocks-to-layout.js +98 -0
  58. package/dist-sdk/src/entities/app/display-blocks-to-record-create-form.d.ts +55 -0
  59. package/dist-sdk/src/entities/app/display-blocks-to-record-create-form.js +99 -0
  60. package/dist-sdk/src/entities/app/from-tf.js +9 -7
  61. package/dist-sdk/src/entities/app/to-tf.js +15 -7
  62. package/dist-sdk/src/entities/automation/build.d.ts +8 -1
  63. package/dist-sdk/src/entities/automation/build.js +36 -5
  64. package/dist-sdk/src/entities/automation/deserializer/ast.d.ts +3 -7
  65. package/dist-sdk/src/entities/automation/deserializer/print-automation.d.ts +2 -0
  66. package/dist-sdk/src/entities/automation/deserializer/print-automation.js +77 -8
  67. package/dist-sdk/src/entities/automation/from-tf.d.ts +10 -1
  68. package/dist-sdk/src/entities/automation/from-tf.js +803 -62
  69. package/dist-sdk/src/entities/automation/index.d.ts +1 -1
  70. package/dist-sdk/src/entities/automation/module.js +13 -3
  71. package/dist-sdk/src/entities/automation/output-key-wire-name.d.ts +12 -4
  72. package/dist-sdk/src/entities/automation/output-key-wire-name.js +17 -6
  73. package/dist-sdk/src/entities/automation/script-code.d.ts +1 -1
  74. package/dist-sdk/src/entities/automation/script-code.js +15 -11
  75. package/dist-sdk/src/entities/automation/task-label.d.ts +2 -0
  76. package/dist-sdk/src/entities/automation/task-label.js +18 -0
  77. package/dist-sdk/src/entities/automation/tasks.d.ts +60 -9
  78. package/dist-sdk/src/entities/automation/tasks.js +16 -4
  79. package/dist-sdk/src/entities/automation/to-tf.d.ts +4 -0
  80. package/dist-sdk/src/entities/automation/to-tf.js +385 -52
  81. package/dist-sdk/src/entities/automation/triggers.d.ts +14 -5
  82. package/dist-sdk/src/entities/automation/triggers.js +1 -1
  83. package/dist-sdk/src/entities/dashboard/from-tf.js +1 -1
  84. package/dist-sdk/src/entities/dynamic-layout/to-tf.d.ts +1 -0
  85. package/dist-sdk/src/entities/dynamic-layout/to-tf.js +1 -1
  86. package/dist-sdk/src/entities/element/build.d.ts +30 -7
  87. package/dist-sdk/src/entities/element/build.js +6 -3
  88. package/dist-sdk/src/entities/element/from-tf.js +7 -3
  89. package/dist-sdk/src/entities/element/index.d.ts +1 -1
  90. package/dist-sdk/src/entities/element/index.js +1 -1
  91. package/dist-sdk/src/entities/element/to-tf.js +49 -8
  92. package/dist-sdk/src/entities/field/from-tf.d.ts +6 -0
  93. package/dist-sdk/src/entities/field/from-tf.js +12 -0
  94. package/dist-sdk/src/entities/field/module.js +1 -0
  95. package/dist-sdk/src/entities/field/to-tf.d.ts +1 -1
  96. package/dist-sdk/src/entities/field/to-tf.js +19 -5
  97. package/dist-sdk/src/entities/org/category-ref.d.ts +5 -0
  98. package/dist-sdk/src/entities/org/category-ref.js +12 -0
  99. package/dist-sdk/src/entities/record-create-form/from-tf.js +3 -3
  100. package/dist-sdk/src/entities/record-create-form/to-tf.d.ts +1 -0
  101. package/dist-sdk/src/entities/record-create-form/to-tf.js +13 -5
  102. package/dist-sdk/src/entities/relationship/from-tf.js +15 -4
  103. package/dist-sdk/src/entities/role/from-tf.js +10 -4
  104. package/dist-sdk/src/entities/search-table/build.d.ts +1 -1
  105. package/dist-sdk/src/entities/search-table/from-tf.d.ts +1 -0
  106. package/dist-sdk/src/entities/search-table/from-tf.js +6 -0
  107. package/dist-sdk/src/entities/search-table/printer.js +3 -1
  108. package/dist-sdk/src/entities/search-table/to-tf.js +3 -3
  109. package/dist-sdk/src/entities/search-table/types.js +1 -0
  110. package/dist-sdk/src/entities/service-account/from-tf.js +10 -4
  111. package/dist-sdk/src/entities/service-account/to-tf.d.ts +2 -1
  112. package/dist-sdk/src/entities/service-account/to-tf.js +7 -0
  113. package/dist-sdk/src/entities/shared/filters/ast.d.ts +7 -1
  114. package/dist-sdk/src/entities/shared/filters/ast.js +3 -0
  115. package/dist-sdk/src/entities/shared/filters/tf-codec.d.ts +7 -1
  116. package/dist-sdk/src/entities/shared/filters/tf-codec.js +8 -4
  117. package/dist-sdk/src/entities/shared/handle-description.d.ts +12 -0
  118. package/dist-sdk/src/entities/shared/handle-description.js +20 -0
  119. package/dist-sdk/src/entities/shared/types/field-types.d.ts +6 -1
  120. package/dist-sdk/src/entities/shared/types/field-types.js +5 -8
  121. package/dist-sdk/src/entities/skill/build.d.ts +51 -2
  122. package/dist-sdk/src/entities/skill/build.js +65 -8
  123. package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.js +35 -3
  124. package/dist-sdk/src/entities/skill/from-tf.d.ts +11 -0
  125. package/dist-sdk/src/entities/skill/from-tf.js +39 -7
  126. package/dist-sdk/src/entities/skill/index.d.ts +1 -1
  127. package/dist-sdk/src/entities/skill/index.js +1 -1
  128. package/dist-sdk/src/entities/skill/input-coverage.d.ts +21 -0
  129. package/dist-sdk/src/entities/skill/input-coverage.js +148 -5
  130. package/dist-sdk/src/entities/skill/to-tf.d.ts +6 -25
  131. package/dist-sdk/src/entities/skill/to-tf.js +22 -38
  132. package/dist-sdk/src/entities/skill/tools.d.ts +2 -1
  133. package/dist-sdk/src/entities/skill/tools.js +3 -1
  134. package/dist-sdk/src/entities/skill-tool/from-tf.js +10 -3
  135. package/dist-sdk/src/entities/skill-tool/to-tf.d.ts +5 -5
  136. package/dist-sdk/src/entities/skill-tool/to-tf.js +5 -7
  137. package/dist-sdk/src/entities/table/to-tf.js +3 -4
  138. package/dist-sdk/src/entities/task/to-tf.js +3 -2
  139. package/dist-sdk/src/entities/view/from-tf.js +36 -10
  140. package/dist-sdk/src/entities/view/to-tf.d.ts +2 -0
  141. package/dist-sdk/src/entities/view/to-tf.js +44 -30
  142. package/dist-sdk/src/entities/widget/from-tf.js +2 -2
  143. package/dist-sdk/src/entities/widget/to-tf.d.ts +1 -0
  144. package/dist-sdk/src/entities/widget/to-tf.js +52 -23
  145. package/dist-sdk/src/graph/index.d.ts +19 -1
  146. package/dist-sdk/src/graph/index.js +20 -0
  147. package/dist-sdk/src/graph/types.d.ts +19 -0
  148. package/package.json +10 -5
@@ -1,6 +1,7 @@
1
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
- import { appResourceLabelPrefix, appScopedResourceLabel, expr, isCanonicalTsLabel, isTfExpr, isUuid, sanitizeName, scopedResourceLabel, snakeSlug, tsLabelToTfLabel, } from "../../engine/tf/index.js";
3
+ import { isUnsafePlatformId } from "../../core/platform-id.js";
4
+ import { appResourceLabelPrefix, appScopedResourceLabel, expr, isTfExpr, isUuid, sanitizeName, scopedResourceLabel, snakeSlug, tsLabelToTfLabel, } from "../../engine/tf/index.js";
4
5
  import { workspaceAspectKey } from "../../engine/build/context.js";
5
6
  import { SEARCH_TABLE_RESOURCE_TYPES } from "../search-table/types.js";
6
7
  import { fieldRefToTfValue } from "../field/to-tf.js";
@@ -15,6 +16,8 @@ import { UNRESOLVED_FIELD_MARKER } from "../shared/ref.js";
15
16
  import { TABLE_RESOURCE_TYPE } from "../table/module.js";
16
17
  import { AUTOMATION_RESOURCE_TYPE, WORKFLOW_PUBLISH_RESOURCE_TYPE } from "./types.js";
17
18
  import { onDemandTriggerRefKey } from "./value-ref-tf.js";
19
+ import { taskTfLabel } from "./task-label.js";
20
+ import { isOnDemandRecordType } from "../shared/types/field-types.js";
18
21
  function scopedLabel(appTfLabel, label) {
19
22
  return appScopedResourceLabel(appTfLabel, label);
20
23
  }
@@ -105,6 +108,10 @@ const TASK_RESOURCES = {
105
108
  runAutomation: "elementum_run_automation_task",
106
109
  aspectRecordFieldLocking: "elementum_aspect_record_field_locking_task",
107
110
  aiFileAnalysis: "elementum_ai_file_read_task",
111
+ // Not a real resource - a read-only data-source reference to a platform
112
+ // task type EDK doesn't implement (edk#785/edk#821). See the `isData` flag
113
+ // on `taskRefs` entries and the dedicated emission branch in `emitTasks`.
114
+ unimplementedTask: "elementum_unmanaged_task_reference",
108
115
  };
109
116
  const CONFIG_ATTRS = {
110
117
  aspectId: "object_id",
@@ -121,6 +128,7 @@ const CONFIG_ATTRS = {
121
128
  emailAliasId: "email_alias_id",
122
129
  approvalChainTemplateId: "approval_chain_template_id",
123
130
  approvalProcessId: "approval_process_id",
131
+ approvalReference: "approval_reference",
124
132
  surveyTemplateId: "survey_template_id",
125
133
  workflowFields: "fields",
126
134
  recordReference: "record_reference",
@@ -359,6 +367,44 @@ function automationTargetToTf(value, ctx, dataBlocks) {
359
367
  : undefined;
360
368
  if (managedOwnerLabel !== undefined) {
361
369
  const managedLabel = scopedLabel(managedOwnerLabel, tsLabelToTfLabel(value.refName));
370
+ const targetOwner = ctx.workspaceAspects?.get(workspaceAspectKey(ownerKind, isOwn ? ctx.appRefName : value.parent ?? ""));
371
+ const targetActiveNames = targetOwner?.activeAutomationNamesByRefName;
372
+ const isActive = targetActiveNames !== undefined
373
+ ? targetActiveNames.has(value.refName)
374
+ : isOwn && ctx.activeAutomationRefNames?.has(value.refName) === true;
375
+ // The on-demand trigger resource exposes the parameter ids from the
376
+ // mutable draft workflow. Once an ACTIVE target is published, however,
377
+ // run-automation validation is performed against its current workflow.
378
+ // Re-read the target after publish so both the target id and parameter
379
+ // ids describe that same current revision. Draft/inactive targets retain
380
+ // the direct resource references below because they have no publish edge.
381
+ if (isActive) {
382
+ const lookupName = targetActiveNames?.get(value.refName) ??
383
+ (isOwn ? ctx.automationNamesByRefName?.get(value.refName) : undefined) ??
384
+ value.name;
385
+ if (lookupName === undefined || lookupName === "") {
386
+ throw new EntityTfError(`managed ACTIVE automation ref "${value.refName}" has no lookup name`);
387
+ }
388
+ // Data block addresses are module-global even though EDK emits one
389
+ // file per automation. Scope the lookup to this caller so two callers
390
+ // of the same target cannot declare a duplicate data address.
391
+ const currentBindingLabel = `${managedLabel}_for_${ctx.automationTfLabel}`;
392
+ dataBlocks.set(`${AUTOMATION_RESOURCE_TYPE}.${currentBindingLabel}`, {
393
+ kind: "data",
394
+ type: AUTOMATION_RESOURCE_TYPE,
395
+ label: currentBindingLabel,
396
+ attrs: {
397
+ app_id: expr.resourceAttr(ownerKind === "element" ? "elementum_element" : "elementum_app", managedOwnerLabel, ["id"]),
398
+ name: lookupName,
399
+ depends_on: [expr.resourceAttr(WORKFLOW_PUBLISH_RESOURCE_TYPE, managedLabel)],
400
+ },
401
+ });
402
+ return {
403
+ id: expr.dataAttr(AUTOMATION_RESOURCE_TYPE, currentBindingLabel, ["id"]),
404
+ parameterId: (name) => expr.dataAttr(AUTOMATION_RESOURCE_TYPE, currentBindingLabel, ["parameter_ids", { index: name }]),
405
+ managedLabel,
406
+ };
407
+ }
362
408
  return {
363
409
  id: expr.resourceAttr(AUTOMATION_RESOURCE_TYPE, managedLabel, ["id"]),
364
410
  parameterId: (name) => expr.resourceAttr("elementum_on_demand_trigger", managedLabel, ["parameter_ids", { index: name }]),
@@ -437,7 +483,7 @@ function tokenToTf(value, ctx, dataBlocks) {
437
483
  return value.refName;
438
484
  case "app":
439
485
  case "element":
440
- if (value.refName !== ctx.appRefName) {
486
+ if (value.refName !== ctx.appRefName || value.__kind !== ctx.parentScope) {
441
487
  const sibling = ctx.workspaceAspects?.get(workspaceAspectKey(value.__kind, value.refName));
442
488
  if (sibling !== undefined) {
443
489
  const siblingType = sibling.kind === "element" ? "elementum_element" : "elementum_app";
@@ -458,7 +504,7 @@ function tokenToTf(value, ctx, dataBlocks) {
458
504
  });
459
505
  return expr.dataAttr(resolved.resourceType, resolved.tfLabel, ["id"]);
460
506
  }
461
- return expr.resourceAttr("elementum_app", ctx.appTfLabel, ["id"]);
507
+ return expr.resourceAttr(ctx.parentScope === "element" ? "elementum_element" : "elementum_app", ctx.appTfLabel, ["id"]);
462
508
  case "field": {
463
509
  // `!== ctx.appRefName` alone isn't enough: an app and a FOREIGN
464
510
  // ELEMENT can share a refName (workspace keys disambiguate by
@@ -506,7 +552,40 @@ function tokenToTf(value, ctx, dataBlocks) {
506
552
  if (value.parent === undefined || value.parent === "") {
507
553
  throw new EntityTfError(`datamine ref "${value.refName}" is missing its owning table`);
508
554
  }
509
- return expr.resourceAttr("elementum_datamine", `${tsLabelToTfLabel(value.parent)}_${tsLabelToTfLabel(value.refName)}`, ["id"]);
555
+ {
556
+ const label = `${tsLabelToTfLabel(value.parent)}_${tsLabelToTfLabel(value.refName)}`;
557
+ const tableSibling = ctx.workspaceAspects?.get(workspaceAspectKey("table", value.parent));
558
+ let tableId;
559
+ if (tableSibling !== undefined) {
560
+ tableId = expr.resourceAttr(TABLE_RESOURCE_TYPE, tableSibling.tfLabel, ["id"]);
561
+ if (tableSibling.managedDatamineLabels?.has(label) === true) {
562
+ return expr.resourceAttr("elementum_datamine", label, ["id"]);
563
+ }
564
+ }
565
+ else {
566
+ const tableLabel = tsLabelToTfLabel(value.parent);
567
+ const tableName = value.parent;
568
+ if (ctx.orgRefs !== undefined) {
569
+ ctx.orgRefs.rawLookup(TABLE_RESOURCE_TYPE, tableLabel, tableName);
570
+ }
571
+ else {
572
+ dataBlocks.set(`${TABLE_RESOURCE_TYPE}.${tableLabel}`, {
573
+ kind: "data",
574
+ type: TABLE_RESOURCE_TYPE,
575
+ label: tableLabel,
576
+ attrs: { name: tableName },
577
+ });
578
+ }
579
+ tableId = expr.dataAttr(TABLE_RESOURCE_TYPE, tableLabel, ["id"]);
580
+ }
581
+ dataBlocks.set(`elementum_datamine.${label}`, {
582
+ kind: "data",
583
+ type: "elementum_datamine",
584
+ label,
585
+ attrs: { table_id: tableId, name: value.name ?? value.refName },
586
+ });
587
+ return expr.dataAttr("elementum_datamine", label, ["id"]);
588
+ }
510
589
  case "table": {
511
590
  const sibling = ctx.workspaceAspects?.get(workspaceAspectKey("table", value.refName));
512
591
  if (sibling !== undefined) {
@@ -630,6 +709,61 @@ function tokenToTf(value, ctx, dataBlocks) {
630
709
  throw new EntityTfError(`catalog ref kind "${value.__kind}" has no automation HCL representation`);
631
710
  }
632
711
  }
712
+ function procedureToTf(value, ctx, dataBlocks) {
713
+ if (value.__kind !== "procedure")
714
+ throw new EntityTfError("procedure requires a procedure catalog reference");
715
+ if (!value.name)
716
+ throw new EntityTfError(`procedure token "${value.refName}" has no lookup name`);
717
+ if (!value.parent)
718
+ throw new EntityTfError(`procedure token "${value.refName}" is missing its owning CloudLink`);
719
+ if (!value.parentName) {
720
+ throw new EntityTfError(`procedure token "${value.parent}.${value.refName}" has no CloudLink lookup name`);
721
+ }
722
+ if (!Array.isArray(value.parameterTypes) || !value.parameterTypes.every((type) => typeof type === "string")) {
723
+ throw new EntityTfError(`procedure token "${value.parent}.${value.refName}" is missing its exact parameter signature`);
724
+ }
725
+ if (!value.databaseName || !value.schemaName) {
726
+ throw new EntityTfError(`procedure token "${value.parent}.${value.refName}" is missing its Snowflake database/schema`);
727
+ }
728
+ const cloudLinkLabel = tsLabelToTfLabel(value.parent);
729
+ const procedureLabel = `${cloudLinkLabel}_${tsLabelToTfLabel(value.refName)}`;
730
+ const cloudLinkId = expr.dataAttr("elementum_cloudlink", cloudLinkLabel, ["id"]);
731
+ if (ctx.orgRefs !== undefined) {
732
+ ctx.orgRefs.rawLookup("elementum_cloudlink", cloudLinkLabel, value.parentName);
733
+ ctx.orgRefs.rawLookup("elementum_stored_function", procedureLabel, value.name, {
734
+ cloudlink_id: cloudLinkId,
735
+ database_name: value.databaseName,
736
+ schema_name: value.schemaName,
737
+ type: "PROCEDURE",
738
+ parameter_types: value.parameterTypes,
739
+ });
740
+ }
741
+ else {
742
+ dataBlocks.set(`elementum_cloudlink.${cloudLinkLabel}`, {
743
+ kind: "data",
744
+ type: "elementum_cloudlink",
745
+ label: cloudLinkLabel,
746
+ attrs: { name: value.parentName },
747
+ });
748
+ dataBlocks.set(`elementum_stored_function.${procedureLabel}`, {
749
+ kind: "data",
750
+ type: "elementum_stored_function",
751
+ label: procedureLabel,
752
+ attrs: {
753
+ cloudlink_id: cloudLinkId,
754
+ name: value.name,
755
+ database_name: value.databaseName,
756
+ schema_name: value.schemaName,
757
+ type: "PROCEDURE",
758
+ parameter_types: value.parameterTypes,
759
+ },
760
+ });
761
+ }
762
+ return {
763
+ cloudLinkId,
764
+ procedureId: expr.dataAttr("elementum_stored_function", procedureLabel, ["id"]),
765
+ };
766
+ }
633
767
  function templateToTf(value, refs) {
634
768
  const template = value.template;
635
769
  const parameters = value.parameters;
@@ -647,11 +781,17 @@ function templateToTf(value, refs) {
647
781
  let cursor = 0;
648
782
  const pattern = /\{\{\{([^}]+)\}\}\}/g;
649
783
  for (let match = pattern.exec(template); match !== null; match = pattern.exec(template)) {
784
+ // The parameter list, not triple-brace text alone, identifies EDK
785
+ // reference placeholders. Third-party URLs and payloads can legitimately
786
+ // contain their own `{{{name}}}` syntax; leave unmatched occurrences in
787
+ // the surrounding literal so pull -> build reproduces them verbatim.
788
+ if (!byName.has(match[1]))
789
+ continue;
650
790
  if (match.index > cursor)
651
791
  parts.push(template.slice(cursor, match.index));
652
792
  const part = byName.get(match[1]);
653
793
  if (part === undefined || !isTfExpr(part)) {
654
- throw new EntityTfError(`template parameter "${match[1]}" is missing or is not a reference`);
794
+ throw new EntityTfError(`template parameter "${match[1]}" is not a reference`);
655
795
  }
656
796
  parts.push(part);
657
797
  cursor = match.index + match[0].length;
@@ -747,9 +887,14 @@ function calculationFormulaRefToParts(value, refs) {
747
887
  * not the task display name). Task id interpolates from the task resource.
748
888
  */
749
889
  function calculationTaskMustache(taskRefKey, wireName, refs) {
890
+ if (wireName.includes('"')) {
891
+ throw new EntityTfError(`calculation task output name "${wireName}" contains a double quote`);
892
+ }
750
893
  const target = refs.tasks.get(taskRefKey);
751
- if (target === undefined)
752
- throw new EntityTfError(`unresolved task ref "${taskRefKey}" in calculation formula`);
894
+ if (target === undefined) {
895
+ refs.owner.diagnostics.push(`automation unresolved task ref ${JSON.stringify(taskRefKey)} preserved as a literal calculation reference`);
896
+ return [`{{task name="${wireName}" id="${taskRefKey}"}}`];
897
+ }
753
898
  if (wireName.includes('"')) {
754
899
  throw new EntityTfError(`calculation task output name "${wireName}" contains a double quote`);
755
900
  }
@@ -877,6 +1022,12 @@ function wireRefName(name, ctx) {
877
1022
  // producer, and it marks the access as an unresolved-field TODO.
878
1023
  if (isUuid(fieldKey))
879
1024
  return name;
1025
+ // Pull-time unresolvedField() uses a typed marker around the raw platform
1026
+ // id so the generated source remains explicit. Strip that marker on the
1027
+ // wire path, just as calculation refs do above.
1028
+ if (fieldKey.startsWith(UNRESOLVED_FIELD_MARKER)) {
1029
+ return `record.${fieldKey.slice(UNRESOLVED_FIELD_MARKER.length)}${parts.length > 2 ? `.${parts.slice(2).join(".")}` : ""}`;
1030
+ }
880
1031
  // System fields (title/status/stage/createdBy/...) have no entry in
881
1032
  // ctx.fieldNames - that map only carries the app's custom fields bag.
882
1033
  // Fall back to the provider's default display name for them; a renamed
@@ -962,11 +1113,40 @@ function taskWireRefName(name, taskRefKey, refs) {
962
1113
  });
963
1114
  }
964
1115
  function triggerWireRefName(name, refs) {
965
- const declared = onDemandTriggerRefKey(name, refs.triggerParamNames);
966
- if (declared !== name || refs.triggerParamNames.includes(name))
1116
+ const scalarParams = refs.triggerParamNames.filter((param) => !refs.recordParamNames.includes(param));
1117
+ const declared = onDemandTriggerRefKey(name, scalarParams);
1118
+ if (declared !== name || scalarParams.includes(name))
967
1119
  return declared;
1120
+ const recordParamName = refs.recordParamNames
1121
+ .filter((paramName) => name === paramName || name.startsWith(`${paramName}.`))
1122
+ .sort((a, b) => b.length - a.length)[0];
1123
+ if (recordParamName !== undefined) {
1124
+ const paramName = recordParamName;
1125
+ const suffix = name === paramName ? "" : name.slice(paramName.length + 1);
1126
+ const displayName = suffix === ""
1127
+ ? "Record"
1128
+ : recordParamFieldWireName(paramName, suffix, refs);
1129
+ return refs.recordParamNames.length > 1
1130
+ ? `${paramName} - ${displayName}`
1131
+ : displayName;
1132
+ }
968
1133
  return wireRefName(name, refs.owner);
969
1134
  }
1135
+ function recordParamFieldWireName(paramName, suffix, refs) {
1136
+ const target = refs.recordParamTargets.get(paramName);
1137
+ if (target === undefined) {
1138
+ throw new EntityTfError(`onDemand record parameter "${paramName}" is missing its target app/element`);
1139
+ }
1140
+ const targetRefName = target.refName;
1141
+ const targetFieldNames = targetRefName === refs.owner.appRefName && target.__kind === refs.owner.parentScope
1142
+ ? refs.owner.fieldNames
1143
+ : refs.fieldNamesByAppRefName?.get(targetRefName);
1144
+ return wireRefName(`record.${suffix}`, {
1145
+ ...refs.owner,
1146
+ appRefName: targetRefName,
1147
+ fieldNames: targetFieldNames ?? new Map(),
1148
+ });
1149
+ }
970
1150
  /** Run-automation targets reference publish for ACTIVE automations, automation for draft/inactive. */
971
1151
  export function automationRunTargetResourceAttr(appTfLabel, automationRefName, activeAutomationRefNames) {
972
1152
  const label = appScopedResourceLabel(appTfLabel, tsLabelToTfLabel(automationRefName));
@@ -1001,15 +1181,19 @@ function workflowPublishOutputEnvelope(value, refs) {
1001
1181
  if (!isObject(value))
1002
1182
  return undefined;
1003
1183
  if (isObject(value.taskReference) && typeof value.taskReference.taskRefKey === "string" && typeof value.taskReference.name === "string") {
1184
+ if (value.taskReference.name.includes('"')) {
1185
+ throw new EntityTfError(`task output name "${value.taskReference.name}" contains a double quote`);
1186
+ }
1004
1187
  const target = refs.tasks.get(value.taskReference.taskRefKey);
1005
1188
  if (target === undefined)
1006
1189
  return undefined;
1007
1190
  const isFileReaderTask = target.type === "elementum_ai_file_read_task" || target.type === "elementum_file_reader_task";
1191
+ const isAiAgentTask = target.type === "elementum_ai_agent_task";
1008
1192
  const isAiSearchTableRecords = target.type === "elementum_ai_search_table_task" &&
1009
1193
  (value.taskReference.name === "records" || value.taskReference.name === "All Records");
1010
1194
  const isTableRecordsSearchRecords = target.type === "elementum_table_records_search_task" &&
1011
1195
  (value.taskReference.name === "records" || value.taskReference.name === "All matching table rows");
1012
- const wireName = isFileReaderTask
1196
+ const refsKey = isFileReaderTask || isAiAgentTask
1013
1197
  ? value.taskReference.name
1014
1198
  : isAiSearchTableRecords
1015
1199
  ? "All Records"
@@ -1018,12 +1202,15 @@ function workflowPublishOutputEnvelope(value, refs) {
1018
1202
  ? "All matching table rows"
1019
1203
  : "records"
1020
1204
  : taskWireRefName(value.taskReference.name, value.taskReference.taskRefKey, refs);
1021
- const typed = publishOutputType(value, target, wireName);
1205
+ const platformRefName = isAiAgentTask
1206
+ ? aiAgentPlatformRefName(refsKey)
1207
+ : refsKey;
1208
+ const typed = publishOutputType(value, target, refsKey);
1022
1209
  return {
1023
1210
  fieldType: typed.fieldType,
1024
1211
  multiple: typed.multiple,
1025
1212
  taskReference: {
1026
- name: wireName,
1213
+ name: platformRefName,
1027
1214
  taskId: expr.resourceAttr(target.type, target.label, ["id"]),
1028
1215
  },
1029
1216
  // ei export always pads the unused reference arms with null.
@@ -1057,6 +1244,19 @@ function workflowPublishOutputEnvelope(value, refs) {
1057
1244
  }
1058
1245
  return undefined;
1059
1246
  }
1247
+ /**
1248
+ * The provider's ai-agent refs map and the serialized platform reference use
1249
+ * different names. Keep the map key for Terraform indexing, but emit the
1250
+ * nested taskReference.name expected by workflow publish envelopes.
1251
+ */
1252
+ function aiAgentPlatformRefName(refsKey) {
1253
+ if (refsKey === "Action Response")
1254
+ return "ai_agent_task_response";
1255
+ if (refsKey === "ai_agent_task_response" || refsKey.startsWith("ai_agent_task_structured_output.")) {
1256
+ return refsKey;
1257
+ }
1258
+ return `ai_agent_task_structured_output.${refsKey.toLowerCase()}`;
1259
+ }
1060
1260
  function publishOutputType(value, target, wireName) {
1061
1261
  if (typeof value.fieldType === "string") {
1062
1262
  return { fieldType: value.fieldType, multiple: value.multiple === true };
@@ -1108,9 +1308,14 @@ function valueToTf(value, refs) {
1108
1308
  return expr.resourceAttr(refs.trigger.type, refs.trigger.label, ["refs", { index: triggerWireRefName(value.triggerReference.name, refs) }]);
1109
1309
  }
1110
1310
  if (isObject(value.taskReference) && typeof value.taskReference.taskRefKey === "string" && typeof value.taskReference.name === "string") {
1311
+ if (value.taskReference.name.includes('"')) {
1312
+ throw new EntityTfError(`task output name "${value.taskReference.name}" contains a double quote`);
1313
+ }
1111
1314
  const target = refs.tasks.get(value.taskReference.taskRefKey);
1112
- if (target === undefined)
1113
- throw new EntityTfError(`unresolved task ref "${value.taskReference.taskRefKey}"`);
1315
+ if (target === undefined) {
1316
+ refs.owner.diagnostics.push(`automation unresolved task ref ${JSON.stringify(value.taskReference.taskRefKey)} preserved as a literal reference`);
1317
+ return `{{task name="${value.taskReference.name}" id="${value.taskReference.taskRefKey}"}}`;
1318
+ }
1114
1319
  // aiFileAnalysis/readFile's refs are keyed by whatever arbitrary field
1115
1320
  // names the file reader schema itself defines (e.g. "title",
1116
1321
  // "document_type") - wireRefName's generic segment table
@@ -1121,6 +1326,7 @@ function valueToTf(value, refs) {
1121
1326
  // resources' actual refs map - confirmed live via a real apply's
1122
1327
  // "Invalid index" error. Their field names pass through verbatim.
1123
1328
  const isFileReaderTask = target.type === "elementum_ai_file_read_task" || target.type === "elementum_file_reader_task";
1329
+ const isAiAgentTask = target.type === "elementum_ai_agent_task";
1124
1330
  // aiSearchTable's collection is `refs["All Records"]`, not recordSearch's
1125
1331
  // globally-mapped `refs["All found records"]` (TS_REF_SEGMENT_TO_WIRE).
1126
1332
  // Accept both the authoring segment and the already-rewritten wire name
@@ -1130,7 +1336,7 @@ function valueToTf(value, refs) {
1130
1336
  // tableRecordsSearch uses yet another wire key: "All matching table rows".
1131
1337
  const isTableRecordsSearchRecords = target.type === "elementum_table_records_search_task" &&
1132
1338
  (value.taskReference.name === "records" || value.taskReference.name === "All matching table rows");
1133
- const refKey = isFileReaderTask
1339
+ const refKey = isFileReaderTask || isAiAgentTask
1134
1340
  ? value.taskReference.name
1135
1341
  : isAiSearchTableRecords
1136
1342
  ? "All Records"
@@ -1259,7 +1465,11 @@ function filterReferenceToTf(value, refs, role = "value", realType) {
1259
1465
  const task = refs.tasks.get(value.taskRefKey);
1260
1466
  if (task === undefined)
1261
1467
  throw new EntityTfError(`unresolved task filter ref "${value.taskRefKey}"`);
1262
- const wireName = taskWireRefName(value.name, value.taskRefKey, refs);
1468
+ const isFileReaderTask = task.type === "elementum_ai_file_read_task" || task.type === "elementum_file_reader_task";
1469
+ const isAiAgentTask = task.type === "elementum_ai_agent_task";
1470
+ const wireName = isFileReaderTask || isAiAgentTask
1471
+ ? value.name
1472
+ : taskWireRefName(value.name, value.taskRefKey, refs);
1263
1473
  // left_value (switch cases): provider Read stores the JSON envelope that
1264
1474
  // `.refs["…"]` evaluates to (#490). Emitting the short `task.<id>.…`
1265
1475
  // string matches record_search RHS state but perpetual-diffs left_value.
@@ -1327,7 +1537,15 @@ function picklistFilterValueToTf(value, field, refs) {
1327
1537
  // rather than blocking on platform data it can't fix.
1328
1538
  if (isUuid(value))
1329
1539
  return undefined;
1330
- throw new EntityTfError(`automation filter for picklist field "${field.name ?? field.refName}" uses unknown option ${JSON.stringify(value)}; expected one of: ${field.options.map((option) => JSON.stringify(option)).join(", ")}`);
1540
+ // A pulled automation can legitimately retain a label for an option that
1541
+ // was deleted/recreated after the automation was authored. Keep the
1542
+ // literal so build remains a lossless offline round-trip; the provider
1543
+ // can report the platform-side mismatch during plan/apply.
1544
+ if (refs.allowUnknownPicklistOptions !== true) {
1545
+ throw new EntityTfError(`automation filter for picklist field "${field.name ?? field.refName}" uses unknown option ${JSON.stringify(value)}; expected one of: ${field.options.map((option) => JSON.stringify(option)).join(", ")}`);
1546
+ }
1547
+ refs.owner.diagnostics.push(`automation filter for picklist field ${JSON.stringify(field.name ?? field.refName)} preserves unknown option ${JSON.stringify(value)}`);
1548
+ return undefined;
1331
1549
  }
1332
1550
  if (field.parent !== undefined && field.parentKind !== undefined) {
1333
1551
  // A field owned by a SIBLING app/element built in this same workspace
@@ -1410,13 +1628,23 @@ function configAttrs(kind, config, refs) {
1410
1628
  continue;
1411
1629
  // A config value of exactly `undefined` almost always means the
1412
1630
  // authoring builder was called with a required option omitted (e.g. a
1413
- // TS-only-enforced field that jiti/bun never actually type-checks at
1631
+ // TS-only-enforced field that no runtime actually type-checks at
1414
1632
  // build time) - name the wire key here, since the generic valueToTf
1415
1633
  // fallback below has no attribute name to report by the time it gets to
1416
1634
  // its own "unsupported value" throw.
1417
1635
  if (raw === undefined) {
1418
1636
  throw new EntityTfError(`${kind}: config value for "${key}" is undefined — likely a missing required option on the authoring builder call that built this task/trigger`);
1419
1637
  }
1638
+ if (key === "procedure" && kind === "elementum_procedure_task") {
1639
+ const procedure = token(raw);
1640
+ if (procedure?.__kind !== "procedure") {
1641
+ throw new EntityTfError("procedure requires a procedure catalog reference");
1642
+ }
1643
+ const resolved = procedureToTf(procedure, refs.owner, refs.dataBlocks);
1644
+ attrs.cloudlink_id = resolved.cloudLinkId;
1645
+ attrs.stored_function_id = resolved.procedureId;
1646
+ continue;
1647
+ }
1420
1648
  // elementum_create_record_task has no "relate to record" boolean
1421
1649
  // attribute - relate_to_record_reference's presence alone conveys the
1422
1650
  // intent (see from-tf.ts's matching comment), so the flag itself is
@@ -1458,6 +1686,14 @@ function configAttrs(kind, config, refs) {
1458
1686
  });
1459
1687
  continue;
1460
1688
  }
1689
+ if (key === "outputMappings" && kind === "elementum_run_automation_task" && config.targetAutomationId !== undefined) {
1690
+ // Static run-automation tasks expose the published target's outputs in
1691
+ // `refs` automatically. The live API rejects even valid, matching
1692
+ // outputMappings with "Unable to validate" (#703), so keep authored
1693
+ // output names as a TypeScript typing aid and do not send the redundant
1694
+ // backend input. Dynamic targets still need their explicit mappings.
1695
+ continue;
1696
+ }
1461
1697
  if (key === "targetAutomationRef" && kind === "elementum_run_automation_task") {
1462
1698
  if (!isObject(raw) || raw.type !== "STATIC") {
1463
1699
  throw new EntityTfError("runAutomation targetAutomationRef must be { type: \"STATIC\", value: ... }");
@@ -1559,6 +1795,10 @@ function configAttrs(kind, config, refs) {
1559
1795
  attrs.record_id = valueToTf(raw, refs);
1560
1796
  continue;
1561
1797
  }
1798
+ if (key === "recordId" && kind === "elementum_initiate_call_task") {
1799
+ attrs.record_id = valueToTf(raw, refs);
1800
+ continue;
1801
+ }
1562
1802
  if (key === "calculations" && kind === "elementum_calculation_task") {
1563
1803
  if (!Array.isArray(raw))
1564
1804
  throw new EntityTfError("calculations must be an array");
@@ -1611,18 +1851,23 @@ function configAttrs(kind, config, refs) {
1611
1851
  const parameters = raw.map((input) => {
1612
1852
  if (!isObject(input))
1613
1853
  throw new EntityTfError("onDemand input must be an object");
1614
- return Object.fromEntries(Object.entries(input).map(([k, v]) => [
1854
+ const mapped = Object.fromEntries(Object.entries(input)
1855
+ .filter(([k]) => k !== "app")
1856
+ .map(([k, v]) => [
1615
1857
  CONFIG_ATTRS[k] ?? snake(k),
1616
1858
  valueToTf(k === "defaultValue" ? unwrapDefault(v) :
1617
- // elementum_on_demand_trigger.parameters[].type's real validator
1618
- // (on_demand_trigger_resource.go) only accepts a bare "attachment"
1619
- // - there is no "attachment_id" variant at this layer, unlike the
1620
- // WorkflowFieldType enum name (ATTACHMENT_ID) the "attachmentId"
1621
- // authoring alias otherwise maps to. A generic lowercase would
1622
- // wrongly emit "attachment_id" and fail plan/apply.
1623
1859
  k === "type" && v === "ATTACHMENT_ID" ? "attachment" :
1624
- k === "type" && typeof v === "string" ? v.toLowerCase() : v, refs),
1860
+ k === "type" && v === "ASPECT_RECORD" ? "record" :
1861
+ k === "type" && typeof v === "string" ? v.toLowerCase() : v, refs),
1625
1862
  ]));
1863
+ if (isOnDemandRecordType(typeof input.type === "string" ? input.type : undefined)) {
1864
+ const target = token(input.app);
1865
+ if (target === undefined || (target.__kind !== "app" && target.__kind !== "element")) {
1866
+ throw new EntityTfError(`onDemand record parameter ${JSON.stringify(input.name)} requires an explicit app/element target`);
1867
+ }
1868
+ mapped.default_value = tokenToTf(target, refs.owner, refs.dataBlocks);
1869
+ }
1870
+ return mapped;
1626
1871
  });
1627
1872
  // The provider normalizes an absent parameter collection to null. An
1628
1873
  // explicit [] therefore creates perpetual [] vs null drift after pull.
@@ -1817,28 +2062,14 @@ function fileReaderDependsOn(blocks) {
1817
2062
  });
1818
2063
  return deps;
1819
2064
  }
1820
- function taskLabel(automationLabel, refKey) {
1821
- // ei's task label is `<auto>_<sanitizeName(task name)>` for display-name
1822
- // stems. Pulled refKeys that are already canonical camelCase (e.g.
1823
- // `commodityBHit_1`) must use the lossless `tsLabelToTfLabel` bijection —
1824
- // `sanitizeName` lowercases before splitCamelCase and collapses
1825
- // single-letter camel segments (`commodityBHit` → `commodityb_hit`), which
1826
- // plans destroy+create against export's `commodity_b_hit_1`.
1827
- //
1828
- // ALLCAPS / SCREAMING_SNAKE refKeys (`GREETING`) also round-trip the
1829
- // bijection (via ugly `_g_r_e_e_t_i_n_g`), but ei's SanitizeName yields
1830
- // `greeting` — require a lowercase letter before preferring the bijection.
1831
- const normalized = isCanonicalTsLabel(refKey) && /[a-z]/.test(refKey)
1832
- ? tsLabelToTfLabel(refKey)
1833
- : sanitizeName(refKey);
1834
- return normalized.startsWith(`${automationLabel}_`) ? normalized : `${automationLabel}_${normalized}`;
1835
- }
1836
2065
  /** App- or element-owned `automation` entity → provider HCL blocks. */
1837
2066
  export function automationToTf(entity, ctx) {
2067
+ const automationTfLabel = scopedLabel(ctx.appTfLabel, tsLabelToTfLabel(entity.refName));
1838
2068
  const owner = {
1839
2069
  appRefName: ctx.appRefName ?? entity.parent,
1840
2070
  parentScope: ctx.parentScope ?? "app",
1841
2071
  appTfLabel: ctx.appTfLabel,
2072
+ automationTfLabel,
1842
2073
  fieldAddresses: ctx.fieldAddresses ?? new Map(),
1843
2074
  fieldNames: ctx.fieldNames ?? new Map(),
1844
2075
  fieldMetadata: ctx.fieldMetadata ?? new Map(),
@@ -1847,6 +2078,7 @@ export function automationToTf(entity, ctx) {
1847
2078
  workspaceAspects: ctx.workspaceAspects,
1848
2079
  diagnostics: ctx.diagnostics ?? [],
1849
2080
  activeAutomationRefNames: ctx.activeAutomationRefNames,
2081
+ automationNamesByRefName: ctx.automationNamesByRefName,
1850
2082
  };
1851
2083
  if (entity.kind !== "automation")
1852
2084
  throw new EntityTfError(`automationToTf expects kind "automation"; got "${entity.kind}"`);
@@ -1858,7 +2090,7 @@ export function automationToTf(entity, ctx) {
1858
2090
  throw new EntityTfError(`automation "${entity.refName}" has a non-object payload`);
1859
2091
  if (typeof payload.workflowRevision !== "string")
1860
2092
  throw new EntityTfError(`automation "${entity.refName}" has no workflowRevision`);
1861
- const label = scopedLabel(owner.appTfLabel, tsLabelToTfLabel(entity.refName));
2093
+ const label = automationTfLabel;
1862
2094
  const automation = {
1863
2095
  kind: "resource",
1864
2096
  type: AUTOMATION_RESOURCE_TYPE,
@@ -1908,7 +2140,10 @@ export function automationToTf(entity, ctx) {
1908
2140
  const resourceType = wireType === "variable" ? variableTaskResourceType(taskBody, refKey) : TASK_RESOURCES[wireType];
1909
2141
  if (resourceType === undefined)
1910
2142
  throw new EntityTfError(`task type "${wireType}" is not supported by the provider HCL contract`);
1911
- const registeredLabel = taskLabel(label, refKey);
2143
+ // elementum_unmanaged_task_reference is a data source exported under its
2144
+ // own bare sanitized-name label - never scoped under the automation the
2145
+ // way every real task resource is (confirmed live, edk#821).
2146
+ const registeredLabel = wireType === "unimplementedTask" ? sanitizeName(refKey) : taskTfLabel(label, refKey);
1912
2147
  const collidingRefKey = usedLabels.get(registeredLabel);
1913
2148
  if (collidingRefKey !== undefined) {
1914
2149
  throw new EntityTfError(`task "${refKey}" sanitizes to the same tf label as task "${collidingRefKey}" (${registeredLabel}) — rename one of them`);
@@ -1916,10 +2151,15 @@ export function automationToTf(entity, ctx) {
1916
2151
  usedLabels.set(registeredLabel, refKey);
1917
2152
  const outputFieldTypes = wireType === "executeScript"
1918
2153
  ? outputFieldTypesFromScriptSchema(taskBody.outputSchema)
1919
- : undefined;
2154
+ : wireType === "aiAgent" && Array.isArray(taskBody.jsonFields)
2155
+ ? new Map(taskBody.jsonFields.flatMap((field) => isObject(field) && typeof field.name === "string" && typeof field.type === "string"
2156
+ ? [[field.name, { fieldType: field.type, multiple: false }]]
2157
+ : []))
2158
+ : undefined;
1920
2159
  taskRefs.set(refKey, {
1921
2160
  type: resourceType,
1922
2161
  label: registeredLabel,
2162
+ ...(wireType === "unimplementedTask" ? { isData: true } : {}),
1923
2163
  ...(outputFieldTypes !== undefined && outputFieldTypes.size > 0 ? { outputFieldTypes } : {}),
1924
2164
  });
1925
2165
  if (wireType === "variable" && Array.isArray(taskBody.create)) {
@@ -1939,15 +2179,35 @@ export function automationToTf(entity, ctx) {
1939
2179
  const triggerParamNames = triggerKey === "onDemand" && isObject(triggerConfig) && Array.isArray(triggerConfig.inputs)
1940
2180
  ? triggerConfig.inputs.flatMap((input) => isObject(input) && typeof input.name === "string" ? [input.name] : [])
1941
2181
  : [];
2182
+ const recordParamNames = triggerKey === "onDemand" && isObject(triggerConfig) && Array.isArray(triggerConfig.inputs)
2183
+ ? triggerConfig.inputs.flatMap((input) => isObject(input) && typeof input.name === "string" && isOnDemandRecordType(typeof input.type === "string" ? input.type : undefined)
2184
+ ? [input.name]
2185
+ : [])
2186
+ : [];
2187
+ const recordParamTargets = new Map(triggerKey === "onDemand" && isObject(triggerConfig) && Array.isArray(triggerConfig.inputs)
2188
+ ? triggerConfig.inputs.flatMap((input) => {
2189
+ if (!isObject(input) ||
2190
+ typeof input.name !== "string" ||
2191
+ !isOnDemandRecordType(typeof input.type === "string" ? input.type : undefined))
2192
+ return [];
2193
+ const target = token(input.app);
2194
+ return target !== undefined && (target.__kind === "app" || target.__kind === "element")
2195
+ ? [[input.name, target]]
2196
+ : [];
2197
+ })
2198
+ : []);
1942
2199
  const refs = {
1943
2200
  trigger: { type: triggerType, label },
1944
2201
  triggerParamNames,
2202
+ recordParamNames,
2203
+ recordParamTargets,
1945
2204
  tasks: taskRefs,
1946
2205
  owner,
1947
2206
  dataBlocks,
1948
2207
  variableById,
1949
2208
  ...(recordFieldContext !== undefined ? { recordFieldContext } : {}),
1950
2209
  ...(ctx.fieldNamesByAppRefName !== undefined ? { fieldNamesByAppRefName: ctx.fieldNamesByAppRefName } : {}),
2210
+ ...(ctx.allowUnknownPicklistOptions === true ? { allowUnknownPicklistOptions: true } : {}),
1951
2211
  };
1952
2212
  let triggerAttrs;
1953
2213
  try {
@@ -1966,7 +2226,14 @@ export function automationToTf(entity, ctx) {
1966
2226
  const blocks = [automation, trigger];
1967
2227
  const dependencies = [expr.resourceAttr(triggerType, label)];
1968
2228
  const runAutomationPublishes = new Set();
1969
- const emitTasks = (tasks, initialParent) => {
2229
+ // `enclosingOperator` describes the switch/forkJoin/forEach scope THIS
2230
+ // `emitTasks` call is nested inside (undefined at automation top level) -
2231
+ // needed only by `unimplementedTask` (edk#821) to populate
2232
+ // `operator_task_id`/`operator_child_id` correctly regardless of how many
2233
+ // real sibling tasks precede the placeholder within the same case/branch
2234
+ // (the rolling `taskParent` chain below tracks the PRECEDING SIBLING, not
2235
+ // the enclosing operator, once more than one task shares a scope).
2236
+ const emitTasks = (tasks, initialParent, enclosingOperator) => {
1970
2237
  let parent = initialParent;
1971
2238
  let first;
1972
2239
  for (const task of tasks) {
@@ -1996,7 +2263,55 @@ export function automationToTf(entity, ctx) {
1996
2263
  initialParent.path[0] === "inner";
1997
2264
  taskParent = inBodyOfExplicitParent
1998
2265
  ? initialParent
1999
- : expr.resourceAttr(explicitParent.type, explicitParent.label);
2266
+ : explicitParent.isData === true
2267
+ ? expr.dataAttr(explicitParent.type, explicitParent.label)
2268
+ : expr.resourceAttr(explicitParent.type, explicitParent.label);
2269
+ }
2270
+ if (wireType === "unimplementedTask") {
2271
+ // elementum_unmanaged_task_reference (edk#785/edk#821) is a read-only
2272
+ // data source, not a resource: it has no CRUD, never joins
2273
+ // `depends_on`, and its structural fields (app_id/automation_id/
2274
+ // workflow_id/parent_is_trigger/operator_task_id/operator_child_id)
2275
+ // are re-derived from context rather than authored - only `id` and
2276
+ // `taskType` are authored (see tasks.ts's unimplementedTask).
2277
+ const idValue = isUnsafePlatformId(inner.id) ? inner.id.value : typeof inner.id === "string" ? inner.id : undefined;
2278
+ if (idValue === undefined)
2279
+ throw new EntityTfError(`unimplementedTask "${refKey}" requires an id`);
2280
+ if (typeof inner.taskType !== "string" || inner.taskType === "") {
2281
+ throw new EntityTfError(`unimplementedTask "${refKey}" requires a taskType`);
2282
+ }
2283
+ const operatorTaskAddress = enclosingOperator?.taskAddress;
2284
+ const operatorChildAddress = enclosingOperator?.childAddress;
2285
+ const isParentTrigger = enclosingOperator === undefined;
2286
+ const extraAttrs = {
2287
+ id: idValue,
2288
+ task_type: inner.taskType,
2289
+ workflow_id: expr.resourceAttr(AUTOMATION_RESOURCE_TYPE, label, ["workflow_id"]),
2290
+ app_id: expr.resourceAttr(owner.parentScope === "element" ? "elementum_element" : "elementum_app", owner.appTfLabel, ["id"]),
2291
+ automation_id: expr.resourceAttr(AUTOMATION_RESOURCE_TYPE, label, ["id"]),
2292
+ parent_is_trigger: isParentTrigger,
2293
+ ...(operatorTaskAddress !== undefined && operatorTaskAddress.$expr === "resource-attr"
2294
+ ? { operator_task_id: expr.resourceAttr(operatorTaskAddress.type, operatorTaskAddress.label, ["id"]) }
2295
+ : {}),
2296
+ ...(operatorChildAddress !== undefined && operatorChildAddress.$expr === "resource-attr"
2297
+ ? { operator_child_id: expr.resourceAttr(operatorChildAddress.type, operatorChildAddress.label, ["id"]) }
2298
+ : {}),
2299
+ };
2300
+ if (owner.orgRefs !== undefined) {
2301
+ owner.orgRefs.rawLookup("elementum_unmanaged_task_reference", tfLabel, displayName, extraAttrs);
2302
+ }
2303
+ else {
2304
+ dataBlocks.set(`elementum_unmanaged_task_reference.${tfLabel}`, {
2305
+ kind: "data",
2306
+ type: "elementum_unmanaged_task_reference",
2307
+ label: tfLabel,
2308
+ attrs: { name: displayName, ...extraAttrs },
2309
+ });
2310
+ }
2311
+ const address = expr.dataAttr("elementum_unmanaged_task_reference", tfLabel);
2312
+ first ??= address;
2313
+ parent = address;
2314
+ continue;
2000
2315
  }
2001
2316
  if (wireType === "variable") {
2002
2317
  const variableType = variableTaskResourceType(inner, refKey);
@@ -2056,14 +2371,18 @@ export function automationToTf(entity, ctx) {
2056
2371
  const address = expr.resourceAttr(resourceType, tfLabel);
2057
2372
  dependencies.push(address);
2058
2373
  first ??= address;
2059
- // Only chain linear siblings; explicit hclParent keeps parallel roots under trigger.
2060
- if (task.hclParent === undefined)
2061
- parent = address;
2374
+ // Always advance the rolling continuation point to this task, even when
2375
+ // it was explicitly anchored (a fork root off the trigger or another
2376
+ // task). Whatever comes next in this same printed sequence is either
2377
+ // this task's own descendant (which must continue from it) or a task
2378
+ // with its own explicit anchor (which ignores this value entirely) -
2379
+ // never a sibling that should skip back to an earlier task.
2380
+ parent = address;
2062
2381
  if (wireType === "forEach") {
2063
2382
  const children = task.children ?? [];
2064
2383
  if (children.length !== 1)
2065
2384
  throw new EntityTfError(`forEach task "${refKey}" must have exactly one child body`);
2066
- emitTasks(children[0].tasks ?? [], expr.resourceAttr(resourceType, tfLabel, ["inner"]));
2385
+ emitTasks(children[0].tasks ?? [], expr.resourceAttr(resourceType, tfLabel, ["inner"]), { taskAddress: expr.resourceAttr(resourceType, tfLabel) });
2067
2386
  // The loop body is a separate operator-owned chain. A fluent sibling
2068
2387
  // authored after `forEach` continues from the for_each task itself,
2069
2388
  // not from the body's terminal task; the latter silently moves that
@@ -2074,6 +2393,20 @@ export function automationToTf(entity, ctx) {
2074
2393
  const children = task.children ?? [];
2075
2394
  if (children.length === 0)
2076
2395
  throw new EntityTfError(`${wireType} task "${refKey}" must have branches`);
2396
+ const usedLabels = new Map();
2397
+ for (const [index, child] of children.entries()) {
2398
+ const isDefault = wireType === "switch" && (child.isDefault === true || child.label === null);
2399
+ const displayLabel = isDefault
2400
+ ? (child.defaultLabel ?? "Default")
2401
+ : (child.label ?? "");
2402
+ const prior = usedLabels.get(displayLabel);
2403
+ if (prior !== undefined) {
2404
+ throw new EntityTfError(displayLabel === ""
2405
+ ? `${wireType} task "${refKey}" has more than one unlabeled case (cases ${prior} and ${index + 1}); give each case a unique label`
2406
+ : `${wireType} task "${refKey}" reuses label ${JSON.stringify(displayLabel)} on cases ${prior} and ${index + 1}`);
2407
+ }
2408
+ usedLabels.set(displayLabel, index + 1);
2409
+ }
2077
2410
  let previous;
2078
2411
  for (const [index, child] of children.entries()) {
2079
2412
  const branchType = wireType === "switch" ? "elementum_switch_case" : "elementum_fork_join_branch";
@@ -2124,7 +2457,7 @@ export function automationToTf(entity, ctx) {
2124
2457
  blocks.push({ kind: "resource", type: branchType, label: branchLabel, attrs: branchAttrs });
2125
2458
  const branchAddress = expr.resourceAttr(branchType, branchLabel);
2126
2459
  dependencies.push(branchAddress);
2127
- emitTasks(child.tasks ?? [], branchAddress);
2460
+ emitTasks(child.tasks ?? [], branchAddress, { taskAddress: address, childAddress: branchAddress });
2128
2461
  previous = branchAddress;
2129
2462
  }
2130
2463
  }