@elementumai/edk 0.9.10 → 0.9.12

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 (124) hide show
  1. package/README.md +39 -5
  2. package/dist-sdk/src/catalog/factories.d.ts +42 -14
  3. package/dist-sdk/src/catalog/factories.js +77 -14
  4. package/dist-sdk/src/catalog/from-authoring.js +4 -4
  5. package/dist-sdk/src/catalog/index.d.ts +2 -2
  6. package/dist-sdk/src/catalog/index.js +1 -1
  7. package/dist-sdk/src/catalog/org.js +3 -2
  8. package/dist-sdk/src/catalog/token.js +4 -0
  9. package/dist-sdk/src/catalog/types.d.ts +32 -5
  10. package/dist-sdk/src/catalog/types.js +1 -0
  11. package/dist-sdk/src/core/triggers/on-demand.d.ts +4 -28
  12. package/dist-sdk/src/core/triggers/on-demand.js +2 -4
  13. package/dist-sdk/src/engine/build/context.d.ts +3 -0
  14. package/dist-sdk/src/engine/entity.d.ts +2 -0
  15. package/dist-sdk/src/engine/logging.d.ts +90 -0
  16. package/dist-sdk/src/engine/logging.js +124 -0
  17. package/dist-sdk/src/engine/progress.d.ts +119 -0
  18. package/dist-sdk/src/engine/progress.js +647 -0
  19. package/dist-sdk/src/engine/pull/result-output.d.ts +24 -0
  20. package/dist-sdk/src/engine/pull/result-output.js +48 -0
  21. package/dist-sdk/src/entities/access-policy/from-tf.d.ts +3 -3
  22. package/dist-sdk/src/entities/access-policy/from-tf.js +27 -8
  23. package/dist-sdk/src/entities/access-policy/to-tf.d.ts +13 -0
  24. package/dist-sdk/src/entities/access-policy/to-tf.js +34 -3
  25. package/dist-sdk/src/entities/agent/deserializer/print-agent-v2.js +13 -7
  26. package/dist-sdk/src/entities/agent/tools.d.ts +2 -2
  27. package/dist-sdk/src/entities/agent-tool/to-tf.js +13 -0
  28. package/dist-sdk/src/entities/app/build.d.ts +22 -2
  29. package/dist-sdk/src/entities/app/build.js +6 -0
  30. package/dist-sdk/src/entities/app/deserializer/normalize.d.ts +13 -3
  31. package/dist-sdk/src/entities/app/from-tf.js +10 -7
  32. package/dist-sdk/src/entities/app/to-tf.js +14 -14
  33. package/dist-sdk/src/entities/automation/build.d.ts +11 -16
  34. package/dist-sdk/src/entities/automation/build.js +38 -8
  35. package/dist-sdk/src/entities/automation/catalog-render.js +1 -1
  36. package/dist-sdk/src/entities/automation/deserializer/ast.d.ts +1 -2
  37. package/dist-sdk/src/entities/automation/deserializer/print-automation.d.ts +23 -5
  38. package/dist-sdk/src/entities/automation/deserializer/print-automation.js +239 -34
  39. package/dist-sdk/src/entities/automation/from-tf.js +276 -19
  40. package/dist-sdk/src/entities/automation/index.d.ts +2 -2
  41. package/dist-sdk/src/entities/automation/module.js +3 -2
  42. package/dist-sdk/src/entities/automation/output-field-types.js +25 -13
  43. package/dist-sdk/src/entities/automation/script-code.d.ts +12 -4
  44. package/dist-sdk/src/entities/automation/script-code.js +659 -101
  45. package/dist-sdk/src/entities/automation/tasks.d.ts +60 -28
  46. package/dist-sdk/src/entities/automation/tasks.js +48 -15
  47. package/dist-sdk/src/entities/automation/to-tf.d.ts +8 -0
  48. package/dist-sdk/src/entities/automation/to-tf.js +514 -103
  49. package/dist-sdk/src/entities/automation/triggers.d.ts +15 -9
  50. package/dist-sdk/src/entities/automation/triggers.js +4 -2
  51. package/dist-sdk/src/entities/automation/types.js +1 -0
  52. package/dist-sdk/src/entities/chart/build.d.ts +15 -3
  53. package/dist-sdk/src/entities/chart/build.js +6 -1
  54. package/dist-sdk/src/entities/chart/from-tf.d.ts +8 -2
  55. package/dist-sdk/src/entities/chart/from-tf.js +71 -8
  56. package/dist-sdk/src/entities/chart/index.d.ts +1 -1
  57. package/dist-sdk/src/entities/chart/index.js +1 -1
  58. package/dist-sdk/src/entities/chart/printer.js +27 -7
  59. package/dist-sdk/src/entities/chart/to-tf.js +56 -2
  60. package/dist-sdk/src/entities/dashboard/from-tf.js +11 -2
  61. package/dist-sdk/src/entities/datamine/from-tf.js +11 -1
  62. package/dist-sdk/src/entities/dynamic-layout/build.d.ts +2 -1
  63. package/dist-sdk/src/entities/dynamic-layout/dialect.d.ts +8 -0
  64. package/dist-sdk/src/entities/dynamic-layout/dialect.js +12 -0
  65. package/dist-sdk/src/entities/dynamic-layout/from-tf.d.ts +7 -0
  66. package/dist-sdk/src/entities/dynamic-layout/from-tf.js +64 -10
  67. package/dist-sdk/src/entities/dynamic-layout/to-tf.js +54 -6
  68. package/dist-sdk/src/entities/element/build.d.ts +8 -0
  69. package/dist-sdk/src/entities/element/from-tf.js +4 -3
  70. package/dist-sdk/src/entities/element/module.js +1 -0
  71. package/dist-sdk/src/entities/element/to-tf.d.ts +4 -1
  72. package/dist-sdk/src/entities/element/to-tf.js +28 -2
  73. package/dist-sdk/src/entities/field/build.d.ts +10 -1
  74. package/dist-sdk/src/entities/field/build.js +2 -1
  75. package/dist-sdk/src/entities/field/calc/index.js +7 -7
  76. package/dist-sdk/src/entities/field/calc/runtime.d.ts +19 -0
  77. package/dist-sdk/src/entities/field/calc/runtime.js +32 -0
  78. package/dist-sdk/src/entities/field/calc/types.d.ts +6 -0
  79. package/dist-sdk/src/entities/field/from-tf.d.ts +24 -0
  80. package/dist-sdk/src/entities/field/from-tf.js +73 -1
  81. package/dist-sdk/src/entities/field/metadata.d.ts +18 -0
  82. package/dist-sdk/src/entities/field/metadata.js +34 -0
  83. package/dist-sdk/src/entities/file-reader/to-tf.js +10 -2
  84. package/dist-sdk/src/entities/relationship/build.d.ts +27 -9
  85. package/dist-sdk/src/entities/relationship/column-contract.d.ts +24 -0
  86. package/dist-sdk/src/entities/relationship/column-contract.js +41 -0
  87. package/dist-sdk/src/entities/relationship/from-tf.js +33 -48
  88. package/dist-sdk/src/entities/relationship/to-tf.d.ts +1 -0
  89. package/dist-sdk/src/entities/relationship/to-tf.js +33 -2
  90. package/dist-sdk/src/entities/shared/aspect-target-from-tf.d.ts +15 -0
  91. package/dist-sdk/src/entities/shared/aspect-target-from-tf.js +21 -0
  92. package/dist-sdk/src/entities/shared/context.d.ts +6 -3
  93. package/dist-sdk/src/entities/shared/context.js +19 -1
  94. package/dist-sdk/src/entities/shared/filters/index.d.ts +7 -0
  95. package/dist-sdk/src/entities/shared/filters/index.js +28 -0
  96. package/dist-sdk/src/entities/shared/filters/print.js +2 -0
  97. package/dist-sdk/src/entities/shared/filters/tf-codec.d.ts +8 -1
  98. package/dist-sdk/src/entities/shared/filters/tf-codec.js +10 -6
  99. package/dist-sdk/src/entities/shared/filters/types.d.ts +1 -1
  100. package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.d.ts +5 -0
  101. package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.js +52 -27
  102. package/dist-sdk/src/entities/skill/module.js +1 -0
  103. package/dist-sdk/src/entities/skill/to-tf.d.ts +1 -1
  104. package/dist-sdk/src/entities/skill/to-tf.js +17 -0
  105. package/dist-sdk/src/entities/skill/tools.d.ts +48 -12
  106. package/dist-sdk/src/entities/skill/tools.js +12 -1
  107. package/dist-sdk/src/entities/skill-tool/from-tf.d.ts +15 -12
  108. package/dist-sdk/src/entities/skill-tool/from-tf.js +50 -46
  109. package/dist-sdk/src/entities/skill-tool/to-tf.d.ts +4 -0
  110. package/dist-sdk/src/entities/skill-tool/to-tf.js +66 -7
  111. package/dist-sdk/src/entities/table/build.js +10 -2
  112. package/dist-sdk/src/entities/table/catalog-render.js +2 -1
  113. package/dist-sdk/src/entities/view/from-tf.js +9 -0
  114. package/dist-sdk/src/entities/view/to-tf.d.ts +7 -0
  115. package/dist-sdk/src/entities/view/to-tf.js +39 -12
  116. package/dist-sdk/src/entities/widget/build.d.ts +43 -7
  117. package/dist-sdk/src/entities/widget/from-tf.d.ts +1 -1
  118. package/dist-sdk/src/entities/widget/from-tf.js +162 -23
  119. package/dist-sdk/src/entities/widget/to-tf.d.ts +17 -0
  120. package/dist-sdk/src/entities/widget/to-tf.js +150 -6
  121. package/dist-sdk/src/graph/types.d.ts +13 -0
  122. package/package.json +1 -1
  123. package/dist-sdk/src/core/types/platform.d.ts +0 -2
  124. package/dist-sdk/src/core/types/platform.js +0 -1
