@elementumai/edk 0.10.4 → 0.12.0

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 (171) hide show
  1. package/README.md +49 -13
  2. package/dist-sdk/src/catalog/token.js +2 -1
  3. package/dist-sdk/src/core/triggers/on-demand.d.ts +2 -0
  4. package/dist-sdk/src/core/triggers/on-demand.js +3 -0
  5. package/dist-sdk/src/core/wire/ref-serializer.js +26 -1
  6. package/dist-sdk/src/engine/build/context.d.ts +4 -0
  7. package/dist-sdk/src/engine/coverage-gaps.d.ts +7 -0
  8. package/dist-sdk/src/engine/coverage-gaps.js +12 -0
  9. package/dist-sdk/src/engine/ei/json-stream-reader.d.ts +43 -0
  10. package/dist-sdk/src/engine/ei/json-stream-reader.js +234 -0
  11. package/dist-sdk/src/engine/ei/run.d.ts +42 -1
  12. package/dist-sdk/src/engine/ei/run.js +201 -32
  13. package/dist-sdk/src/engine/ei/saved-plan.d.ts +16 -0
  14. package/dist-sdk/src/engine/ei/saved-plan.js +19 -0
  15. package/dist-sdk/src/engine/ei/state.d.ts +1 -1
  16. package/dist-sdk/src/engine/ei/state.js +17 -4
  17. package/dist-sdk/src/engine/progress.d.ts +3 -1
  18. package/dist-sdk/src/engine/progress.js +7 -3
  19. package/dist-sdk/src/engine/pull/authored-address.d.ts +15 -1
  20. package/dist-sdk/src/engine/pull/authored-address.js +180 -29
  21. package/dist-sdk/src/engine/pull/context.d.ts +37 -2
  22. package/dist-sdk/src/engine/pull/context.js +7 -4
  23. package/dist-sdk/src/engine/pull/ref-only.d.ts +18 -0
  24. package/dist-sdk/src/engine/pull/ref-only.js +52 -0
  25. package/dist-sdk/src/engine/tf/index.d.ts +2 -2
  26. package/dist-sdk/src/engine/tf/index.js +2 -2
  27. package/dist-sdk/src/engine/tf/labels.d.ts +22 -0
  28. package/dist-sdk/src/engine/tf/labels.js +50 -0
  29. package/dist-sdk/src/engine/tf/model.d.ts +2 -0
  30. package/dist-sdk/src/engine/tf/model.js +26 -0
  31. package/dist-sdk/src/engine/validation.d.ts +2 -0
  32. package/dist-sdk/src/entities/access-policy/from-tf.d.ts +18 -0
  33. package/dist-sdk/src/entities/access-policy/from-tf.js +58 -24
  34. package/dist-sdk/src/entities/access-policy/member-list.d.ts +32 -0
  35. package/dist-sdk/src/entities/access-policy/member-list.js +57 -0
  36. package/dist-sdk/src/entities/access-policy/module.js +10 -0
  37. package/dist-sdk/src/entities/access-policy/to-tf.d.ts +4 -3
  38. package/dist-sdk/src/entities/access-policy/to-tf.js +21 -12
  39. package/dist-sdk/src/entities/agent/build.js +7 -5
  40. package/dist-sdk/src/entities/agent/deserializer/print-agent-v2.js +2 -0
  41. package/dist-sdk/src/entities/agent/from-tf.js +13 -3
  42. package/dist-sdk/src/entities/agent/to-tf.js +11 -5
  43. package/dist-sdk/src/entities/agent/tools.d.ts +8 -0
  44. package/dist-sdk/src/entities/agent/tools.js +6 -2
  45. package/dist-sdk/src/entities/agent-tool/from-tf.d.ts +2 -0
  46. package/dist-sdk/src/entities/agent-tool/to-tf.d.ts +29 -0
  47. package/dist-sdk/src/entities/agent-tool/to-tf.js +56 -3
  48. package/dist-sdk/src/entities/agent-tool/types.d.ts +12 -8
  49. package/dist-sdk/src/entities/agent-tool/types.js +13 -9
  50. package/dist-sdk/src/entities/app/build.d.ts +12 -1
  51. package/dist-sdk/src/entities/app/build.js +80 -4
  52. package/dist-sdk/src/entities/app/deserializer/normalize.d.ts +5 -7
  53. package/dist-sdk/src/entities/app/from-tf.js +17 -12
  54. package/dist-sdk/src/entities/app/stage-keys.d.ts +18 -3
  55. package/dist-sdk/src/entities/app/stage-keys.js +40 -6
  56. package/dist-sdk/src/entities/app/to-tf.js +7 -4
  57. package/dist-sdk/src/entities/automation/build.d.ts +52 -31
  58. package/dist-sdk/src/entities/automation/build.js +59 -20
  59. package/dist-sdk/src/entities/automation/deserializer/ast.d.ts +7 -4
  60. package/dist-sdk/src/entities/automation/deserializer/print-automation.js +39 -14
  61. package/dist-sdk/src/entities/automation/from-tf.d.ts +35 -2
  62. package/dist-sdk/src/entities/automation/from-tf.js +721 -93
  63. package/dist-sdk/src/entities/automation/index.d.ts +1 -1
  64. package/dist-sdk/src/entities/automation/module.d.ts +22 -2
  65. package/dist-sdk/src/entities/automation/module.js +244 -57
  66. package/dist-sdk/src/entities/automation/output-key-wire-name.d.ts +22 -2
  67. package/dist-sdk/src/entities/automation/output-key-wire-name.js +54 -3
  68. package/dist-sdk/src/entities/automation/run-target.d.ts +61 -0
  69. package/dist-sdk/src/entities/automation/run-target.js +104 -0
  70. package/dist-sdk/src/entities/automation/script-code.d.ts +11 -1
  71. package/dist-sdk/src/entities/automation/script-code.js +106 -4
  72. package/dist-sdk/src/entities/automation/script-lint.d.ts +13 -0
  73. package/dist-sdk/src/entities/automation/script-lint.js +418 -0
  74. package/dist-sdk/src/entities/automation/task-output-labels.d.ts +17 -1
  75. package/dist-sdk/src/entities/automation/task-output-labels.js +58 -1
  76. package/dist-sdk/src/entities/automation/tasks.d.ts +67 -22
  77. package/dist-sdk/src/entities/automation/tasks.js +18 -4
  78. package/dist-sdk/src/entities/automation/to-tf.d.ts +1 -3
  79. package/dist-sdk/src/entities/automation/to-tf.js +354 -64
  80. package/dist-sdk/src/entities/automation/triggers.d.ts +63 -11
  81. package/dist-sdk/src/entities/automation/triggers.js +45 -5
  82. package/dist-sdk/src/entities/automation/types.d.ts +21 -0
  83. package/dist-sdk/src/entities/automation/types.js +26 -0
  84. package/dist-sdk/src/entities/chart/build.d.ts +9 -1
  85. package/dist-sdk/src/entities/chart/from-tf.d.ts +2 -2
  86. package/dist-sdk/src/entities/chart/from-tf.js +25 -1
  87. package/dist-sdk/src/entities/chart/index.d.ts +1 -1
  88. package/dist-sdk/src/entities/chart/printer.js +5 -2
  89. package/dist-sdk/src/entities/chart/to-tf.js +19 -1
  90. package/dist-sdk/src/entities/dashboard/from-tf.d.ts +11 -1
  91. package/dist-sdk/src/entities/dashboard/from-tf.js +57 -7
  92. package/dist-sdk/src/entities/dashboard/to-tf.js +13 -0
  93. package/dist-sdk/src/entities/dynamic-layout/build.d.ts +8 -2
  94. package/dist-sdk/src/entities/dynamic-layout/condition-keys.d.ts +48 -0
  95. package/dist-sdk/src/entities/dynamic-layout/condition-keys.js +261 -0
  96. package/dist-sdk/src/entities/dynamic-layout/from-tf.d.ts +3 -0
  97. package/dist-sdk/src/entities/dynamic-layout/from-tf.js +66 -20
  98. package/dist-sdk/src/entities/dynamic-layout/module.js +2 -1
  99. package/dist-sdk/src/entities/dynamic-layout/to-tf.d.ts +4 -2
  100. package/dist-sdk/src/entities/dynamic-layout/to-tf.js +341 -32
  101. package/dist-sdk/src/entities/element/build.d.ts +10 -0
  102. package/dist-sdk/src/entities/element/build.js +1 -0
  103. package/dist-sdk/src/entities/element/from-tf.js +14 -9
  104. package/dist-sdk/src/entities/element/to-tf.js +7 -4
  105. package/dist-sdk/src/entities/field/from-tf.d.ts +24 -0
  106. package/dist-sdk/src/entities/field/from-tf.js +79 -1
  107. package/dist-sdk/src/entities/field/module.js +14 -1
  108. package/dist-sdk/src/entities/field/to-tf.d.ts +38 -3
  109. package/dist-sdk/src/entities/field/to-tf.js +108 -34
  110. package/dist-sdk/src/entities/file-reader/module.js +3 -3
  111. package/dist-sdk/src/entities/file-reader/to-tf.js +3 -3
  112. package/dist-sdk/src/entities/file-reader/types.d.ts +4 -16
  113. package/dist-sdk/src/entities/file-reader/types.js +7 -0
  114. package/dist-sdk/src/entities/org/group.d.ts +1 -0
  115. package/dist-sdk/src/entities/org/group.js +2 -1
  116. package/dist-sdk/src/entities/org/role.js +5 -0
  117. package/dist-sdk/src/entities/record-create-form/from-tf.d.ts +8 -0
  118. package/dist-sdk/src/entities/record-create-form/from-tf.js +10 -2
  119. package/dist-sdk/src/entities/record-create-form/to-tf.d.ts +2 -1
  120. package/dist-sdk/src/entities/record-create-form/to-tf.js +14 -2
  121. package/dist-sdk/src/entities/relationship/from-tf.d.ts +8 -0
  122. package/dist-sdk/src/entities/relationship/from-tf.js +88 -10
  123. package/dist-sdk/src/entities/relationship/to-tf.d.ts +3 -2
  124. package/dist-sdk/src/entities/relationship/to-tf.js +79 -8
  125. package/dist-sdk/src/entities/role/from-tf.js +24 -7
  126. package/dist-sdk/src/entities/role/to-tf.js +25 -7
  127. package/dist-sdk/src/entities/service-account/from-tf.js +2 -1
  128. package/dist-sdk/src/entities/service-account/module.js +20 -0
  129. package/dist-sdk/src/entities/service-account/role-fidelity.d.ts +5 -5
  130. package/dist-sdk/src/entities/service-account/role-fidelity.js +21 -13
  131. package/dist-sdk/src/entities/service-account/to-tf.js +12 -4
  132. package/dist-sdk/src/entities/shared/filters/ast.d.ts +2 -0
  133. package/dist-sdk/src/entities/shared/filters/tf-codec.d.ts +1 -1
  134. package/dist-sdk/src/entities/shared/filters/tf-codec.js +12 -7
  135. package/dist-sdk/src/entities/shared/types/field-types.d.ts +6 -3
  136. package/dist-sdk/src/entities/shared/types/field-types.js +3 -1
  137. package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.d.ts +5 -2
  138. package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.js +8 -1
  139. package/dist-sdk/src/entities/skill/from-tf.js +10 -3
  140. package/dist-sdk/src/entities/skill/module.js +18 -0
  141. package/dist-sdk/src/entities/skill/to-tf.d.ts +8 -1
  142. package/dist-sdk/src/entities/skill/to-tf.js +23 -14
  143. package/dist-sdk/src/entities/skill/tools.d.ts +8 -0
  144. package/dist-sdk/src/entities/skill/tools.js +6 -2
  145. package/dist-sdk/src/entities/skill-tool/from-tf.d.ts +2 -0
  146. package/dist-sdk/src/entities/skill-tool/to-tf.d.ts +8 -0
  147. package/dist-sdk/src/entities/skill-tool/to-tf.js +6 -2
  148. package/dist-sdk/src/entities/task/to-tf.js +1 -0
  149. package/dist-sdk/src/entities/view/agent-view-layout-from-tf.d.ts +8 -2
  150. package/dist-sdk/src/entities/view/agent-view-layout-from-tf.js +20 -10
  151. package/dist-sdk/src/entities/view/from-tf.d.ts +68 -4
  152. package/dist-sdk/src/entities/view/from-tf.js +394 -22
  153. package/dist-sdk/src/entities/view/module.js +20 -0
  154. package/dist-sdk/src/entities/view/portable.d.ts +11 -0
  155. package/dist-sdk/src/entities/view/portable.js +47 -0
  156. package/dist-sdk/src/entities/view/to-tf.d.ts +4 -1
  157. package/dist-sdk/src/entities/view/to-tf.js +25 -7
  158. package/dist-sdk/src/entities/view/types.js +1 -1
  159. package/dist-sdk/src/entities/widget/build.d.ts +4 -3
  160. package/dist-sdk/src/entities/widget/from-tf.d.ts +3 -0
  161. package/dist-sdk/src/entities/widget/from-tf.js +100 -3
  162. package/dist-sdk/src/entities/widget/to-tf.d.ts +16 -1
  163. package/dist-sdk/src/entities/widget/to-tf.js +77 -14
  164. package/dist-sdk/src/generated/ragnarok/ragnarok-spec.d.ts +30 -9
  165. package/dist-sdk/src/generated/ragnarok/ragnarok-spec.js +2 -2
  166. package/dist-sdk/src/graph/field-resource-types.d.ts +2 -0
  167. package/dist-sdk/src/graph/field-resource-types.js +11 -0
  168. package/dist-sdk/src/graph/index.d.ts +41 -3
  169. package/dist-sdk/src/graph/index.js +24 -2
  170. package/dist-sdk/src/graph/types.d.ts +195 -5
  171. package/package.json +1 -1
