@antelopejs/dms-automation 0.7.0 → 0.8.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 (246) hide show
  1. package/README.md +19 -5
  2. package/dist/cluster/bus.js +8 -6
  3. package/dist/cluster/bus.js.map +1 -1
  4. package/dist/cluster/leader.js.map +1 -1
  5. package/dist/data/procedures-query.d.ts +19 -0
  6. package/dist/data/procedures-query.js +105 -0
  7. package/dist/data/procedures-query.js.map +1 -0
  8. package/dist/data/procedures-table.d.ts +4 -20
  9. package/dist/data/procedures-table.js +10 -106
  10. package/dist/data/procedures-table.js.map +1 -1
  11. package/dist/data/runs-table.js.map +1 -1
  12. package/dist/db/models/automation_template.model.d.ts +2 -4
  13. package/dist/db/models/automation_template.model.js +13 -14
  14. package/dist/db/models/automation_template.model.js.map +1 -1
  15. package/dist/db/models/procedure_run.model.js.map +1 -1
  16. package/dist/db/models/stats.model.js.map +1 -1
  17. package/dist/db/tables/procedure_run.table.d.ts +2 -1
  18. package/dist/db/tables/procedure_run.table.js.map +1 -1
  19. package/dist/implementations/dms-automation/index.js.map +1 -1
  20. package/dist/index.js.map +1 -1
  21. package/dist/pages/category.js.map +1 -1
  22. package/dist/pages/index.js.map +1 -1
  23. package/dist/pages/overview.js.map +1 -1
  24. package/dist/pages/procedures.js.map +1 -1
  25. package/dist/pages/runs.js.map +1 -1
  26. package/dist/pages/shared.js +1 -1
  27. package/dist/pages/shared.js.map +1 -1
  28. package/dist/realtime.d.ts +2 -1
  29. package/dist/realtime.js.map +1 -1
  30. package/dist/routes/action-types.d.ts +2 -1
  31. package/dist/routes/action-types.js.map +1 -1
  32. package/dist/routes/deleteConfirm.js +4 -3
  33. package/dist/routes/deleteConfirm.js.map +1 -1
  34. package/dist/routes/graphSchema.d.ts +2 -1
  35. package/dist/routes/graphSchema.js +8 -7
  36. package/dist/routes/graphSchema.js.map +1 -1
  37. package/dist/routes/procedureChecks.js +5 -5
  38. package/dist/routes/procedureChecks.js.map +1 -1
  39. package/dist/routes/procedureRuns.d.ts +2 -2
  40. package/dist/routes/procedureRuns.js +1 -1
  41. package/dist/routes/procedureRuns.js.map +1 -1
  42. package/dist/routes/procedures.d.ts +14 -13
  43. package/dist/routes/procedures.js +2 -1
  44. package/dist/routes/procedures.js.map +1 -1
  45. package/dist/routes/requests.d.ts +11 -5
  46. package/dist/routes/requests.js.map +1 -1
  47. package/dist/routes/runs.d.ts +7 -7
  48. package/dist/routes/runs.js +5 -3
  49. package/dist/routes/runs.js.map +1 -1
  50. package/dist/routes/stats.js.map +1 -1
  51. package/dist/routes/templates.d.ts +3 -2
  52. package/dist/routes/templates.js +2 -1
  53. package/dist/routes/templates.js.map +1 -1
  54. package/dist/routes/trigger-types.d.ts +2 -1
  55. package/dist/routes/trigger-types.js.map +1 -1
  56. package/dist/routes/typeConfigShared.d.ts +2 -1
  57. package/dist/routes/typeConfigShared.js.map +1 -1
  58. package/dist/runtime/armErrors.d.ts +0 -1
  59. package/dist/runtime/armErrors.js +0 -7
  60. package/dist/runtime/armErrors.js.map +1 -1
  61. package/dist/runtime/boundRunLog.d.ts +1 -0
  62. package/dist/runtime/boundRunLog.js +11 -11
  63. package/dist/runtime/boundRunLog.js.map +1 -1
  64. package/dist/runtime/builtins/actions/http.js +16 -9
  65. package/dist/runtime/builtins/actions/http.js.map +1 -1
  66. package/dist/runtime/builtins/actions/log.js.map +1 -1
  67. package/dist/runtime/builtins/data/boolean.js +4 -5
  68. package/dist/runtime/builtins/data/boolean.js.map +1 -1
  69. package/dist/runtime/builtins/data/compare.js +8 -10
  70. package/dist/runtime/builtins/data/compare.js.map +1 -1
  71. package/dist/runtime/builtins/data/constant.js +1 -1
  72. package/dist/runtime/builtins/data/constant.js.map +1 -1
  73. package/dist/runtime/builtins/data/json.js +4 -5
  74. package/dist/runtime/builtins/data/json.js.map +1 -1
  75. package/dist/runtime/builtins/data/math.js +4 -4
  76. package/dist/runtime/builtins/data/math.js.map +1 -1
  77. package/dist/runtime/builtins/data/object.js +12 -17
  78. package/dist/runtime/builtins/data/object.js.map +1 -1
  79. package/dist/runtime/builtins/data/string.js +13 -18
  80. package/dist/runtime/builtins/data/string.js.map +1 -1
  81. package/dist/runtime/builtins/nodes/action.js +5 -2
  82. package/dist/runtime/builtins/nodes/action.js.map +1 -1
  83. package/dist/runtime/builtins/nodes/data.js +2 -2
  84. package/dist/runtime/builtins/nodes/data.js.map +1 -1
  85. package/dist/runtime/builtins/nodes/delay.js +2 -2
  86. package/dist/runtime/builtins/nodes/delay.js.map +1 -1
  87. package/dist/runtime/builtins/nodes/forRange.js +2 -2
  88. package/dist/runtime/builtins/nodes/forRange.js.map +1 -1
  89. package/dist/runtime/builtins/nodes/foreach.js +2 -2
  90. package/dist/runtime/builtins/nodes/foreach.js.map +1 -1
  91. package/dist/runtime/builtins/nodes/groupInput.js +2 -2
  92. package/dist/runtime/builtins/nodes/groupInput.js.map +1 -1
  93. package/dist/runtime/builtins/nodes/groupOutput.js +2 -2
  94. package/dist/runtime/builtins/nodes/groupOutput.js.map +1 -1
  95. package/dist/runtime/builtins/nodes/if.js +2 -2
  96. package/dist/runtime/builtins/nodes/if.js.map +1 -1
  97. package/dist/runtime/builtins/nodes/parallel.js +2 -2
  98. package/dist/runtime/builtins/nodes/parallel.js.map +1 -1
  99. package/dist/runtime/builtins/nodes/retry.js +5 -4
  100. package/dist/runtime/builtins/nodes/retry.js.map +1 -1
  101. package/dist/runtime/builtins/nodes/setVariable.js +2 -2
  102. package/dist/runtime/builtins/nodes/setVariable.js.map +1 -1
  103. package/dist/runtime/builtins/nodes/switch.js +2 -2
  104. package/dist/runtime/builtins/nodes/switch.js.map +1 -1
  105. package/dist/runtime/builtins/nodes/trigger.js +2 -2
  106. package/dist/runtime/builtins/nodes/trigger.js.map +1 -1
  107. package/dist/runtime/builtins/nodes/tryCatch.js +2 -2
  108. package/dist/runtime/builtins/nodes/tryCatch.js.map +1 -1
  109. package/dist/runtime/builtins/triggers/cron.d.ts +2 -1
  110. package/dist/runtime/builtins/triggers/cron.js +5 -7
  111. package/dist/runtime/builtins/triggers/cron.js.map +1 -1
  112. package/dist/runtime/builtins/triggers/webhook.d.ts +6 -5
  113. package/dist/runtime/builtins/triggers/webhook.js +9 -6
  114. package/dist/runtime/builtins/triggers/webhook.js.map +1 -1
  115. package/dist/runtime/declaredProcedures.d.ts +2 -2
  116. package/dist/runtime/declaredProcedures.js +3 -2
  117. package/dist/runtime/declaredProcedures.js.map +1 -1
  118. package/dist/runtime/describe.js +11 -10
  119. package/dist/runtime/describe.js.map +1 -1
  120. package/dist/runtime/executor.js +10 -7
  121. package/dist/runtime/executor.js.map +1 -1
  122. package/dist/runtime/flatten.d.ts +1 -1
  123. package/dist/runtime/flatten.js +1 -1
  124. package/dist/runtime/flatten.js.map +1 -1
  125. package/dist/runtime/graphIssues.js +24 -22
  126. package/dist/runtime/graphIssues.js.map +1 -1
  127. package/dist/runtime/graphVersion.d.ts +1 -1
  128. package/dist/runtime/graphVersion.js +2 -1
  129. package/dist/runtime/graphVersion.js.map +1 -1
  130. package/dist/runtime/nodeKinds.d.ts +3 -2
  131. package/dist/runtime/nodeKinds.js.map +1 -1
  132. package/dist/runtime/reconcile.d.ts +3 -0
  133. package/dist/runtime/reconcile.js +178 -0
  134. package/dist/runtime/reconcile.js.map +1 -0
  135. package/dist/runtime/resolveInputs.d.ts +4 -2
  136. package/dist/runtime/resolveInputs.js +13 -12
  137. package/dist/runtime/resolveInputs.js.map +1 -1
  138. package/dist/runtime/resolveSubgraph.js +4 -5
  139. package/dist/runtime/resolveSubgraph.js.map +1 -1
  140. package/dist/runtime/retention.js.map +1 -1
  141. package/dist/runtime/runProcedure.d.ts +1 -1
  142. package/dist/runtime/runProcedure.js +2 -1
  143. package/dist/runtime/runProcedure.js.map +1 -1
  144. package/dist/runtime/schedule.js +3 -0
  145. package/dist/runtime/schedule.js.map +1 -1
  146. package/dist/runtime/schemaProperties.d.ts +11 -0
  147. package/dist/runtime/schemaProperties.js +7 -0
  148. package/dist/runtime/schemaProperties.js.map +1 -0
  149. package/dist/runtime/serialQueue.d.ts +1 -1
  150. package/dist/runtime/serialQueue.js +4 -4
  151. package/dist/runtime/serialQueue.js.map +1 -1
  152. package/dist/runtime/subscriptions.js +21 -180
  153. package/dist/runtime/subscriptions.js.map +1 -1
  154. package/dist/runtime/subwalk.js.map +1 -1
  155. package/dist/runtime/templateCacheState.d.ts +6 -0
  156. package/dist/runtime/templateCacheState.js +10 -0
  157. package/dist/runtime/templateCacheState.js.map +1 -0
  158. package/dist/runtime/templateUsages.js +4 -5
  159. package/dist/runtime/templateUsages.js.map +1 -1
  160. package/dist/runtime/topo.js +2 -4
  161. package/dist/runtime/topo.js.map +1 -1
  162. package/dist/runtime/trace.js +12 -9
  163. package/dist/runtime/trace.js.map +1 -1
  164. package/dist/runtime/triggerHandles.d.ts +3 -0
  165. package/dist/runtime/triggerHandles.js +8 -0
  166. package/dist/runtime/triggerHandles.js.map +1 -1
  167. package/dist/runtime/typeOrigin.js +12 -1
  168. package/dist/runtime/typeOrigin.js.map +1 -1
  169. package/dist/runtime/validate.js.map +1 -1
  170. package/dist/runtime/validateTemplate.js.map +1 -1
  171. package/dist/runtime/withTimeout.js.map +1 -1
  172. package/dist/runtime/wording.js +12 -8
  173. package/dist/runtime/wording.js.map +1 -1
  174. package/dist/stats/health-banner.js.map +1 -1
  175. package/dist/stats/health.js.map +1 -1
  176. package/dist/stats/kpi.js +1 -1
  177. package/dist/stats/kpi.js.map +1 -1
  178. package/dist/stats/overview.js +2 -1
  179. package/dist/stats/overview.js.map +1 -1
  180. package/dist/types/dataValue.d.ts +8 -0
  181. package/dist/types/dataValue.js +36 -0
  182. package/dist/types/dataValue.js.map +1 -0
  183. package/dist/types/graph.d.ts +8 -1
  184. package/dist/types/graph.js +8 -0
  185. package/dist/types/graph.js.map +1 -1
  186. package/dist/types/guards.d.ts +8 -0
  187. package/dist/types/guards.js +35 -0
  188. package/dist/types/guards.js.map +1 -0
  189. package/frontend-vue/app/build/components/editor/EditorBanners.vue +120 -0
  190. package/frontend-vue/app/build/components/editor/EditorChoiceModal.vue +44 -0
  191. package/frontend-vue/app/build/components/editor/EditorForkModal.vue +51 -0
  192. package/frontend-vue/app/build/components/editor/EditorHeader.vue +146 -0
  193. package/frontend-vue/app/build/components/editor/EditorSaveTemplateModal.vue +105 -0
  194. package/frontend-vue/app/build/components/editor/NodeInspectorDocs.vue +28 -0
  195. package/frontend-vue/app/build/components/editor/NodeInspectorRun.vue +48 -0
  196. package/frontend-vue/app/build/composables/editor/useBuilderMount.ts +129 -0
  197. package/frontend-vue/app/build/composables/editor/useBuilderSave.ts +256 -0
  198. package/frontend-vue/app/build/composables/editor/useBuilderShortcuts.ts +270 -0
  199. package/frontend-vue/app/build/composables/editor/useDraftProblems.ts +288 -0
  200. package/frontend-vue/app/build/composables/editor/useEditorContext.ts +388 -0
  201. package/frontend-vue/app/build/composables/editor/useEditorLoad.ts +158 -0
  202. package/frontend-vue/app/build/composables/editor/useForkToLocal.ts +193 -0
  203. package/frontend-vue/app/build/composables/editor/useGraphHistory.ts +113 -0
  204. package/frontend-vue/app/build/composables/editor/useGroupEditing.ts +445 -0
  205. package/frontend-vue/app/build/composables/editor/usePalette.ts +41 -0
  206. package/frontend-vue/app/build/composables/editor/useProcedureActions.ts +96 -0
  207. package/frontend-vue/app/build/composables/editor/useRunOverlay.ts +466 -0
  208. package/frontend-vue/app/build/composables/editor/useSaveAsTemplate.ts +172 -0
  209. package/frontend-vue/app/build/composables/editor/useSavedTriggers.ts +100 -0
  210. package/frontend-vue/app/build/composables/editor/useSubgraphNavigation.ts +270 -0
  211. package/frontend-vue/app/build/composables/editor/useUnsavedGuards.ts +233 -0
  212. package/frontend-vue/app/build/composables/graphCanvas/connect.ts +273 -0
  213. package/frontend-vue/app/build/composables/graphCanvas/edgeEvents.ts +172 -0
  214. package/frontend-vue/app/build/composables/graphCanvas/edges.ts +55 -0
  215. package/frontend-vue/app/build/composables/graphCanvas/handles.ts +181 -0
  216. package/frontend-vue/app/build/composables/graphCanvas/linkMenu.ts +123 -0
  217. package/frontend-vue/app/build/composables/graphCanvas/nodeData.ts +222 -0
  218. package/frontend-vue/app/build/composables/graphCanvas/placement.ts +248 -0
  219. package/frontend-vue/app/build/composables/graphCanvas/selection.ts +150 -0
  220. package/frontend-vue/app/build/composables/graphCanvas/types.ts +188 -0
  221. package/frontend-vue/app/build/composables/graphCanvas/view.ts +230 -0
  222. package/frontend-vue/app/build/types/editor.ts +94 -0
  223. package/frontend-vue/app/build/utils/editor/apiErrors.ts +74 -0
  224. package/frontend-vue/app/build/utils/editor/graphShape.ts +68 -0
  225. package/frontend-vue/app/build/utils/editor/groupPlan.ts +322 -0
  226. package/frontend-vue/app/build/utils/editor/groupPorts.ts +210 -0
  227. package/frontend-vue/app/build/utils/editor/keyboard.ts +22 -0
  228. package/frontend-vue/app/build/utils/inspectorFields.ts +117 -0
  229. package/frontend-vue/app/build/utils/templates.ts +88 -0
  230. package/frontend-vue/app/components/AttentionList.vue +2 -6
  231. package/frontend-vue/app/components/Catalog.vue +2 -1
  232. package/frontend-vue/app/components/Editor/GraphCanvas.vue +78 -1337
  233. package/frontend-vue/app/components/Editor/NodeInspector.vue +31 -123
  234. package/frontend-vue/app/components/Editor.vue +134 -3484
  235. package/frontend-vue/app/components/Templates.vue +14 -84
  236. package/frontend-vue/build/backend-boundary.ts +122 -0
  237. package/frontend-vue/dms.frontend.build.ts +2 -0
  238. package/frontend-vue/eslint-rules/index.mjs +103 -0
  239. package/frontend-vue/eslint.config.mjs +15 -0
  240. package/frontend-vue/package.json +7 -4
  241. package/frontend-vue/pnpm-lock.yaml +73 -59
  242. package/frontend-vue/tests/api-error-presentation.test.cjs +257 -0
  243. package/frontend-vue/tests/helpers/setup.cjs +44 -0
  244. package/package.json +4 -4
  245. package/skills/dms-automation/SKILL.md +2 -2
  246. package/skills/dms-automation/cases.json +58 -0