@@ -1,21 +1,22 @@
1
1
  import { dataRefLabel, optionalBoolean, optionalString, plainObject, resourceRefLabel } from "../../engine/pull/context.js";
2
- import { isTfExpr, scopedLabelToBagKey, tfLabelToTsLabel } from "../../engine/tf/index.js";
2
+ import { bareChildResourceLabel, isTfExpr, scopedLabelToBagKey, tfLabelToTsLabel, tsLabelToTfLabel } from "../../engine/tf/index.js";
3
3
  import { AUTOMATION_RESOURCE_TYPE } from "../automation/types.js";
4
- import { fieldKeyFromTfRef } from "../field/from-tf.js";
4
+ import { decodeForeignDataField, fieldKeyFromTfRef } from "../field/from-tf.js";
5
5
  import { SUB_TYPE_BY_RESOURCE } from "../field/types.js";
6
6
  import { WIDGET_RESOURCE_TYPE } from "./module.js";
7
7
  import { filterFromTf } from "../access-policy/from-tf.js";
8
+ import { resolveDataAspectTarget } from "../shared/aspect-target-from-tf.js";
8
9
  const KNOWN_ROOT_ATTRS = new Set(["object_id", "related_object", "related_link_action", "related_create_action", "run_automation_action"]);
9
- export function widgetsFromTf(blocks, owner, ctx) {
10
+ export function widgetsFromTf(blocks, owner, ctx, dynamicLayoutCompanionLabels = new Set()) {
10
11
  const out = [];
11
12
  for (const block of blocks) {
12
- const widget = widgetFromTf(block, owner, ctx);
13
+ const widget = widgetFromTf(block, owner, ctx, dynamicLayoutCompanionLabels);
13
14
  if (widget !== undefined)
14
15
  out.push(widget);
15
16
  }
16
17
  return out;
17
18
  }
18
- function widgetFromTf(block, owner, ctx) {
19
+ function widgetFromTf(block, owner, ctx, dynamicLayoutCompanionLabels) {
19
20
  const label = block.label ?? "";
20
21
  const where = `${WIDGET_RESOURCE_TYPE}.${label || "?"}`;
21
22
  for (const attr of Object.keys(block.attrs)) {
@@ -32,6 +33,8 @@ function widgetFromTf(block, owner, ctx) {
32
33
  return undefined;
33
34
  }
34
35
  const variant = variants[0];
36
+ if (variant === "run_automation_action" && dynamicLayoutCompanionLabels.has(label))
37
+ return undefined;
35
38
  const body = plainObject(block.attrs[variant]);
36
39
  if (body === undefined) {
37
40
  ctx.diagnostics.push(`${where}.${variant}: expected an object; skipped`);
@@ -54,9 +57,26 @@ function widgetFromTf(block, owner, ctx) {
54
57
  const target = aspectRef(body["object_id"], owner, ctx);
55
58
  if (target === undefined)
56
59
  return undefined;
57
- const filterOwner = typeof target !== "string" && (target.kind === "app" || target.kind === "element")
58
- ? { type: `elementum_${target.kind}`, label: target.key, ownerRefName: target.key, ownerTfLabel: target.key, scope: target.kind, foreignFieldRefsById: ctx.foreignFieldRefsById }
59
- : { type: owner.type, label: owner.label, ownerRefName: owner.ownerRefName, ownerTfLabel: owner.ownerTfLabel, scope: owner.scope, foreignFieldRefsById: ctx.foreignFieldRefsById };
60
+ const targetTfLabel = typeof target !== "string" && (target.kind === "app" || target.kind === "element")
61
+ ? (target.key === owner.ownerRefName ? owner.ownerTfLabel : target.namespace ?? tsLabelToTfLabel(target.key))
62
+ : undefined;
63
+ const filterOwner = targetTfLabel !== undefined && typeof target !== "string" && (target.kind === "app" || target.kind === "element")
64
+ ? {
65
+ type: target.kind === "app" ? "elementum_app" : "elementum_element",
66
+ label: targetTfLabel,
67
+ ownerRefName: target.key,
68
+ ownerTfLabel: targetTfLabel,
69
+ scope: target.kind,
70
+ foreignFieldRefsById: ctx.foreignFieldRefsById,
71
+ }
72
+ : {
73
+ type: owner.type,
74
+ label: owner.label,
75
+ ownerRefName: owner.ownerRefName,
76
+ ownerTfLabel: owner.ownerTfLabel,
77
+ scope: owner.scope,
78
+ foreignFieldRefsById: ctx.foreignFieldRefsById,
79
+ };
60
80
  const filter = body["filter"] === undefined
61
81
  ? undefined
62
82
  : filterFromTf(body["filter"], filterOwner, `${where}.${variant}.filter`);
@@ -89,7 +109,7 @@ function widgetFromTf(block, owner, ctx) {
89
109
  return [];
90
110
  }
91
111
  const rec = raw;
92
- const field = ownerFieldRef(rec["field_id"], owner);
112
+ const field = ownerFieldRef(rec["field_id"], owner, ctx);
93
113
  return [{
94
114
  name: typeof rec["name"] === "string" ? rec["name"] : "",
95
115
  fieldType: typeof rec["field_type"] === "string" ? rec["field_type"] : "TEXT",
@@ -146,7 +166,7 @@ function columnsAndRows(attrs, target, owner, ctx, where) {
146
166
  }
147
167
  function aspectRef(value, owner, ctx) {
148
168
  if (typeof value === "string")
149
- return value;
169
+ return aspectRefFromRawId(value, owner, ctx) ?? value;
150
170
  if (resourceRefLabel(value, owner.type) === owner.label) {
151
171
  return { kind: owner.scope, key: owner.ownerRefName, name: owner.ownerRefName };
152
172
  }
@@ -155,30 +175,84 @@ function aspectRef(value, owner, ctx) {
155
175
  if (resourceLabel !== undefined)
156
176
  return { kind, key: tfLabelToTsLabel(resourceLabel), name: tfLabelToTsLabel(resourceLabel) };
157
177
  const dataLabel = dataRefLabel(value, type);
158
- if (dataLabel !== undefined) {
159
- const name = ctx.dataName(type, dataLabel) ?? tfLabelToTsLabel(dataLabel);
160
- return { kind, key: tfLabelToTsLabel(dataLabel), name };
161
- }
178
+ if (dataLabel !== undefined)
179
+ return { kind, ...resolveDataAspectTarget(ctx, kind, dataLabel) };
162
180
  }
163
181
  return undefined;
164
182
  }
183
+ function aspectRefFromRawId(id, owner, ctx) {
184
+ if (id === "")
185
+ return undefined;
186
+ const imported = ctx.importedResource?.(id);
187
+ if (imported === undefined)
188
+ return undefined;
189
+ if (imported.type === owner.type && imported.label === owner.label) {
190
+ return { kind: owner.scope, key: owner.ownerRefName, name: owner.ownerRefName };
191
+ }
192
+ if (imported.type !== "elementum_app" && imported.type !== "elementum_element")
193
+ return undefined;
194
+ const kind = imported.type === "elementum_app" ? "app" : "element";
195
+ const name = typeof imported.attrs["name"] === "string" ? imported.attrs["name"] : tfLabelToTsLabel(imported.label);
196
+ const namespace = typeof imported.attrs["namespace"] === "string" ? imported.attrs["namespace"] : undefined;
197
+ const refName = namespace !== undefined
198
+ ? (kind === "app" ? ctx.foreignAppRefNamespaces?.get(namespace) : ctx.foreignElementRefNamespaces?.get(namespace))
199
+ : (kind === "app" ? ctx.foreignAppRefNames?.get(name) : ctx.foreignElementRefNames?.get(name));
200
+ return {
201
+ kind,
202
+ key: refName ?? tfLabelToTsLabel(imported.label),
203
+ name,
204
+ ...(namespace !== undefined ? { namespace } : {}),
205
+ ...(refName !== undefined ? { catalogRef: true } : {}),
206
+ };
207
+ }
165
208
  function widgetFieldRef(value, target, owner, ctx) {
166
209
  if (typeof value === "string")
167
- return value;
168
- const targetLabel = typeof target === "string" ? undefined : target.key === owner.ownerRefName ? owner.ownerTfLabel : target.key;
169
- const targetScope = typeof target === "string" || target.kind === "automation" || target.kind === "field" ? owner.scope : target.kind;
210
+ return fieldRefFromRawId(value, target, owner, ctx) ?? value;
211
+ const targetLabel = typeof target === "string"
212
+ ? undefined
213
+ : target.key === owner.ownerRefName
214
+ ? owner.ownerTfLabel
215
+ : target.namespace ?? tsLabelToTfLabel(target.key);
216
+ const targetScope = typeof target === "string" || target.kind === "automation" || target.kind === "field" || target.kind === "recordIdentity" ? owner.scope : target.kind;
170
217
  if (targetLabel !== undefined) {
171
218
  const key = fieldKeyFromTfRef(value, targetLabel, targetScope);
172
219
  if (key !== undefined) {
173
- return { kind: "field", key, name: key, ...(typeof target !== "string" && { parent: target.key }), fieldType: "TEXT" };
220
+ // HANDLE system field (`local.<target>_id_field_id`) round-trips as
221
+ // recordIdentity(target) — same catalog token widgets author with.
222
+ if (key === "id" && typeof target !== "string" && (target.kind === "app" || target.kind === "element")) {
223
+ return { kind: "recordIdentity", key: "id", name: "Record ID", target };
224
+ }
225
+ const fieldType = isTfExpr(value) && value.$expr === "resource-attr"
226
+ ? (SUB_TYPE_BY_RESOURCE.get(value.type)?.authoringType ?? "TEXT")
227
+ : "TEXT";
228
+ return { kind: "field", key, name: key, ...(typeof target !== "string" && { parent: target.key }), fieldType };
174
229
  }
175
230
  }
176
231
  const dataLabel = dataRefLabel(value, "elementum_field");
232
+ if (dataLabel !== undefined && typeof target !== "string") {
233
+ const decoded = decodeForeignDataField(dataLabel, {
234
+ key: target.key,
235
+ name: target.name,
236
+ namespace: target.namespace,
237
+ kind: target.kind,
238
+ }, ctx);
239
+ return {
240
+ kind: "field",
241
+ key: decoded.key,
242
+ name: decoded.name,
243
+ parent: target.key,
244
+ fieldType: decoded.fieldType ?? "TEXT",
245
+ };
246
+ }
177
247
  if (dataLabel !== undefined) {
178
248
  const name = ctx.dataName("elementum_field", dataLabel) ?? tfLabelToTsLabel(dataLabel);
179
- return { kind: "field", key: tfLabelToTsLabel(dataLabel), name, ...(typeof target !== "string" && { parent: target.key }), fieldType: "TEXT" };
249
+ return { kind: "field", key: tfLabelToTsLabel(dataLabel), name, fieldType: "TEXT" };
180
250
  }
181
251
  if (isTfExpr(value) && value.$expr === "resource-attr" && value.path.length === 1 && value.path[0] === "id") {
252
+ const identityTarget = recordIdentityTarget(value.type, target);
253
+ if (identityTarget !== undefined) {
254
+ return { kind: "recordIdentity", key: "id", name: "Record ID", target: identityTarget };
255
+ }
182
256
  const subType = SUB_TYPE_BY_RESOURCE.get(value.type);
183
257
  if (subType !== undefined) {
184
258
  return { kind: "field", key: tfLabelToTsLabel(value.label), name: tfLabelToTsLabel(value.label), ...(typeof target !== "string" && { parent: target.key }), fieldType: subType.authoringType };
@@ -186,6 +260,12 @@ function widgetFieldRef(value, target, owner, ctx) {
186
260
  }
187
261
  return undefined;
188
262
  }
263
+ function recordIdentityTarget(resourceType, target) {
264
+ if (typeof target === "string" || (target.kind !== "app" && target.kind !== "element"))
265
+ return undefined;
266
+ const targetType = target.kind === "app" ? "elementum_app" : "elementum_element";
267
+ return resourceType === WIDGET_RESOURCE_TYPE || resourceType === targetType ? target : undefined;
268
+ }
189
269
  function automationRef(value, owner) {
190
270
  if (typeof value === "string")
191
271
  return value;
@@ -194,16 +274,75 @@ function automationRef(value, owner) {
194
274
  return undefined;
195
275
  return {
196
276
  kind: "automation",
197
- key: scopedLabelToBagKey(owner.ownerTfLabel, label, owner.scope),
198
- name: scopedLabelToBagKey(owner.ownerTfLabel, label, owner.scope),
277
+ // Automations keep the `apps_` prefix on elements; decoding with the
278
+ // widget owner's element scope leaves `apps…` in the bag key.
279
+ key: scopedLabelToBagKey(owner.ownerTfLabel, label, "app"),
280
+ name: scopedLabelToBagKey(owner.ownerTfLabel, label, "app"),
199
281
  parent: owner.ownerRefName,
200
282
  };
201
283
  }
202
- function ownerFieldRef(value, owner) {
284
+ function ownerFieldRef(value, owner, ctx) {
203
285
  if (value === undefined || value === null)
204
286
  return undefined;
205
287
  if (typeof value === "string")
206
- return value;
288
+ return fieldRefFromRawId(value, owner.ownerRefName, owner, ctx) ?? value;
207
289
  const key = fieldKeyFromTfRef(value, owner.ownerTfLabel, owner.scope);
208
290
  return key === undefined ? undefined : { kind: "field", key, name: key, parent: owner.ownerRefName, fieldType: "TEXT" };
209
291
  }
292
+ function fieldRefFromRawId(id, target, owner, ctx) {
293
+ if (id === "")
294
+ return undefined;
295
+ const stateRef = ctx.foreignFieldRefsById?.get(id);
296
+ if (stateRef !== undefined) {
297
+ // runAutomation field_id is owner-scoped (same check as fieldKeyFromTfValue).
298
+ // Widget columns accept any registry hit and keep the field's real parent.
299
+ if (typeof target === "string" && target === owner.ownerRefName && stateRef.ownerRefName !== owner.ownerRefName) {
300
+ return undefined;
301
+ }
302
+ return {
303
+ kind: "field",
304
+ key: stateRef.fieldKey,
305
+ name: stateRef.fieldName ?? stateRef.fieldKey,
306
+ parent: stateRef.ownerRefName,
307
+ fieldType: "TEXT",
308
+ };
309
+ }
310
+ const imported = ctx.importedResource?.(id);
311
+ if (imported === undefined)
312
+ return undefined;
313
+ const subType = SUB_TYPE_BY_RESOURCE.get(imported.type);
314
+ if (subType === undefined)
315
+ return undefined;
316
+ const ownerKey = fieldKeyIfScopedTo(imported.type, imported.label, owner.ownerTfLabel, owner.scope);
317
+ if (ownerKey !== undefined) {
318
+ return {
319
+ kind: "field",
320
+ key: ownerKey,
321
+ name: typeof imported.attrs["name"] === "string" ? imported.attrs["name"] : ownerKey,
322
+ parent: owner.ownerRefName,
323
+ fieldType: subType.authoringType,
324
+ };
325
+ }
326
+ if (typeof target !== "string" && target.kind !== "automation" && target.kind !== "field" && target.kind !== "recordIdentity") {
327
+ const targetTfLabel = target.key === owner.ownerRefName
328
+ ? owner.ownerTfLabel
329
+ : target.namespace ?? tsLabelToTfLabel(target.key);
330
+ const targetKey = fieldKeyIfScopedTo(imported.type, imported.label, targetTfLabel, target.kind);
331
+ if (targetKey !== undefined) {
332
+ return {
333
+ kind: "field",
334
+ key: targetKey,
335
+ name: typeof imported.attrs["name"] === "string" ? imported.attrs["name"] : targetKey,
336
+ parent: target.key,
337
+ fieldType: subType.authoringType,
338
+ };
339
+ }
340
+ }
341
+ return undefined;
342
+ }
343
+ /** `fieldKeyFromTfRef` camelCases any label; only treat it as owned when the scoped prefix was actually stripped. */
344
+ function fieldKeyIfScopedTo(type, label, ownerTfLabel, scope) {
345
+ if (bareChildResourceLabel(ownerTfLabel, label, scope) === label)
346
+ return undefined;
347
+ return fieldKeyFromTfRef({ $expr: "resource-attr", type, label, path: ["id"] }, ownerTfLabel, scope);
348
+ }
@@ -8,6 +8,7 @@ interface RefWire {
8
8
  readonly namespace?: string;
9
9
  readonly parent?: string;
10
10
  readonly fieldType?: string;
11
+ readonly target?: RefWire | string;
11
12
  }
12
13
  interface WidgetParameterWire {
13
14
  readonly name?: string;
@@ -31,6 +32,16 @@ export interface AppWidgetWire {
31
32
  readonly automation?: RefWire | string;
32
33
  readonly parameters?: readonly WidgetParameterWire[];
33
34
  }
35
+ export interface RelationshipTargetWire {
36
+ readonly kind?: string;
37
+ readonly refName?: string;
38
+ }
39
+ export declare function relationshipTargetsFromBag(relationships: Readonly<Record<string, {
40
+ readonly target?: {
41
+ readonly __kind?: string;
42
+ readonly refName?: string;
43
+ };
44
+ }>> | undefined): RelationshipTargetWire[];
34
45
  export interface WidgetsToTfOptions {
35
46
  readonly ownerRefName: string;
36
47
  readonly ownerTfLabel: string;
@@ -41,6 +52,12 @@ export interface WidgetsToTfOptions {
41
52
  readonly workspaceAspects?: ReadonlyMap<string, WorkspaceAspect>;
42
53
  readonly diagnostics: string[];
43
54
  readonly fieldAddresses?: ReadonlyMap<string, string>;
55
+ /**
56
+ * Targets of this owner's authored `relationships[]` joins. A foreign
57
+ * `relatedRecords` widget needs one of these; child `relationField` /
58
+ * `autoRelate` does not populate the widget.
59
+ */
60
+ readonly relationshipTargets?: readonly RelationshipTargetWire[];
44
61
  }
45
62
  export declare function widgetsToTf(widgets: Readonly<Record<string, AppWidgetWire>> | undefined, opts: WidgetsToTfOptions): TfBlock[];
46
63
  export {};
@@ -1,10 +1,17 @@
1
1
  import { workspaceAspectKey } from "../../engine/build/context.js";
2
2
  import { resolveForeignRef } from "../shared/foreign-ref.js";
3
- import { expr, isCanonicalTsLabel, sanitizeName, scopedResourceLabel, tsLabelToTfLabel, } from "../../engine/tf/index.js";
3
+ import { appScopedResourceLabel, expr, isCanonicalTsLabel, sanitizeName, scopedResourceLabel, tfLabelToTsLabel, tsLabelToTfLabel, } from "../../engine/tf/index.js";
4
4
  import { AUTOMATION_RESOURCE_TYPE } from "../automation/types.js";
5
+ import { fieldMetadataFromEntries } from "../field/metadata.js";
5
6
  import { fieldRefToTfValue } from "../field/to-tf.js";
6
7
  import { WIDGET_RESOURCE_TYPE } from "./module.js";
7
8
  import { filterToTf } from "../access-policy/to-tf.js";
9
+ export function relationshipTargetsFromBag(relationships) {
10
+ return Object.values(relationships ?? {}).map((relationship) => ({
11
+ kind: relationship.target?.__kind,
12
+ refName: relationship.target?.refName,
13
+ }));
14
+ }
8
15
  export function widgetsToTf(widgets, opts) {
9
16
  const blocks = [];
10
17
  for (const [key, widget] of Object.entries(widgets ?? {})) {
@@ -19,6 +26,7 @@ export function widgetsToTf(widgets, opts) {
19
26
  const target = resolveAspect(widget.target, `${where}.target`, opts);
20
27
  if (target === undefined)
21
28
  continue;
29
+ diagnoseRelatedRecordsJoin(where, target, opts);
22
30
  const body = { name: widget.name ?? "", object_id: target.value };
23
31
  // Always emitted, even empty — the provider schema treats columns as
24
32
  // always-present on related-object/related-link widgets, so omitting
@@ -86,6 +94,24 @@ export function widgetsToTf(widgets, opts) {
86
94
  }
87
95
  return blocks;
88
96
  }
97
+ function diagnoseRelatedRecordsJoin(where, target, opts) {
98
+ if (target.refName === undefined || target.kind === undefined)
99
+ return;
100
+ const isSelf = target.kind === opts.scope && target.refName === opts.ownerRefName;
101
+ if (isSelf)
102
+ return;
103
+ const matches = (opts.relationshipTargets ?? []).some((relationship) => {
104
+ const kind = relationship.kind === "element"
105
+ ? "element"
106
+ : relationship.kind === "table"
107
+ ? "table"
108
+ : "app";
109
+ return kind === target.kind && relationship.refName === target.refName;
110
+ });
111
+ if (matches)
112
+ return;
113
+ opts.diagnostics.push(`${where}: relatedRecords lists records through an authored relationships[] join on this ${opts.scope}, not through a child relationField or autoRelate. Add relationships.<key> targeting "${target.refName}" whose columns join comparable stored values. Do not join this object's system "id" (HANDLE) to a child relationField.`);
114
+ }
89
115
  function buttonBody(widget) {
90
116
  const body = {
91
117
  name: widget.name ?? "",
@@ -105,12 +131,15 @@ function resolveAspect(value, where, opts) {
105
131
  const kind = value?.__kind === "element" ? "element" : "app";
106
132
  const refName = value?.refName ?? "";
107
133
  if (kind === opts.scope && refName === opts.ownerRefName) {
108
- return { value: opts.objectId, kind, refName, tfLabel: opts.ownerTfLabel, managed: true };
134
+ return { value: opts.objectId, kind, refName, tfLabel: opts.ownerTfLabel, name: value?.name ?? refName, managed: true };
109
135
  }
110
136
  const sibling = opts.workspaceAspects?.get(workspaceAspectKey(kind, refName));
111
137
  if (sibling !== undefined) {
112
138
  const type = kind === "app" ? "elementum_app" : "elementum_element";
113
- return { value: expr.resourceAttr(type, sibling.tfLabel, ["id"]), kind, refName, tfLabel: sibling.tfLabel, managed: true };
139
+ const payloadName = typeof sibling.streamRootEntity?.payload === "object" && sibling.streamRootEntity.payload !== null
140
+ ? sibling.streamRootEntity.payload.name
141
+ : undefined;
142
+ return { value: expr.resourceAttr(type, sibling.tfLabel, ["id"]), kind, refName, tfLabel: sibling.tfLabel, name: value?.name ?? payloadName, managed: true };
114
143
  }
115
144
  const name = value?.name ?? refName;
116
145
  if (name === "") {
@@ -124,14 +153,52 @@ function resolveAspect(value, where, opts) {
124
153
  kind,
125
154
  refName,
126
155
  tfLabel: foreign.tfLabel,
156
+ name,
127
157
  managed: false,
128
158
  };
129
159
  }
160
+ /**
161
+ * listView-parity system column: managed target → `local.<tfLabel>_<key>_field_id`.
162
+ * Returns the TF value when `key` is a system field on this managed target;
163
+ * otherwise undefined (caller treats non-system strings as raw platform ids).
164
+ */
165
+ function resolveManagedSystemColumn(key, target) {
166
+ if (!target.managed || target.tfLabel === undefined || target.kind === undefined)
167
+ return undefined;
168
+ return fieldRefToTfValue("HANDLE", key, target.tfLabel, target.kind);
169
+ }
130
170
  function resolveColumns(columns, target, where, opts) {
131
171
  const out = [];
132
172
  for (const [index, value] of (columns ?? []).entries()) {
133
173
  if (typeof value === "string") {
134
- out.push(value);
174
+ // Bare system keys ("id", "title", …) resolve to the handle/title field
175
+ // id — same as listView — never the aspect resource `.id`. Other strings
176
+ // stay as raw platform field ids (extract / adopt).
177
+ const systemColumn = resolveManagedSystemColumn(value, target);
178
+ out.push(systemColumn ?? value);
179
+ continue;
180
+ }
181
+ if (value != null && value.__kind === "recordIdentity") {
182
+ if (value.target === undefined) {
183
+ opts.diagnostics.push(`${where}[${index}]: record identity has no target; dropped`);
184
+ continue;
185
+ }
186
+ const identityTarget = resolveAspect(value.target, `${where}[${index}].target`, opts);
187
+ if (identityTarget === undefined)
188
+ continue;
189
+ if (target.kind !== identityTarget.kind || target.refName !== identityTarget.refName) {
190
+ opts.diagnostics.push(`${where}[${index}]: record identity target does not match widget target; dropped`);
191
+ continue;
192
+ }
193
+ // Platform widget columns expect field UUIDs. `recordIdentity` is the
194
+ // HANDLE system field — emit local.<target>_id_field_id like listView's
195
+ // `"id"` column, never the aspect resource `.id` (GraphQL rejects that).
196
+ const handleColumn = resolveManagedSystemColumn("id", identityTarget);
197
+ if (handleColumn === undefined) {
198
+ opts.diagnostics.push(`${where}[${index}]: record identity needs a managed app/element target so the handle field id is available; dropped`);
199
+ continue;
200
+ }
201
+ out.push(handleColumn);
135
202
  continue;
136
203
  }
137
204
  if (value == null) {
@@ -148,7 +215,8 @@ function resolveColumns(columns, target, where, opts) {
148
215
  continue;
149
216
  }
150
217
  if (target.managed && target.kind !== undefined && target.tfLabel !== undefined) {
151
- const direct = fieldRefToTfValue(value.fieldType ?? "", refName, target.tfLabel, target.kind);
218
+ const column = resolveManagedColumn(value, refName, target, opts);
219
+ const direct = fieldRefToTfValue(column.fieldType, column.refName, target.tfLabel, target.kind);
152
220
  if (direct !== undefined) {
153
221
  out.push(direct);
154
222
  continue;
@@ -216,7 +284,83 @@ function resolveAutomation(value, where, opts) {
216
284
  opts.diagnostics.push(`${where}: foreign automation references require a provider data lookup with app_id; use a raw id until that data-source tranche lands`);
217
285
  return undefined;
218
286
  }
219
- return expr.resourceAttr(AUTOMATION_RESOURCE_TYPE, scopedResourceLabel(opts.ownerTfLabel, tsLabelToTfLabel(refName), opts.scope), ["id"]);
287
+ // Automations always use `appScopedResourceLabel` (`apps_<owner>_…`), even
288
+ // when the owner is an element. `scopedResourceLabel(..., opts.scope)` would
289
+ // mint `elements_…` here and plan an undeclared resource.
290
+ return expr.resourceAttr(AUTOMATION_RESOURCE_TYPE, appScopedResourceLabel(opts.ownerTfLabel, tsLabelToTfLabel(refName)), ["id"]);
291
+ }
292
+ function resolveManagedColumn(value, refName, target, opts) {
293
+ const meta = siblingFieldMetadata(target, opts);
294
+ const recovered = recoverManagedColumnRef(refName, value, target, meta);
295
+ return {
296
+ refName: recovered,
297
+ fieldType: meta.get(recovered)?.fieldType ?? value.fieldType ?? "",
298
+ };
299
+ }
300
+ function siblingFieldMetadata(target, opts) {
301
+ const out = new Map();
302
+ if (target.kind === undefined || target.refName === undefined)
303
+ return out;
304
+ const sibling = opts.workspaceAspects?.get(workspaceAspectKey(target.kind, target.refName));
305
+ const payload = sibling?.streamRootEntity?.payload;
306
+ for (const [key, info] of fieldMetadataFromEntries(payload?.fields)) {
307
+ out.set(key, { fieldType: info.fieldType, name: info.name });
308
+ }
309
+ if (target.kind === "element") {
310
+ if (!out.has("title"))
311
+ out.set("title", { fieldType: "TEXT", name: "Title" });
312
+ if (!out.has("id"))
313
+ out.set("id", { fieldType: "HANDLE", name: "ID" });
314
+ }
315
+ return out;
316
+ }
317
+ function recoverManagedColumnRef(refName, value, target, meta) {
318
+ if (meta.has(refName))
319
+ return refName;
320
+ if (target.kind === "element" && (refName === "title" || refName === "id"))
321
+ return refName;
322
+ let tf;
323
+ try {
324
+ tf = tsLabelToTfLabel(refName);
325
+ }
326
+ catch {
327
+ return refName;
328
+ }
329
+ for (const prefix of managedColumnOwnerPrefixes(target, value)) {
330
+ if (tf.startsWith(prefix) && tf.length > prefix.length) {
331
+ const key = tfLabelToTsLabel(tf.slice(prefix.length));
332
+ if (meta.has(key) || (target.kind === "element" && (key === "title" || key === "id")))
333
+ return key;
334
+ }
335
+ }
336
+ const display = value.name;
337
+ if (display !== undefined) {
338
+ const lower = display.toLowerCase();
339
+ for (const [key, info] of meta) {
340
+ if (info.name.toLowerCase() === lower)
341
+ return key;
342
+ }
343
+ }
344
+ return refName;
345
+ }
346
+ function managedColumnOwnerPrefixes(target, _value) {
347
+ const kind = target.kind;
348
+ const ownerType = kind === "element" ? "elementum_element" : kind === "app" ? "elementum_app" : undefined;
349
+ const slugs = [
350
+ target.name !== undefined && target.name !== "" ? sanitizeName(target.name) : undefined,
351
+ target.tfLabel,
352
+ target.refName !== undefined && target.refName !== "" ? tsLabelToTfLabel(target.refName) : undefined,
353
+ target.refName !== undefined && target.refName !== "" ? sanitizeName(target.refName) : undefined,
354
+ ].filter((slug) => typeof slug === "string" && slug !== "");
355
+ const prefixes = [];
356
+ for (const slug of slugs) {
357
+ if (ownerType !== undefined)
358
+ prefixes.push(`${ownerType}_${slug}_`);
359
+ if (kind !== undefined)
360
+ prefixes.push(`${kind}_${slug}_`);
361
+ prefixes.push(`${slug}_`);
362
+ }
363
+ return [...new Set(prefixes)].sort((a, b) => b.length - a.length);
220
364
  }
221
365
  function resolveOwnerField(value, where, opts) {
222
366
  if (typeof value === "string")
@@ -329,6 +329,11 @@ export interface PrintOwnFileOptions {
329
329
  readonly ownerRefName: string;
330
330
  readonly fieldKey: string;
331
331
  }>;
332
+ /** Platform table id → canonical table catalog identity. */
333
+ readonly foreignTableRefsById?: ReadonlyMap<string, {
334
+ readonly tableRefName: string;
335
+ readonly name: string;
336
+ }>;
332
337
  /**
333
338
  * `JSON.stringify([aspect display name, search-table display name])` →
334
339
  * pulled catalog identity. Names are scoped by aspect to avoid collisions.
@@ -362,12 +367,20 @@ export interface BuildChildContext {
362
367
  * catalog (#490).
363
368
  */
364
369
  readonly fieldNamesByAppRefName?: ReadonlyMap<string, ReadonlyMap<string, string>>;
370
+ /**
371
+ * Field resource addresses for every app/element in this build, keyed the
372
+ * same way as {@link fieldNamesByAppRefName}. A calculation addresses a
373
+ * found record's field by field id, so a cross-app `recordSearch` output
374
+ * used in a formula needs the searched aspect's field resources.
375
+ */
376
+ readonly fieldAddressesByAppRefName?: ReadonlyMap<string, ReadonlyMap<string, string>>;
365
377
  /** Pulled roots may retain picklist labels removed from the live field. */
366
378
  readonly allowUnknownPicklistOptions?: boolean;
367
379
  /** Every app/element/table stream root in this build, by refName (see `ToTfContext.workspaceAspects`). */
368
380
  readonly workspaceAspects?: ReadonlyMap<string, {
369
381
  readonly kind: ChildResourceScope | "table";
370
382
  readonly tfLabel: string;
383
+ readonly streamRootEntity?: DeployableEntity;
371
384
  }>;
372
385
  /** The current app/element/table entity, forwarded for owner-local reference resolution. */
373
386
  readonly streamRootEntity?: DeployableEntity;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elementumai/edk",
3
- "version": "0.9.10",
3
+ "version": "0.9.12",
4
4
  "description": "Elementum Development Kit — statically typed SDK for building Elementum apps, automations, and agents",
5
5
  "type": "module",
6
6
  "license": "SEE LICENSE IN LICENSE",
@@ -1,2 +0,0 @@
1
- /** Re-export — canonical home is `src/entities/shared/types/platform.ts`. */
2
- export type { Attachment, Employee, Group, Manager, DropdownValue, DynamicDropdownValue, RecordId, RichText, User, } from "../../entities/shared/types/platform.js";
@@ -1 +0,0 @@
1
- export {};