@@ -0,0 +1,104 @@
1
+ import { expr, isUuid } from "../../engine/tf/index.js";
2
+ import { isOnDemandRecordType } from "../shared/types/field-types.js";
3
+ import { AUTOMATION_RESOURCE_TYPE, WORKFLOW_PUBLISH_RESOURCE_TYPE, automationResourceLabel } from "./types.js";
4
+ /**
5
+ * The app or element that owns an automation token, and whether that is the
6
+ * building automation's own app or element. A token with no `parent` is the
7
+ * caller's own. A same-named automation in another app or element is a
8
+ * different automation (edk#2118).
9
+ */
10
+ export function automationTargetOwner(value, ctx) {
11
+ const ownerKind = value.parentKind ?? ctx.parentScope;
12
+ const isOwn = value.parent === undefined ||
13
+ (value.parent === ctx.appRefName && ownerKind === ctx.parentScope);
14
+ return { ownerKind, isOwn };
15
+ }
16
+ /**
17
+ * A bare platform-id target whose every input names a raw parameter id is a
18
+ * static run: pull prints a static export with an undiscovered target
19
+ * (`target_automation_id` + `input_mappings[].parameter_id`) as
20
+ * `automation: "<uuid>"` + `inputs[].name: "<uuid>"`. Dynamic mappings name
21
+ * the target's inputs, never a parameter id.
22
+ */
23
+ export function bareStaticRunTarget(config) {
24
+ return typeof config.targetAutomationId === "string" &&
25
+ Array.isArray(config.inputMappings) &&
26
+ config.inputMappings.length > 0 &&
27
+ config.inputMappings.every((mapping) => mapping !== null && typeof mapping === "object" && "name" in mapping && typeof mapping.name === "string" && isUuid(mapping.name));
28
+ }
29
+ /** The onDemand trigger's record parameters: they expose only a record ID. */
30
+ export function onDemandRecordParamNames(trigger) {
31
+ const inputs = trigger?.onDemand?.inputs;
32
+ if (!Array.isArray(inputs))
33
+ return new Set();
34
+ return new Set(inputs.flatMap((input) => {
35
+ const param = input;
36
+ return typeof param?.name === "string" && isOnDemandRecordType(typeof param.type === "string" ? param.type : undefined) ? [param.name] : [];
37
+ }));
38
+ }
39
+ /** An automation payload's on-demand record inputs, for a static runAutomation caller's check (edk#1868). */
40
+ export function automationOnDemandRecordInputs(payload) {
41
+ return onDemandRecordParamNames(payload?.trigger);
42
+ }
43
+ /**
44
+ * edk#1868: a static runAutomation mapping into the target's on-demand record
45
+ * input reaches the target as no record, so every task reading it fails at
46
+ * run time. Returns the problem, naming the text-handle workaround, or
47
+ * undefined. `recordInputNames` is known only for a target this build owns.
48
+ */
49
+ export function recordInputMappingProblem(target, mappings, recordInputNames) {
50
+ if (recordInputNames === undefined || recordInputNames.size === 0)
51
+ return undefined;
52
+ const mapped = [...new Set(mappings.flatMap((mapping) => {
53
+ const name = mapping?.name;
54
+ return typeof name === "string" && recordInputNames.has(name) ? [name] : [];
55
+ }))];
56
+ if (mapped.length === 0)
57
+ return undefined;
58
+ const label = `${target.name !== undefined && target.name !== "" ? `"${target.name}" ` : ""}(${target.parent !== undefined ? `${target.parent}.` : ""}${target.refName})`;
59
+ const plural = mapped.length > 1;
60
+ return `maps automation ${label}'s on-demand record input${plural ? "s" : ""} ${mapped.map((name) => `"${name}"`).join(", ")}, ` +
61
+ `but the platform passes no record through a runAutomation input: the called automation's tasks that read ${plural ? "them" : "it"} ` +
62
+ `fail at run time ("missing workflow input parameter"). Make ${plural ? "each" : "it"} a text input, pass the record's handle as text, ` +
63
+ "and look the record up with a recordSearch in the called automation.";
64
+ }
65
+ /** Run-automation targets reference publish for ACTIVE automations, automation for draft/inactive. */
66
+ export function automationRunTargetResourceAttr(appTfLabel, automationRefName, activeAutomationRefNames, scope = "app", handle) {
67
+ const label = automationResourceLabel(appTfLabel, automationRefName, scope, handle);
68
+ if (activeAutomationRefNames?.has(automationRefName) === true) {
69
+ return expr.resourceAttr(WORKFLOW_PUBLISH_RESOURCE_TYPE, label, ["id"]);
70
+ }
71
+ return expr.resourceAttr(AUTOMATION_RESOURCE_TYPE, label, ["id"]);
72
+ }
73
+ const AUTOMATION_DECL = new RegExp(`^resource\\s+"${AUTOMATION_RESOURCE_TYPE}"\\s+"([^"]+)"`, "m");
74
+ const PUBLISH_DECL = new RegExp(`^resource\\s+"${WORKFLOW_PUBLISH_RESOURCE_TYPE}"\\s+"([^"]+)"`, "gm");
75
+ const PUBLISH_REF = new RegExp(`(?<![\\w.])${WORKFLOW_PUBLISH_RESOURCE_TYPE}\\.([a-z0-9_]+)`, "g");
76
+ /** The publish resource addresses a compiled automation file declares (its own). */
77
+ export function automationPublishDeclarations(text) {
78
+ return [...text.matchAll(PUBLISH_DECL)].map((match) => `${WORKFLOW_PUBLISH_RESOURCE_TYPE}.${match[1]}`);
79
+ }
80
+ /** The `elementum_automation` label a compiled automation file declares. */
81
+ export function automationDeclaration(text) {
82
+ return AUTOMATION_DECL.exec(text)?.[1];
83
+ }
84
+ /**
85
+ * The publish addresses of a compiled automation file's static
86
+ * `runAutomation` targets. A static call to an ACTIVE target names the
87
+ * target's publish: the run task and the caller's publish `depends_on` it
88
+ * (own app or element, `automationToTf`), and the target lookup
89
+ * `data.<automation>.<target>_for_<caller>` `depends_on` it (another app or
90
+ * element in the workspace, `automationTargetToTf`). Nothing else in an
91
+ * automation file names another automation's publish. A target outside the
92
+ * workspace (a lookup by name) or one that is not ACTIVE names no publish and
93
+ * is not tracked.
94
+ */
95
+ export function staticRunTargets(text) {
96
+ const own = new Set(automationPublishDeclarations(text));
97
+ const targets = new Set();
98
+ for (const match of text.matchAll(PUBLISH_REF)) {
99
+ const address = `${WORKFLOW_PUBLISH_RESOURCE_TYPE}.${match[1]}`;
100
+ if (!own.has(address))
101
+ targets.add(address);
102
+ }
103
+ return [...targets].sort();
104
+ }
@@ -78,7 +78,8 @@ export declare function stripTsIgnoreLines(code: string): string;
78
78
  * half of what the `code` function's TS return type already gives the type