@@ -0,0 +1,100 @@
1
+ import { computed, type Ref } from "vue";
2
+ import type { EditorContext } from "./useEditorContext";
3
+ import { describeDateTime } from "../../../utils/describe";
4
+ import type { ProcedureTriggers } from "../../types/editor";
5
+ import type { DraftProblems } from "./useDraftProblems";
6
+
7
+ /** The triggers of the saved procedure, what the paused banner says of them, and the save state label. */
8
+ export function useSavedTriggers(ctx: EditorContext, problems: DraftProblems) {
9
+ const {
10
+ dateTimeFormat,
11
+ editorMode,
12
+ processText,
13
+ props,
14
+ saving,
15
+ selected,
16
+ selectedId,
17
+ t,
18
+ } = ctx;
19
+ const { changeCount, currentSummary, savedName } = problems;
20
+
21
+ /**
22
+ * A URL of the procedure open, for the reads that follow it: none in
23
+ * template mode or while no procedure is open.
24
+ */
25
+ function selectedProcedureUrl(path: string): () => string | null {
26
+ return () =>
27
+ selectedId.value && editorMode.value === "procedure"
28
+ ? `${props.apiUrl}/procedures/${selectedId.value}/${path}`
29
+ : null;
30
+ }
31
+
32
+ /**
33
+ * Data of the procedure open, or nothing while another one's is still on
34
+ * screen or once its read failed.
35
+ */
36
+ function ofSelected<T>(read: {
37
+ data: Ref<T | null>;
38
+ placeholder: Readonly<Ref<boolean>>;
39
+ error: Ref<unknown>;
40
+ }): T | null {
41
+ return read.placeholder.value || read.error.value ? null : read.data.value;
42
+ }
43
+
44
+ // The saved triggers: what a paused procedure will not fire, and what Run
45
+ // now offers.
46
+ const savedTriggersRead = useApiData<ProcedureTriggers>(
47
+ selectedProcedureUrl("triggers"),
48
+ );
49
+ const savedTriggers = computed(
50
+ () => ofSelected(savedTriggersRead)?.triggers ?? [],
51
+ );
52
+
53
+ /** The banner of a disabled procedure names what will not fire (AU-16). */
54
+ const pausedBanner = computed(() => {
55
+ const current = selected.value;
56
+ if (!current || current.enabled || editorMode.value !== "procedure")
57
+ return null;
58
+ const triggers = savedTriggers.value.filter((tr) => tr.typeId !== "manual");
59
+ if (triggers.length === 0) return null;
60
+ const first = triggers[0]!;
61
+ const what = processText(first.text);
62
+ const isDraft = currentSummary.value?.status === "draft";
63
+ return {
64
+ title: t(
65
+ isDraft
66
+ ? "dms_automation.editor.paused.draftTitle"
67
+ : "dms_automation.editor.paused.title",
68
+ { name: savedName.value },
69
+ ),
70
+ description: first.nextAt
71
+ ? t("dms_automation.editor.paused.schedule", {
72
+ what,
73
+ next: describeDateTime(
74
+ first.nextAt,
75
+ dateTimeFormat,
76
+ first.timeZone,
77
+ ),
78
+ })
79
+ : t("dms_automation.editor.paused.other", { what }),
80
+ };
81
+ });
82
+
83
+ const saveState = computed(() => {
84
+ if (saving.value) return t("dms_automation.editor.saving");
85
+ if (changeCount.value > 0)
86
+ return t("dms_automation.editor.unsaved", { count: changeCount.value });
87
+ return t("dms_automation.editor.saved");
88
+ });
89
+
90
+ return {
91
+ selectedProcedureUrl,
92
+ ofSelected,
93
+ savedTriggersRead,
94
+ savedTriggers,
95
+ pausedBanner,
96
+ saveState,
97
+ };
98
+ }
99
+
100
+ export type SavedTriggers = ReturnType<typeof useSavedTriggers>;
@@ -0,0 +1,270 @@
1
+ import { computed, watch } from "vue";
2
+ import type { BreadcrumbItem } from "@nuxt/ui";
3
+ import type { EditorContext } from "./useEditorContext";
4
+ import type {
5
+ GraphNode,
6
+ GroupPort,
7
+ ProcedureGraph,
8
+ } from "../../../composables/useGraphConnect";
9
+ import { cloneGraph } from "../../utils/editor/graphShape";
10
+ import type { GraphHistory } from "./useGraphHistory";
11
+ import type { EditorLoad } from "./useEditorLoad";
12
+
13
+ /**
14
+ * Seeds the path with the root segment: the procedure, or in template mode the
15
+ * template itself.
16
+ */
17
+ export function seedSubgraphPath({
18
+ selected,
19
+ editorMode,
20
+ subgraphPath,
21
+ }: EditorContext) {
22
+ const p = selected.value;
23
+ if (!p) {
24
+ subgraphPath.value = [];
25
+ return;
26
+ }
27
+ // In template mode the root segment is the template itself (we reuse the
28
+ // 'template' kind already declared on SubgraphPathSegment). The breadcrumb
29
+ // renders it with the i-ph-link icon, which is exactly what we want for
30
+ // "Templates ▸ <name>".
31
+ if (editorMode.value === "template") {
32
+ subgraphPath.value = [
33
+ { kind: "template", id: p._id, name: p.name || "Template" },
34
+ ];
35
+ return;
36
+ }
37
+ subgraphPath.value = [
38
+ { kind: "procedure", id: p._id, name: p.name || "Procedure" },
39
+ ];
40
+ }
41
+
42
+ /** Moving through the groups of the open procedure, and writing the canvas's graph back. */
43
+ export function useSubgraphNavigation(
44
+ ctx: EditorContext,
45
+ history: GraphHistory,
46
+ load: EditorLoad,
47
+ ) {
48
+ const {
49
+ currentGraph,
50
+ editorMode,
51
+ graphCanvasRef,
52
+ selected,
53
+ selectedId,
54
+ selectedNode,
55
+ selectedNodeId,
56
+ subgraphFrames,
57
+ subgraphPath,
58
+ t,
59
+ } = ctx;
60
+ const { initHistoryAndSaved, pushHistory } = history;
61
+ const { openTemplate, templateRead } = load;
62
+
63
+ const breadcrumbItems = computed<BreadcrumbItem[]>(() =>
64
+ subgraphPath.value.map((seg, idx) => ({
65
+ label: seg.name,
66
+ icon:
67
+ seg.kind === "procedure"
68
+ ? "i-ph-flow-arrow"
69
+ : seg.kind === "template"
70
+ ? "i-ph-link"
71
+ : "i-ph-package",
72
+ // onSelect fires for clicks; popping the path to `idx + 1` keeps
73
+ // the clicked segment and discards everything deeper.
74
+ onSelect: (ev: Event) => {
75
+ ev.preventDefault();
76
+ if (idx < subgraphPath.value.length - 1) {
77
+ subgraphPath.value = subgraphPath.value.slice(0, idx + 1);
78
+ // Clear node selection — the old selected node id may not
79
+ // exist in the new current graph.
80
+ selectedNodeId.value = null;
81
+ }
82
+ },
83
+ })),
84
+ );
85
+
86
+ // If the user deletes a group node from an outer graph while their path
87
+ // points into its subgraph, prune the path back to the deepest valid
88
+ // frame. Cheap because subgraphFrames already does the traversal.
89
+ watch(
90
+ () => subgraphFrames.value.length,
91
+ (framesLen) => {
92
+ if (framesLen > 0 && framesLen < subgraphPath.value.length) {
93
+ subgraphPath.value = subgraphPath.value.slice(0, framesLen);
94
+ selectedNodeId.value = null;
95
+ }
96
+ },
97
+ );
98
+
99
+ // Keep the procedure-root breadcrumb label in sync with renames.
100
+ watch(
101
+ () => selected.value?.name,
102
+ (name) => {
103
+ const first = subgraphPath.value[0];
104
+ if (first && first.kind === "procedure" && name) first.name = name;
105
+ },
106
+ );
107
+
108
+ function exitSubgraph() {
109
+ subgraphPath.value = subgraphPath.value.slice(0, -1);
110
+ selectedNodeId.value = null;
111
+ }
112
+
113
+ function onGraphUpdate(g: ProcedureGraph) {
114
+ if (!selected.value) return;
115
+ const path = subgraphPath.value;
116
+ if (path.length <= 1) {
117
+ // At procedure root — write straight through, same as before
118
+ // subgraph navigation existed.
119
+ selected.value.graph = g;
120
+ pushHistory(g, true);
121
+ return;
122
+ }
123
+ // At a deeper level — `g` is the subgraph of the deepest group node.
124
+ // Walk the path from the outer procedure graph down, cloning the
125
+ // chain so Vue's reactivity sees a fresh top-level graph (history
126
+ // keys structuralKey() off node identity at every level).
127
+ const rootGraph = cloneGraph(selected.value.graph);
128
+ let containerNodes = rootGraph.nodes;
129
+ for (let i = 1; i < path.length; i++) {
130
+ const seg = path[i]!;
131
+ const idx = containerNodes.findIndex(
132
+ (n) => n.id === seg.id && n.kind === "group",
133
+ );
134
+ if (idx === -1) {
135
+ // Path was stale — bail out without mutating; the watch on
136
+ // subgraphFrames.length will prune the path.
137
+ return;
138
+ }
139
+ // Replace the node at idx with a shallow clone so we can mutate
140
+ // its `subgraph` without aliasing back into the original (the
141
+ // outer graph already came from cloneGraph above, so its node
142
+ // objects are fresh too — but the inner subgraph reference
143
+ // chain needs explicit substitution).
144
+ const cloned: GraphNode = { ...containerNodes[idx]! };
145
+ containerNodes[idx] = cloned;
146
+ if (i === path.length - 1) {
147
+ cloned.subgraph = g;
148
+ } else {
149
+ // Mid-chain — keep walking, ensuring each intermediate
150
+ // subgraph is also a fresh object for the next iteration's
151
+ // nodes array.
152
+ if (!cloned.subgraph) return;
153
+ cloned.subgraph = {
154
+ ...cloned.subgraph,
155
+ nodes: [...cloned.subgraph.nodes],
156
+ };
157
+ containerNodes = cloned.subgraph.nodes;
158
+ }
159
+ }
160
+ selected.value.graph = rootGraph;
161
+ pushHistory(rootGraph, true);
162
+ }
163
+
164
+ function onSelectNode(id: string | null) {
165
+ selectedNodeId.value = id;
166
+ }
167
+
168
+ function onPaletteAddNode(payload: { kind: string; typeId?: string }) {
169
+ graphCanvasRef.value?.addNode(payload.kind, payload.typeId);
170
+ }
171
+ function onPaletteAddTemplate(template: {
172
+ _id: string;
173
+ name: string;
174
+ icon?: string;
175
+ ports: GroupPort[];
176
+ }) {
177
+ graphCanvasRef.value?.addTemplateInstance(template);
178
+ }
179
+
180
+ watch(selectedId, () => {
181
+ selectedNodeId.value = null;
182
+ initHistoryAndSaved();
183
+ seedSubgraphPath(ctx);
184
+ });
185
+
186
+ // `?template=<id>` changed while the editor stayed mounted (Editor is
187
+ // mounted once per route navigation, so this is mostly a guard for in-place
188
+ // query updates, e.g. catalog "Edit" buttons using router.push): the template
189
+ // read follows the URL, and the editor opens what it answers.
190
+ watch(
191
+ () => templateRead.status.value,
192
+ (status) => {
193
+ if (
194
+ editorMode.value === "template" &&
195
+ (status === "success" || status === "error")
196
+ )
197
+ openTemplate();
198
+ },
199
+ );
200
+
201
+ function groupName(node: GraphNode): string {
202
+ return (
203
+ node.label ||
204
+ (node.config?.name as string | undefined) ||
205
+ t("dms_automation.editor.groupDefaultName")
206
+ );
207
+ }
208
+
209
+ function onGroupEnter(payload: { id: string }) {
210
+ const g = currentGraph.value;
211
+ if (!g) return;
212
+ const node = g.nodes.find((n) => n.id === payload.id && n.kind === "group");
213
+ if (!node) return;
214
+ // Template instances have no inline subgraph — the frame walker would
215
+ // stop at this segment and the watcher would prune it back. Bail early so
216
+ // the path doesn't briefly include a doomed segment.
217
+ if (node.templateId && !node.subgraph) return;
218
+ const name = groupName(node);
219
+ subgraphPath.value = [
220
+ ...subgraphPath.value,
221
+ { kind: "group", id: node.id, name },
222
+ ];
223
+ selectedNodeId.value = null;
224
+ }
225
+
226
+ // "Enter group" affordance — true iff the currently selected node is a group
227
+ // (local, or a template instance with a resolved subgraph). Read-only state
228
+
229
+ // Drives the toolbar button, the inspector button, and the Tab keybinding.
230
+ const canEnterGroup = computed<boolean>(() => {
231
+ const n = selectedNode.value;
232
+ if (!n || n.kind !== "group") return false;
233
+ // Template instances need a resolved subgraph to navigate into; without
234
+ // one we can't render the inner view.
235
+ if (n.templateId && !n.subgraph) return false;
236
+ return true;
237
+ });
238
+
239
+ function enterSelectedGroup() {
240
+ const n = selectedNode.value;
241
+ if (!n || n.kind !== "group") return;
242
+ onGroupEnter({ id: n.id });
243
+ }
244
+
245
+ function onUpdateNode(updated: GraphNode) {
246
+ const g = currentGraph.value;
247
+ if (!g) return;
248
+ const nextGraph: ProcedureGraph = {
249
+ ...g,
250
+ nodes: g.nodes.map((n) => (n.id === updated.id ? updated : n)),
251
+ };
252
+ onGraphUpdate(nextGraph);
253
+ }
254
+
255
+ return {
256
+ breadcrumbItems,
257
+ exitSubgraph,
258
+ onGraphUpdate,
259
+ onSelectNode,
260
+ onPaletteAddNode,
261
+ onPaletteAddTemplate,
262
+ groupName,
263
+ onGroupEnter,
264
+ canEnterGroup,
265
+ enterSelectedGroup,
266
+ onUpdateNode,
267
+ };
268
+ }
269
+
270
+ export type SubgraphNavigation = ReturnType<typeof useSubgraphNavigation>;
@@ -0,0 +1,233 @@
1
+ import { ref, watch } from "vue";
2
+ import type { EditorContext } from "./useEditorContext";
3
+ import type { ChoiceQuestion } from "../../types/editor";
4
+ import type { GraphHistory } from "./useGraphHistory";
5
+ import type { DraftProblems } from "./useDraftProblems";
6
+
7
+ /**
8
+ * The questions with more than a yes and a no, and the guards that ask them
9
+ * before an unsaved draft is left.
10
+ */
11
+ export function useUnsavedGuards(
12
+ ctx: EditorContext,
13
+ history: GraphHistory,
14
+ problems: DraftProblems,
15
+ later: { onSave: () => Promise<boolean> },
16
+ ) {
17
+ const {
18
+ builderPath,
19
+ editorMode,
20
+ missingProcedureId,
21
+ procedures,
22
+ queryValue,
23
+ router,
24
+ selectedId,
25
+ switcherOpen,
26
+ t,
27
+ unsavedDraft,
28
+ } = ctx;
29
+ const { isDirty } = history;
30
+ const { changeCount, onCancel, savedName } = problems;
31
+ const { onSave } = later;
32
+
33
+ const choiceDialog = ref<
34
+ ChoiceQuestion<string> & {
35
+ open: boolean;
36
+ resolve?: (choice: string) => void;
37
+ }
38
+ >({
39
+ open: false,
40
+ title: "",
41
+ description: "",
42
+ choices: [],
43
+ dismiss: "",
44
+ });
45
+
46
+ function askChoice<C extends string>(
47
+ question: ChoiceQuestion<C>,
48
+ ): Promise<C> {
49
+ return new Promise((resolve) => {
50
+ choiceDialog.value = {
51
+ ...question,
52
+ open: true,
53
+ resolve: resolve as (choice: string) => void,
54
+ };
55
+ });
56
+ }
57
+
58
+ /** Escape, ✕ or a click outside answers the question's `dismiss`. */
59
+ function onChoiceDialogOpen(open: boolean) {
60
+ if (!open && choiceDialog.value.open)
61
+ answerChoice(choiceDialog.value.dismiss);
62
+ }
63
+
64
+ function answerChoice(choice: string) {
65
+ const resolve = choiceDialog.value.resolve;
66
+ choiceDialog.value = {
67
+ ...choiceDialog.value,
68
+ open: false,
69
+ resolve: undefined,
70
+ };
71
+ resolve?.(choice);
72
+ }
73
+
74
+ /** Resolves `true` when the draft may be left: saved, discarded or clean. */
75
+ async function confirmDiscard(): Promise<boolean> {
76
+ if (!isDirty.value) return true;
77
+ const choice = await askChoice({
78
+ title: t("dms_automation.editor.leave.title", { name: savedName.value }),
79
+ description: t("dms_automation.editor.leave.description", {
80
+ count: changeCount.value,
81
+ }),
82
+ choices: [
83
+ {
84
+ value: "discard",
85
+ label: t("dms_automation.editor.discard"),
86
+ color: "error",
87
+ variant: "ghost",
88
+ },
89
+ {
90
+ value: "stay",
91
+ label: t("dms_automation.editor.leave.keep"),
92
+ color: "neutral",
93
+ variant: "outline",
94
+ },
95
+ {
96
+ value: "save",
97
+ label: t("dms_automation.editor.leave.save"),
98
+ color: "primary",
99
+ },
100
+ ],
101
+ dismiss: "stay",
102
+ });
103
+ if (choice === "stay") return false;
104
+ if (choice === "save") return await onSave();
105
+ onCancel();
106
+ return true;
107
+ }
108
+
109
+ async function onPickProcedure(id: string) {
110
+ if (!(await confirmDiscard())) return;
111
+ selectedId.value = id;
112
+ void router.replace(`${builderPath()}?selected=${id}`);
113
+ }
114
+
115
+ // The template editor guards its draft like the procedure editor.
116
+ watch(isDirty, (dirty) => (unsavedDraft.value = dirty), { immediate: true });
117
+
118
+ // The DMS leave guard only holds a visit to another path. A visit to the
119
+ // builder itself that remounts it (the sidebar's "Builder", a link with
120
+ // another ?selected=), or that keeps it mounted but opens another procedure,
121
+ // loses the draft just the same: it is held here and run again once the
122
+ // draft is saved or discarded. The builder's own URL updates go through.
123
+ interface PendingVisit {
124
+ url?: URL;
125
+ method?: string;
126
+ prefetch?: boolean;
127
+ preserveState?: unknown;
128
+ replace?: boolean;
129
+ }
130
+
131
+ function onInertiaBefore(event: Event) {
132
+ const visit = (event as CustomEvent<{ visit?: PendingVisit }>).detail
133
+ ?.visit;
134
+ if (!visit?.url || visit.prefetch) return;
135
+ if (visit.method && visit.method.toLowerCase() !== "get") return;
136
+ if (visit.url.pathname !== builderPath() || !isDirty.value) return;
137
+ const keepsPage = visit.preserveState === true;
138
+ if (
139
+ keepsPage &&
140
+ !opensAnotherProcedure(visit.url.searchParams.get("selected"))
141
+ )
142
+ return;
143
+ event.preventDefault();
144
+ const href = `${visit.url.pathname}${visit.url.search}${visit.url.hash}`;
145
+ void confirmDiscard().then((leave) => {
146
+ if (leave)
147
+ void navigateDms(href, {
148
+ preserveState: keepsPage,
149
+ replace: visit.replace,
150
+ });
151
+ });
152
+ }
153
+
154
+ function opensAnotherProcedure(id: string | null): id is string {
155
+ return editorMode.value === "procedure" && !!id && id !== selectedId.value;
156
+ }
157
+
158
+ // Back and Forward between two addresses of the builder: the DMS guard lets
159
+ // them through (same path). Where the browser announces them before they
160
+ // happen (Navigation API), the draft is asked about first and the step is
161
+ // taken again once it is saved or discarded. A browser without it remounts
162
+ // the page on the step: the DMS guard has the same gap.
163
+ interface TraverseEvent extends Event {
164
+ navigationType?: string;
165
+ destination?: { url: string; key: string; sameDocument: boolean };
166
+ }
167
+
168
+ interface NavigationLike extends EventTarget {
169
+ traverseTo: (key: string) => unknown;
170
+ }
171
+
172
+ function browserNavigation(): NavigationLike | undefined {
173
+ return typeof window === "undefined"
174
+ ? undefined
175
+ : (window as unknown as { navigation?: NavigationLike }).navigation;
176
+ }
177
+
178
+ // Set while the history step the user chose to take is taken again.
179
+ let traverseAllowed = false;
180
+
181
+ function onNavigate(event: Event) {
182
+ const { navigationType, destination } = event as TraverseEvent;
183
+ if (navigationType !== "traverse" || !destination) return;
184
+ if (traverseAllowed) {
185
+ traverseAllowed = false;
186
+ return;
187
+ }
188
+ if (!destination.sameDocument || !event.cancelable || !isDirty.value)
189
+ return;
190
+ if (new URL(destination.url).pathname !== builderPath()) return;
191
+ event.preventDefault();
192
+ void confirmDiscard().then((leave) => {
193
+ if (!leave) return;
194
+ traverseAllowed = true;
195
+ void browserNavigation()?.traverseTo(destination.key);
196
+ });
197
+ }
198
+
199
+ // ?selected= changed while the page stayed mounted (a visit that keeps it,
200
+ // such as a link to the builder from the builder): the builder opens that
201
+
202
+ watch(
203
+ () => queryValue("selected"),
204
+ async (id) => {
205
+ if (!opensAnotherProcedure(id)) return;
206
+ if (!(await confirmDiscard())) {
207
+ void router.replace(`${builderPath()}?selected=${selectedId.value}`);
208
+ return;
209
+ }
210
+ if (procedures.value.some((p) => p._id === id)) {
211
+ selectedId.value = id;
212
+ return;
213
+ }
214
+ missingProcedureId.value = id;
215
+ selectedId.value = null;
216
+ switcherOpen.value = true;
217
+ },
218
+ );
219
+
220
+ return {
221
+ choiceDialog,
222
+ askChoice,
223
+ onChoiceDialogOpen,
224
+ answerChoice,
225
+ confirmDiscard,
226
+ onPickProcedure,
227
+ onInertiaBefore,
228
+ onNavigate,
229
+ browserNavigation,
230
+ };
231
+ }
232
+
233
+ export type UnsavedGuards = ReturnType<typeof useUnsavedGuards>;