79
79
  * system. Same best-effort contract as the closure scan:
80
80
  *
81
- * - every `return`ed object literal contributes its top-level keys; multiple
81
+ * - every top-level `return`ed object literal contributes its top-level keys
82
+ * (a return inside a callback or nested function is skipped); multiple
82
83
  * returns merge (a key typed differently across branches widens to "text")
83
84
  * - value types come from LITERAL values only: string/template → "text",
84
85
  * integral number → "integer", other number → "number", true/false →
@@ -114,5 +115,14 @@ export declare function freeIdentifiersLexer(body: string, params: ReadonlySet<s
114
115
  * there's no tracked source (not running under `elementum build`, e.g. an
115
116
  * automation constructed directly in a test) or the name/shape wasn't found
116
117
  * by the scan (falls back to `extractScriptBody`).
118
+ *
119
+ * A body that is not plain JavaScript is also `undefined` (#1927). The
120
+ * verbatim text exists so a pulled body, which is always JavaScript,
121
+ * round-trips byte for byte. A body with type annotations was written by
122
+ * hand, and the runtime that loaded the file has already erased its types
123
+ * from the function itself, so `extractScriptBody` gives the same result in
124
+ * every workspace instead of depending on whether a usable TypeScript
125
+ * compiler happens to resolve. It also runs the closure check, which this
126
+ * path skips.
117
127
  */
118
128
  export declare function tryExtractFromCurrentSource(name: string): string | undefined;
@@ -7,7 +7,8 @@ let packageTsModule;
7
7
  function getTs() {
8
8
  if (packageTsModule === undefined) {
9
9
  try {
10
- packageTsModule = createRequire(import.meta.url)("typescript");
10
+ const loaded = createRequire(import.meta.url)("typescript");
11
+ packageTsModule = hasCompilerApi(loaded) ? loaded : null;
11
12
  }
12
13
  catch {
13
14
  packageTsModule = null;
@@ -26,7 +27,9 @@ function getTs() {
26
27
  ];
27
28
  for (const root of roots) {
28
29
  try {
29
- return createRequire(root)("typescript");
30
+ const loaded = createRequire(root)("typescript");
31
+ if (hasCompilerApi(loaded))
32
+ return loaded;
30
33
  }
31
34
  catch {
32
35
  // Try the next legitimate module root.
@@ -34,6 +37,14 @@ function getTs() {
34
37
  }
35
38
  return undefined;
36
39
  }
40
+ /**
41
+ * TypeScript 7's package entry exposes only its version, not the compiler
42
+ * API (#1927). Treat that package as no TypeScript rather than calling
43
+ * functions it does not have.
44
+ */
45
+ function hasCompilerApi(ts) {
46
+ return typeof ts?.transpileModule === "function" && typeof ts.createSourceFile === "function";
47
+ }
37
48
  /**
38
49
  * Reference a plain-JavaScript script file for `executeScript`'s `code:`.
39
50
  * The path is relative to the automation file that calls it.
@@ -286,7 +297,8 @@ function collectParamNames(params) {
286
297
  * half of what the `code` function's TS return type already gives the type
287
298
  * system. Same best-effort contract as the closure scan:
288
299
  *
289
- * - every `return`ed object literal contributes its top-level keys; multiple
300
+ * - every top-level `return`ed object literal contributes its top-level keys
301
+ * (a return inside a callback or nested function is skipped); multiple
290
302
  * returns merge (a key typed differently across branches widens to "text")
291
303
  * - value types come from LITERAL values only: string/template → "text",
292
304
  * integral number → "integer", other number → "number", true/false →
@@ -301,6 +313,18 @@ function collectParamNames(params) {
301
313
  * with `${}` nesting) so braces inside them don't derail the literal walk.
302
314
  */
303
315
  export function inferOutputSchema(body) {
316
+ let topLevel = topLevelReturns(body);
317
+ if (topLevel === undefined) {
318
+ // A stored TypeScript body (a scriptFile keeps its types): scan the
319
+ // type-stripped body the #2286 lint also reads, so both see the same
320
+ // top-level returns. A body that still doesn't parse reads every return.
321
+ const stripped = typeStrippedBody(body);
322
+ if (stripped !== undefined) {
323
+ topLevel = topLevelReturns(stripped);
324
+ if (topLevel !== undefined)
325
+ body = stripped;
326
+ }
327
+ }
304
328
  const merged = {};
305
329
  let found = false;
306
330
  const src = body;
@@ -318,11 +342,21 @@ export function inferOutputSchema(body) {
318
342
  if (src.startsWith("return", i) &&
319
343
  !/[A-Za-z0-9_$]/.test(src[i + 6] ?? "") &&
320
344
  !/[A-Za-z0-9_$.]/.test(src[i - 1] ?? "")) {
345
+ const returned = topLevel?.get(i);
346
+ if (topLevel !== undefined && (returned === undefined || returned === "nothing")) {
347
+ // A return inside a callback or nested function is not the script's
348
+ // result, and a bare `return` (including `return` followed by a
349
+ // newline and `{`, which automatic semicolon insertion ends) sets no keys.
350
+ i += 6;
351
+ continue;
352
+ }
321
353
  let j = skipWs(src, i + 6);
322
354
  while (src[j] === "(")
323
355
  j = skipWs(src, j + 1);
324
356
  if (src[j] !== "{")
325
357
  return undefined; // non-literal return → bail
358
+ if (returned !== undefined && returned !== j)
359
+ return undefined; // `{ ... }.x` or another non-literal → bail
326
360
  const lit = parseReturnedLiteral(src, j);
327
361
  if (lit === undefined)
328
362
  return undefined; // spread/computed key → bail
@@ -337,6 +371,64 @@ export function inferOutputSchema(body) {
337
371
  }
338
372
  return found ? merged : undefined;
339
373
  }
374
+ /**
375
+ * The body's top-level `return` statements: the ones the script's own
376
+ * function runs, not those inside a callback or nested function
377
+ * (`ROWS.map((r) => { return { id: r.id }; })`). Maps each `return` keyword's
378
+ * offset to the offset of its returned object literal, "nothing" for a bare
379
+ * `return`, or "other" for any other value. Undefined when the body is not
380
+ * plain JavaScript.
381
+ */
382
+ function topLevelReturns(body) {
383
+ let program;
384
+ try {
385
+ program = parse(body, {
386
+ ecmaVersion: "latest",
387
+ sourceType: "script",
388
+ allowReturnOutsideFunction: true,
389
+ allowAwaitOutsideFunction: true,
390
+ });
391
+ }
392
+ catch {
393
+ return undefined;
394
+ }
395
+ const returns = new Map();
396
+ const visit = (node) => {
397
+ if (Array.isArray(node)) {
398
+ for (const child of node)
399
+ visit(child);
400
+ return;
401
+ }
402
+ if (typeof node !== "object" || node === null)
403
+ return;
404
+ const { type, start, argument } = node;
405
+ if (typeof type !== "string")
406
+ return;
407
+ if (type === "FunctionDeclaration" || type === "FunctionExpression" || type === "ArrowFunctionExpression")
408
+ return;
409
+ if (type === "ReturnStatement" && typeof start === "number") {
410
+ const literal = argument;
411
+ returns.set(start, literal === null || literal === undefined
412
+ ? "nothing"
413
+ : literal.type === "ObjectExpression" && typeof literal.start === "number" ? literal.start : "other");
414
+ }
415
+ for (const [key, value] of Object.entries(node))
416
+ if (key !== "type")
417
+ visit(value);
418
+ };
419
+ visit(program);
420
+ return returns;
421
+ }
422
+ /** The body with TypeScript types stripped, or undefined when it can't be (no TypeScript, or it doesn't parse). */
423
+ function typeStrippedBody(body) {
424
+ try {
425
+ const stripped = toPlatformScriptBody(body, "executeScript output inference");
426
+ return stripped === body ? undefined : stripped;
427
+ }
428
+ catch {
429
+ return undefined;
430
+ }
431
+ }
340
432
  /** Skip whitespace and comments. */
341
433
  function skipWs(src, i) {
342
434
  for (;;) {
@@ -1339,6 +1431,15 @@ const scanCache = new WeakMap();
1339
1431
  * there's no tracked source (not running under `elementum build`, e.g. an
1340
1432
  * automation constructed directly in a test) or the name/shape wasn't found
1341
1433
  * by the scan (falls back to `extractScriptBody`).
1434
+ *
1435
+ * A body that is not plain JavaScript is also `undefined` (#1927). The
1436
+ * verbatim text exists so a pulled body, which is always JavaScript,
1437
+ * round-trips byte for byte. A body with type annotations was written by
1438
+ * hand, and the runtime that loaded the file has already erased its types
1439
+ * from the function itself, so `extractScriptBody` gives the same result in
1440
+ * every workspace instead of depending on whether a usable TypeScript
1441
+ * compiler happens to resolve. It also runs the closure check, which this
1442
+ * path skips.
1342
1443
  */
1343
1444
  export function tryExtractFromCurrentSource(name) {
1344
1445
  const ctx = currentSourceText();
@@ -1349,7 +1450,8 @@ export function tryExtractFromCurrentSource(name) {
1349
1450
  bodies = scanExecuteScriptBodies(ctx.text);
1350
1451
  scanCache.set(ctx, bodies);
1351
1452
  }
1352
- return bodies.get(name);
1453
+ const body = bodies.get(name);
1454
+ return body !== undefined && plainJavaScriptSyntaxError(body) === undefined ? body : undefined;
1353
1455
  }
1354
1456
  /**
1355
1457
  * Scan raw (untranspiled) `.ts` source text for every fluent
@@ -0,0 +1,13 @@
1
+ /** The input names a script reads from `input.parameters`, and the keys its top-level object returns set. */
2
+ export interface ScriptParameterUse {
3
+ readonly reads: ReadonlySet<string>;
4
+ /** Undefined when a top-level return isn't a plain object literal with static keys. */
5
+ readonly returnedKeys: ReadonlySet<string> | undefined;
6
+ /** Whether any top-level `return` returns a value. With none, every run returns undefined and fails. */
7
+ readonly returnsValue: boolean;
8
+ }
9
+ export declare function scriptParameterUse(code: string): ScriptParameterUse | undefined;
10
+ /** The top-level output names an `outputs` schema declares, or undefined when its shape isn't a plain object. */
11
+ export declare function declaredScriptOutputs(outputSchema: unknown): readonly string[] | undefined;
12
+ /** Advisory messages for one executeScript task. */
13
+ export declare function scriptLintFindings(taskName: string, code: unknown, inputs: unknown, outputSchema: unknown): string[];