@antelopejs/dms-automation 0.6.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 (262) hide show
  1. package/README.md +21 -7
  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 +5 -16
  9. package/dist/data/procedures-table.js +12 -101
  10. package/dist/data/procedures-table.js.map +1 -1
  11. package/dist/data/runs-table.js +0 -5
  12. package/dist/data/runs-table.js.map +1 -1
  13. package/dist/db/models/automation_template.model.d.ts +2 -4
  14. package/dist/db/models/automation_template.model.js +13 -14
  15. package/dist/db/models/automation_template.model.js.map +1 -1
  16. package/dist/db/models/procedure_run.model.js.map +1 -1
  17. package/dist/db/models/stats.model.js.map +1 -1
  18. package/dist/db/tables/procedure.table.d.ts +0 -1
  19. package/dist/db/tables/procedure.table.js +0 -4
  20. package/dist/db/tables/procedure.table.js.map +1 -1
  21. package/dist/db/tables/procedure_run.table.d.ts +2 -1
  22. package/dist/db/tables/procedure_run.table.js.map +1 -1
  23. package/dist/implementations/dms-automation/index.js.map +1 -1
  24. package/dist/index.js.map +1 -1
  25. package/dist/pages/category.js.map +1 -1
  26. package/dist/pages/index.js.map +1 -1
  27. package/dist/pages/overview.js.map +1 -1
  28. package/dist/pages/procedures.js.map +1 -1
  29. package/dist/pages/runs.js.map +1 -1
  30. package/dist/pages/shared.js +1 -1
  31. package/dist/pages/shared.js.map +1 -1
  32. package/dist/realtime.d.ts +2 -1
  33. package/dist/realtime.js.map +1 -1
  34. package/dist/routes/action-types.d.ts +2 -1
  35. package/dist/routes/action-types.js.map +1 -1
  36. package/dist/routes/deleteConfirm.js +4 -3
  37. package/dist/routes/deleteConfirm.js.map +1 -1
  38. package/dist/routes/graphSchema.d.ts +2 -1
  39. package/dist/routes/graphSchema.js +8 -7
  40. package/dist/routes/graphSchema.js.map +1 -1
  41. package/dist/routes/procedureChecks.js +5 -5
  42. package/dist/routes/procedureChecks.js.map +1 -1
  43. package/dist/routes/procedureRuns.d.ts +2 -2
  44. package/dist/routes/procedureRuns.js +1 -1
  45. package/dist/routes/procedureRuns.js.map +1 -1
  46. package/dist/routes/procedures.d.ts +14 -15
  47. package/dist/routes/procedures.js +2 -1
  48. package/dist/routes/procedures.js.map +1 -1
  49. package/dist/routes/requests.d.ts +11 -5
  50. package/dist/routes/requests.js.map +1 -1
  51. package/dist/routes/runs.d.ts +7 -7
  52. package/dist/routes/runs.js +5 -3
  53. package/dist/routes/runs.js.map +1 -1
  54. package/dist/routes/stats.js.map +1 -1
  55. package/dist/routes/templates.d.ts +3 -2
  56. package/dist/routes/templates.js +2 -1
  57. package/dist/routes/templates.js.map +1 -1
  58. package/dist/routes/trigger-types.d.ts +2 -1
  59. package/dist/routes/trigger-types.js.map +1 -1
  60. package/dist/routes/typeConfigShared.d.ts +2 -1
  61. package/dist/routes/typeConfigShared.js.map +1 -1
  62. package/dist/runtime/armErrors.d.ts +0 -1
  63. package/dist/runtime/armErrors.js +0 -7
  64. package/dist/runtime/armErrors.js.map +1 -1
  65. package/dist/runtime/boundRunLog.d.ts +1 -0
  66. package/dist/runtime/boundRunLog.js +11 -11
  67. package/dist/runtime/boundRunLog.js.map +1 -1
  68. package/dist/runtime/builtins/actions/http.js +16 -9
  69. package/dist/runtime/builtins/actions/http.js.map +1 -1
  70. package/dist/runtime/builtins/actions/log.js.map +1 -1
  71. package/dist/runtime/builtins/data/boolean.js +4 -5
  72. package/dist/runtime/builtins/data/boolean.js.map +1 -1
  73. package/dist/runtime/builtins/data/compare.js +8 -10
  74. package/dist/runtime/builtins/data/compare.js.map +1 -1
  75. package/dist/runtime/builtins/data/constant.js +1 -1
  76. package/dist/runtime/builtins/data/constant.js.map +1 -1
  77. package/dist/runtime/builtins/data/json.js +4 -5
  78. package/dist/runtime/builtins/data/json.js.map +1 -1
  79. package/dist/runtime/builtins/data/math.js +4 -4
  80. package/dist/runtime/builtins/data/math.js.map +1 -1
  81. package/dist/runtime/builtins/data/object.js +12 -17
  82. package/dist/runtime/builtins/data/object.js.map +1 -1
  83. package/dist/runtime/builtins/data/string.js +13 -18
  84. package/dist/runtime/builtins/data/string.js.map +1 -1
  85. package/dist/runtime/builtins/nodes/action.js +5 -2
  86. package/dist/runtime/builtins/nodes/action.js.map +1 -1
  87. package/dist/runtime/builtins/nodes/data.js +2 -2
  88. package/dist/runtime/builtins/nodes/data.js.map +1 -1
  89. package/dist/runtime/builtins/nodes/delay.js +2 -2
  90. package/dist/runtime/builtins/nodes/delay.js.map +1 -1
  91. package/dist/runtime/builtins/nodes/forRange.js +2 -2
  92. package/dist/runtime/builtins/nodes/forRange.js.map +1 -1
  93. package/dist/runtime/builtins/nodes/foreach.js +2 -2
  94. package/dist/runtime/builtins/nodes/foreach.js.map +1 -1
  95. package/dist/runtime/builtins/nodes/groupInput.js +2 -2
  96. package/dist/runtime/builtins/nodes/groupInput.js.map +1 -1
  97. package/dist/runtime/builtins/nodes/groupOutput.js +2 -2
  98. package/dist/runtime/builtins/nodes/groupOutput.js.map +1 -1
  99. package/dist/runtime/builtins/nodes/if.js +2 -2
  100. package/dist/runtime/builtins/nodes/if.js.map +1 -1
  101. package/dist/runtime/builtins/nodes/parallel.js +2 -2
  102. package/dist/runtime/builtins/nodes/parallel.js.map +1 -1
  103. package/dist/runtime/builtins/nodes/retry.js +5 -6
  104. package/dist/runtime/builtins/nodes/retry.js.map +1 -1
  105. package/dist/runtime/builtins/nodes/setVariable.js +2 -2
  106. package/dist/runtime/builtins/nodes/setVariable.js.map +1 -1
  107. package/dist/runtime/builtins/nodes/switch.js +2 -2
  108. package/dist/runtime/builtins/nodes/switch.js.map +1 -1
  109. package/dist/runtime/builtins/nodes/trigger.js +2 -2
  110. package/dist/runtime/builtins/nodes/trigger.js.map +1 -1
  111. package/dist/runtime/builtins/nodes/tryCatch.js +2 -4
  112. package/dist/runtime/builtins/nodes/tryCatch.js.map +1 -1
  113. package/dist/runtime/builtins/triggers/cron.d.ts +2 -1
  114. package/dist/runtime/builtins/triggers/cron.js +5 -7
  115. package/dist/runtime/builtins/triggers/cron.js.map +1 -1
  116. package/dist/runtime/builtins/triggers/webhook.d.ts +6 -5
  117. package/dist/runtime/builtins/triggers/webhook.js +9 -6
  118. package/dist/runtime/builtins/triggers/webhook.js.map +1 -1
  119. package/dist/runtime/declaredProcedures.d.ts +2 -2
  120. package/dist/runtime/declaredProcedures.js +17 -92
  121. package/dist/runtime/declaredProcedures.js.map +1 -1
  122. package/dist/runtime/describe.js +11 -10
  123. package/dist/runtime/describe.js.map +1 -1
  124. package/dist/runtime/executionResult.d.ts +1 -1
  125. package/dist/runtime/executor.js +12 -38
  126. package/dist/runtime/executor.js.map +1 -1
  127. package/dist/runtime/flatten.d.ts +1 -1
  128. package/dist/runtime/flatten.js +1 -1
  129. package/dist/runtime/flatten.js.map +1 -1
  130. package/dist/runtime/graphIssues.js +24 -22
  131. package/dist/runtime/graphIssues.js.map +1 -1
  132. package/dist/runtime/graphVersion.d.ts +1 -1
  133. package/dist/runtime/graphVersion.js +2 -1
  134. package/dist/runtime/graphVersion.js.map +1 -1
  135. package/dist/runtime/nodeKinds.d.ts +3 -2
  136. package/dist/runtime/nodeKinds.js.map +1 -1
  137. package/dist/runtime/reconcile.d.ts +3 -0
  138. package/dist/runtime/reconcile.js +178 -0
  139. package/dist/runtime/reconcile.js.map +1 -0
  140. package/dist/runtime/registry.d.ts +0 -7
  141. package/dist/runtime/registry.js +3 -24
  142. package/dist/runtime/registry.js.map +1 -1
  143. package/dist/runtime/resolveInputs.d.ts +4 -2
  144. package/dist/runtime/resolveInputs.js +13 -12
  145. package/dist/runtime/resolveInputs.js.map +1 -1
  146. package/dist/runtime/resolveSubgraph.js +4 -5
  147. package/dist/runtime/resolveSubgraph.js.map +1 -1
  148. package/dist/runtime/retention.js.map +1 -1
  149. package/dist/runtime/runProcedure.d.ts +1 -1
  150. package/dist/runtime/runProcedure.js +2 -1
  151. package/dist/runtime/runProcedure.js.map +1 -1
  152. package/dist/runtime/schedule.js +3 -0
  153. package/dist/runtime/schedule.js.map +1 -1
  154. package/dist/runtime/schemaProperties.d.ts +11 -0
  155. package/dist/runtime/schemaProperties.js +7 -0
  156. package/dist/runtime/schemaProperties.js.map +1 -0
  157. package/dist/runtime/serialQueue.d.ts +1 -1
  158. package/dist/runtime/serialQueue.js +4 -4
  159. package/dist/runtime/serialQueue.js.map +1 -1
  160. package/dist/runtime/subscriptions.js +21 -183
  161. package/dist/runtime/subscriptions.js.map +1 -1
  162. package/dist/runtime/subwalk.js.map +1 -1
  163. package/dist/runtime/templateCacheState.d.ts +6 -0
  164. package/dist/runtime/templateCacheState.js +10 -0
  165. package/dist/runtime/templateCacheState.js.map +1 -0
  166. package/dist/runtime/templateUsages.js +4 -5
  167. package/dist/runtime/templateUsages.js.map +1 -1
  168. package/dist/runtime/topo.js +2 -4
  169. package/dist/runtime/topo.js.map +1 -1
  170. package/dist/runtime/trace.js +12 -9
  171. package/dist/runtime/trace.js.map +1 -1
  172. package/dist/runtime/triggerHandles.d.ts +3 -0
  173. package/dist/runtime/triggerHandles.js +8 -3
  174. package/dist/runtime/triggerHandles.js.map +1 -1
  175. package/dist/runtime/typeOrigin.js +12 -1
  176. package/dist/runtime/typeOrigin.js.map +1 -1
  177. package/dist/runtime/validate.js.map +1 -1
  178. package/dist/runtime/validateTemplate.js.map +1 -1
  179. package/dist/runtime/withTimeout.js.map +1 -1
  180. package/dist/runtime/wording.js +12 -8
  181. package/dist/runtime/wording.js.map +1 -1
  182. package/dist/stats/health-banner.js.map +1 -1
  183. package/dist/stats/health.js +2 -3
  184. package/dist/stats/health.js.map +1 -1
  185. package/dist/stats/kpi.js +1 -1
  186. package/dist/stats/kpi.js.map +1 -1
  187. package/dist/stats/overview.js +2 -1
  188. package/dist/stats/overview.js.map +1 -1
  189. package/dist/types/dataValue.d.ts +8 -0
  190. package/dist/types/dataValue.js +36 -0
  191. package/dist/types/dataValue.js.map +1 -0
  192. package/dist/types/graph.d.ts +8 -1
  193. package/dist/types/graph.js +8 -0
  194. package/dist/types/graph.js.map +1 -1
  195. package/dist/types/guards.d.ts +8 -0
  196. package/dist/types/guards.js +35 -0
  197. package/dist/types/guards.js.map +1 -0
  198. package/frontend-vue/app/build/components/editor/EditorBanners.vue +120 -0
  199. package/frontend-vue/app/build/components/editor/EditorChoiceModal.vue +44 -0
  200. package/frontend-vue/app/build/components/editor/EditorForkModal.vue +51 -0
  201. package/frontend-vue/app/build/components/editor/EditorHeader.vue +146 -0
  202. package/frontend-vue/app/build/components/editor/EditorSaveTemplateModal.vue +105 -0
  203. package/frontend-vue/app/build/components/editor/NodeInspectorDocs.vue +28 -0
  204. package/frontend-vue/app/build/components/editor/NodeInspectorRun.vue +48 -0
  205. package/frontend-vue/app/build/composables/editor/useBuilderMount.ts +129 -0
  206. package/frontend-vue/app/build/composables/editor/useBuilderSave.ts +256 -0
  207. package/frontend-vue/app/build/composables/editor/useBuilderShortcuts.ts +270 -0
  208. package/frontend-vue/app/build/composables/editor/useDraftProblems.ts +288 -0
  209. package/frontend-vue/app/build/composables/editor/useEditorContext.ts +388 -0
  210. package/frontend-vue/app/build/composables/editor/useEditorLoad.ts +158 -0
  211. package/frontend-vue/app/build/composables/editor/useForkToLocal.ts +193 -0
  212. package/frontend-vue/app/build/composables/editor/useGraphHistory.ts +113 -0
  213. package/frontend-vue/app/build/composables/editor/useGroupEditing.ts +445 -0
  214. package/frontend-vue/app/build/composables/editor/usePalette.ts +41 -0
  215. package/frontend-vue/app/build/composables/editor/useProcedureActions.ts +96 -0
  216. package/frontend-vue/app/build/composables/editor/useRunOverlay.ts +466 -0
  217. package/frontend-vue/app/build/composables/editor/useSaveAsTemplate.ts +172 -0
  218. package/frontend-vue/app/build/composables/editor/useSavedTriggers.ts +100 -0
  219. package/frontend-vue/app/build/composables/editor/useSubgraphNavigation.ts +270 -0
  220. package/frontend-vue/app/build/composables/editor/useUnsavedGuards.ts +233 -0
  221. package/frontend-vue/app/build/composables/graphCanvas/connect.ts +273 -0
  222. package/frontend-vue/app/build/composables/graphCanvas/edgeEvents.ts +172 -0
  223. package/frontend-vue/app/build/composables/graphCanvas/edges.ts +55 -0
  224. package/frontend-vue/app/build/composables/graphCanvas/handles.ts +181 -0
  225. package/frontend-vue/app/build/composables/graphCanvas/linkMenu.ts +123 -0
  226. package/frontend-vue/app/build/composables/graphCanvas/nodeData.ts +222 -0
  227. package/frontend-vue/app/build/composables/graphCanvas/placement.ts +248 -0
  228. package/frontend-vue/app/build/composables/graphCanvas/selection.ts +150 -0
  229. package/frontend-vue/app/build/composables/graphCanvas/types.ts +188 -0
  230. package/frontend-vue/app/build/composables/graphCanvas/view.ts +230 -0
  231. package/frontend-vue/app/build/types/editor.ts +94 -0
  232. package/frontend-vue/app/build/utils/editor/apiErrors.ts +74 -0
  233. package/frontend-vue/app/build/utils/editor/graphShape.ts +68 -0
  234. package/frontend-vue/app/build/utils/editor/groupPlan.ts +322 -0
  235. package/frontend-vue/app/build/utils/editor/groupPorts.ts +210 -0
  236. package/frontend-vue/app/build/utils/editor/keyboard.ts +22 -0
  237. package/frontend-vue/app/build/utils/inspectorFields.ts +117 -0
  238. package/frontend-vue/app/build/utils/templates.ts +88 -0
  239. package/frontend-vue/app/components/AttentionList.vue +2 -6
  240. package/frontend-vue/app/components/Catalog.vue +2 -1
  241. package/frontend-vue/app/components/Editor/BottomDock.vue +0 -3
  242. package/frontend-vue/app/components/Editor/GraphCanvas.vue +78 -1337
  243. package/frontend-vue/app/components/Editor/NodeInspector.vue +31 -123
  244. package/frontend-vue/app/components/Editor.vue +134 -3490
  245. package/frontend-vue/app/components/Templates.vue +14 -84
  246. package/frontend-vue/app/composables/useAutomationRuns.ts +2 -2
  247. package/frontend-vue/build/backend-boundary.ts +122 -0
  248. package/frontend-vue/dms.frontend.build.ts +2 -0
  249. package/frontend-vue/eslint-rules/index.mjs +103 -0
  250. package/frontend-vue/eslint.config.mjs +15 -0
  251. package/frontend-vue/i18n/locales/dms-automation-en-GB.json +0 -3
  252. package/frontend-vue/i18n/locales/dms-automation-fr-FR.json +0 -3
  253. package/frontend-vue/package.json +7 -4
  254. package/frontend-vue/pnpm-lock.yaml +98 -68
  255. package/frontend-vue/tests/api-error-presentation.test.cjs +257 -0
  256. package/frontend-vue/tests/helpers/setup.cjs +44 -0
  257. package/package.json +10 -10
  258. package/skills/dms-automation/SKILL.md +4 -4
  259. package/skills/dms-automation/cases.json +58 -0
  260. package/dist/runtime/departed.d.ts +0 -6
  261. package/dist/runtime/departed.js +0 -38
  262. package/dist/runtime/departed.js.map +0 -1
@@ -0,0 +1,158 @@
1
+ import type { EditorContext } from "./useEditorContext";
2
+ import type {
3
+ GroupPort,
4
+ ProcedureGraph,
5
+ } from "../../../composables/useGraphConnect";
6
+ import type { ProcedureRecord } from "../../types/editor";
7
+
8
+ /** Reads what the builder opens, the procedure list or the template `?template=` names, and opens it. */
9
+ export function useEditorLoad(ctx: EditorContext) {
10
+ const {
11
+ editorMode,
12
+ editorTemplateId,
13
+ loadError,
14
+ loading,
15
+ missingProcedureId,
16
+ procedures,
17
+ queryValue,
18
+ selectedId,
19
+ t,
20
+ templateIcon,
21
+ templateNotFound,
22
+ templatePorts,
23
+ } = ctx;
24
+
25
+ const PROCEDURES_PAGE_SIZE = 200;
26
+
27
+ // What the builder opens, read with the page: every procedure, or the
28
+ // template `?template=` names. The editor works on its own copy of what they
29
+ // answered (`procedures`), which its drafts change.
30
+ const procedureListRead = useApiData<ProcedureRecord[]>(
31
+ () =>
32
+ editorMode.value === "procedure"
33
+ ? "dms-automation-builder-procedures"
34
+ : null,
35
+ async ({ $authFetch, signal }) => {
36
+ // The API serves pages of PROCEDURES_PAGE_SIZE at most: read them all.
37
+ const list: ProcedureRecord[] = [];
38
+ for (let page = 0; ; page++) {
39
+ const { results } = await $authFetch<{ results: ProcedureRecord[] }>(
40
+ `/api/automation/procedures?limit=${PROCEDURES_PAGE_SIZE}&page=${page}`,
41
+ { signal },
42
+ );
43
+ list.push(...results);
44
+ if (results.length < PROCEDURES_PAGE_SIZE) break;
45
+ }
46
+ return list;
47
+ },
48
+ );
49
+
50
+ interface AutomationTemplateApi {
51
+ _id: string;
52
+ name: string;
53
+ description?: string;
54
+ icon?: string;
55
+ ports: GroupPort[];
56
+ subgraph: ProcedureGraph;
57
+ }
58
+
59
+ const templateRead = useApiData<AutomationTemplateApi>(() =>
60
+ editorTemplateId.value
61
+ ? `/api/automation/templates/${editorTemplateId.value}`
62
+ : null,
63
+ );
64
+
65
+ /** Opens what the procedure list answered: `?selected=`, else the procedure open, else the first. */
66
+ function openList() {
67
+ const error = procedureListRead.error.value;
68
+ if (error) {
69
+ loadError.value = describeApiError(error);
70
+ return;
71
+ }
72
+ loadError.value = null;
73
+ // Copies: a draft changes the editor's records, not the read's.
74
+ procedures.value = (procedureListRead.data.value ?? []).map((p) => ({
75
+ ...p,
76
+ }));
77
+ if (
78
+ selectedId.value &&
79
+ !procedures.value.some((p) => p._id === selectedId.value)
80
+ ) {
81
+ selectedId.value = null;
82
+ }
83
+ const requested = queryValue("selected");
84
+ if (requested && procedures.value.some((p) => p._id === requested)) {
85
+ selectedId.value = requested;
86
+ } else if (requested && !selectedId.value) {
87
+ // A link to a deleted procedure opens none: the builder says so
88
+ // and offers the switcher (see onMounted).
89
+ missingProcedureId.value = requested;
90
+ return;
91
+ }
92
+ if (!selectedId.value && procedures.value.length > 0) {
93
+ const first = procedures.value[0];
94
+ selectedId.value = first ? first._id : null;
95
+ }
96
+ }
97
+
98
+ /** Reads the procedure list again (one was created) and opens it. */
99
+ async function loadList() {
100
+ loading.value = true;
101
+ await procedureListRead.refresh();
102
+ loading.value = false;
103
+ openList();
104
+ }
105
+
106
+ /** Opens what the template read answered, or says why there is none. */
107
+ function openTemplate() {
108
+ const id = editorTemplateId.value;
109
+ if (!id) {
110
+ templateNotFound.value = true;
111
+ loadError.value = t("dms_automation.editor.templateNotFound");
112
+ return;
113
+ }
114
+ const error = templateRead.error.value;
115
+ const tpl = templateRead.data.value;
116
+ if (error || !tpl) {
117
+ // Worded for users, the same on the server and in the browser (the
118
+ // raw fetch error names an internal URL that differs between them).
119
+ templateNotFound.value = apiErrorStatus(error) === 404;
120
+ loadError.value = templateNotFound.value
121
+ ? t("dms_automation.editor.templateNotFound")
122
+ : describeApiError(error);
123
+ procedures.value = [];
124
+ selectedId.value = null;
125
+ templatePorts.value = [];
126
+ return;
127
+ }
128
+ templateNotFound.value = false;
129
+ loadError.value = null;
130
+ // Adapt the template into the ProcedureRecord shape so the rest of
131
+ // the editor (history, dirty-tracking, sentinel sync, group toolbar)
132
+ // keeps working unchanged. `enabled` is meaningless for templates;
133
+ // pin it to true so the "disabled" warning banner never appears.
134
+ const adapted: ProcedureRecord = {
135
+ _id: tpl._id,
136
+ name: tpl.name,
137
+ description: tpl.description ?? "",
138
+ enabled: true,
139
+ graph: tpl.subgraph,
140
+ };
141
+ procedures.value = [adapted];
142
+ selectedId.value = adapted._id;
143
+ templateIcon.value = tpl.icon || "i-ph-package";
144
+ templatePorts.value = Array.isArray(tpl.ports)
145
+ ? tpl.ports.map((p) => ({ ...p }))
146
+ : [];
147
+ }
148
+
149
+ return {
150
+ procedureListRead,
151
+ templateRead,
152
+ openList,
153
+ loadList,
154
+ openTemplate,
155
+ };
156
+ }
157
+
158
+ export type EditorLoad = ReturnType<typeof useEditorLoad>;
@@ -0,0 +1,193 @@
1
+ import { computed, ref } from "vue";
2
+ import type { EditorContext } from "./useEditorContext";
3
+ import type {
4
+ GraphNode,
5
+ GroupPort,
6
+ ProcedureGraph,
7
+ } from "../../../composables/useGraphConnect";
8
+ import type { SubgraphNavigation } from "./useSubgraphNavigation";
9
+
10
+ /** Turning a template instance back into a local group. */
11
+ export function useForkToLocal(ctx: EditorContext, nav: SubgraphNavigation) {
12
+ const { resolveError } = useApiErrorPresenter();
13
+ const {
14
+ $authFetch,
15
+ currentGraph,
16
+ isCurrentGraphReadOnly,
17
+ selected,
18
+ selectedNode,
19
+ selectedNodeId,
20
+ subgraphFrames,
21
+ subgraphPath,
22
+ t,
23
+ toast,
24
+ } = ctx;
25
+ const { onUpdateNode } = nav;
26
+
27
+ // ---------------------------------------------------------------------------
28
+ // Fork-to-local-group
29
+ // ---------------------------------------------------------------------------
30
+ // Visible when a template-instance group node is selected — i.e. kind === 'group'
31
+ // AND templateId is set. The fork is the escape hatch from the live-link
32
+ // model: it converts the instance back to a local group by deep-cloning the
33
+ // template's current subgraph into the node and dropping templateId.
34
+ //
35
+ // Selection rules:
36
+ // • Allowed even from the read-only inner-subgraph view of a template
37
+ // instance — this is the "Fork to edit" CTA flow. The action mutates
38
+ // the OUTER graph (the procedure root or whichever container holds
39
+ // the template instance), not the inner subgraph being viewed.
40
+ // • To make that work, the selection target for the fork is the deepest
41
+ // currentParentGroup when we're inside a template-instance subgraph,
42
+ // OR the selectedNode otherwise.
43
+ //
44
+ // The deep-clone uses a JSON parse/stringify round-trip: structuredClone
45
+ // throws on Vue's reactive proxies (see the inline note at the clone site).
46
+ //
47
+ // Edge case: if the template was deleted between selection and confirm,
48
+ // GET /:id returns 404 → we surface that as an error in the modal so the
49
+ // user can dismiss and stop trying.
50
+ interface ForkTarget {
51
+ // The group node to convert (the template instance).
52
+ node: GraphNode;
53
+ // The graph in which that node lives. May be the procedure root or any
54
+ // outer subgraph (when the user is viewing the inner read-only graph of
55
+ // an instance, the instance node lives one frame up).
56
+ containerGraph: ProcedureGraph;
57
+ }
58
+
59
+ const forkTarget = computed<ForkTarget | null>(() => {
60
+ if (!selected.value) return null;
61
+ const frames = subgraphFrames.value;
62
+ // Case A: user is INSIDE a template-instance subgraph (read-only view).
63
+ // The instance node is the parent group; its container is the previous
64
+ // frame's graph.
65
+ if (isCurrentGraphReadOnly.value && frames.length >= 2) {
66
+ const parent = frames[frames.length - 1]!.groupNode;
67
+ const container = frames[frames.length - 2]!.graph;
68
+ if (parent && parent.kind === "group" && parent.templateId) {
69
+ return { node: parent, containerGraph: container };
70
+ }
71
+ }
72
+ // Case B: user has a template-instance node selected in the current
73
+ // (editable) graph.
74
+ const sel = selectedNode.value;
75
+ const g = currentGraph.value;
76
+ if (sel && g && sel.kind === "group" && sel.templateId) {
77
+ return { node: sel, containerGraph: g };
78
+ }
79
+ return null;
80
+ });
81
+
82
+ const canForkToLocal = computed<boolean>(() => forkTarget.value !== null);
83
+
84
+ const forkOpen = ref<boolean>(false);
85
+ const forkSubmitting = ref<boolean>(false);
86
+ const forkError = ref<ReturnType<typeof resolveError>>(null);
87
+
88
+ function openFork() {
89
+ if (!canForkToLocal.value) return;
90
+ forkError.value = null;
91
+ forkSubmitting.value = false;
92
+ forkOpen.value = true;
93
+ }
94
+
95
+ interface FetchedTemplate {
96
+ _id: string;
97
+ name: string;
98
+ description: string;
99
+ icon: string;
100
+ ports: GroupPort[];
101
+ subgraph: ProcedureGraph;
102
+ }
103
+
104
+ // Replace the template instance node with a forked local copy. Mirrors the
105
+ // shape of convertLocalGroupToTemplateInstance but in reverse: drops
106
+ // templateId, installs subgraph from the fetched template, and writes back
107
+ // through onUpdateNode / onGraphUpdate (so history + dirty-snapshot stay
108
+ // consistent). The replacement uses a fresh node object so Vue reactivity
109
+ // sees a real change.
110
+ function convertTemplateInstanceToLocal(
111
+ target: ForkTarget,
112
+ template: FetchedTemplate,
113
+ ): void {
114
+ const { node, containerGraph } = target;
115
+ const idx = containerGraph.nodes.findIndex(
116
+ (n) => n.id === node.id && n.kind === "group",
117
+ );
118
+ if (idx === -1) return;
119
+ const updated: GraphNode = {
120
+ ...node,
121
+ // Deep-clone the template's subgraph so subsequent edits to the
122
+ // template (or to this fork) don't alias each other. JSON round-trip
123
+ // strips Vue's reactive proxy — structuredClone throws on proxies.
124
+ subgraph: JSON.parse(JSON.stringify(template.subgraph)) as ProcedureGraph,
125
+ // Preserve the local ports if the instance carries them (it should
126
+ // — palette drops clone template.ports onto the instance); fall back
127
+ // to the template's port list otherwise.
128
+ ports:
129
+ Array.isArray(node.ports) && node.ports.length > 0
130
+ ? node.ports.map((p) => ({ ...p }))
131
+ : template.ports.map((p) => ({ ...p })),
132
+ };
133
+ delete updated.templateId;
134
+
135
+ // If the user was inside the template instance's subgraph (Case A in
136
+ // forkTarget), pop the read-only path segment first so the canvas
137
+ // re-binds to the (now editable) outer graph. The user can re-enter
138
+ // the local subgraph fresh via dblclick if they want.
139
+ if (isCurrentGraphReadOnly.value && subgraphPath.value.length > 1) {
140
+ subgraphPath.value = subgraphPath.value.slice(0, -1);
141
+ selectedNodeId.value = updated.id;
142
+ }
143
+
144
+ // Use onUpdateNode — it walks subgraphPath and rewrites the correct
145
+ // container graph (root or any depth). Because we just popped above
146
+ // when needed, currentGraph now points at containerGraph.
147
+ onUpdateNode(updated);
148
+ }
149
+
150
+ async function submitFork() {
151
+ const target = forkTarget.value;
152
+ if (!target) return;
153
+ const templateId = target.node.templateId;
154
+ if (!templateId) return;
155
+ forkSubmitting.value = true;
156
+ forkError.value = null;
157
+ try {
158
+ const tpl = await $authFetch<FetchedTemplate>(
159
+ `/api/automation/templates/${templateId}`,
160
+ );
161
+ convertTemplateInstanceToLocal(target, tpl);
162
+ forkOpen.value = false;
163
+ toast.add({
164
+ title: t("dms_automation.editor.forked", {
165
+ name:
166
+ target.node.label ||
167
+ (target.node.config?.name as string | undefined) ||
168
+ tpl.name,
169
+ }),
170
+ color: "success",
171
+ icon: "i-ph-check-circle",
172
+ });
173
+ } catch (e) {
174
+ forkError.value = resolveError(e, {
175
+ fallbackKey: "dms_automation.editor.forkModal.error",
176
+ });
177
+ } finally {
178
+ forkSubmitting.value = false;
179
+ }
180
+ }
181
+
182
+ return {
183
+ forkTarget,
184
+ canForkToLocal,
185
+ forkOpen,
186
+ forkSubmitting,
187
+ forkError,
188
+ openFork,
189
+ submitFork,
190
+ };
191
+ }
192
+
193
+ export type ForkToLocal = ReturnType<typeof useForkToLocal>;
@@ -0,0 +1,113 @@
1
+ import { computed, ref } from "vue";
2
+ import type { EditorContext } from "./useEditorContext";
3
+ import type { ProcedureGraph } from "../../../composables/useGraphConnect";
4
+ import { cloneGraph, repeatableChangeKey } from "../../utils/editor/graphShape";
5
+ import type { ProcedureRecord, ProcedureSnapshot } from "../../types/editor";
6
+
7
+ /** The draft's undo history, and the snapshot a save is compared against. */
8
+ export function useGraphHistory(ctx: EditorContext) {
9
+ const { editorMode, savedSnapshot, selected, templatePorts } = ctx;
10
+
11
+ const graphHistory = ref<ProcedureGraph[]>([]);
12
+ const graphHistoryIndex = ref<number>(-1);
13
+
14
+ const HISTORY_LIMIT = 100;
15
+
16
+ function takeSnapshot(p: ProcedureRecord): ProcedureSnapshot {
17
+ return {
18
+ name: p.name,
19
+ enabled: p.enabled,
20
+ graph: cloneGraph(p.graph),
21
+ ...(editorMode.value === "template"
22
+ ? { ports: templatePorts.value.map((port) => ({ ...port })) }
23
+ : {}),
24
+ };
25
+ }
26
+
27
+ // A drag emits a graph per frame and a text field one per keystroke: the
28
+ // changes of one burst make one undo step (the last one is replaced while
29
+ // the same nodes move, or the same field is typed in, without a pause).
30
+ const HISTORY_MERGE_MS = 1000;
31
+ let lastChange: { key: string | null; at: number } = { key: null, at: 0 };
32
+
33
+ function pushHistory(g: ProcedureGraph, merge = false) {
34
+ const current = graphHistory.value[graphHistoryIndex.value];
35
+ if (current && JSON.stringify(current) === JSON.stringify(g)) return;
36
+ const key = merge && current ? repeatableChangeKey(current, g) : null;
37
+ const now = Date.now();
38
+ const sameBurst =
39
+ key !== null &&
40
+ key === lastChange.key &&
41
+ now - lastChange.at < HISTORY_MERGE_MS;
42
+ lastChange = { key, at: now };
43
+ // A new action, a drag included, drops what Redo could bring back.
44
+ graphHistory.value = graphHistory.value.slice(
45
+ 0,
46
+ graphHistoryIndex.value + 1,
47
+ );
48
+ if (sameBurst) {
49
+ graphHistory.value[graphHistoryIndex.value] = cloneGraph(g);
50
+ return;
51
+ }
52
+ graphHistory.value.push(cloneGraph(g));
53
+ graphHistoryIndex.value = graphHistory.value.length - 1;
54
+ if (graphHistory.value.length > HISTORY_LIMIT) {
55
+ graphHistory.value.shift();
56
+ graphHistoryIndex.value--;
57
+ }
58
+ }
59
+
60
+ function initHistoryAndSaved() {
61
+ lastChange = { key: null, at: 0 };
62
+ if (selected.value) {
63
+ graphHistory.value = [cloneGraph(selected.value.graph)];
64
+ graphHistoryIndex.value = 0;
65
+ savedSnapshot.value = takeSnapshot(selected.value);
66
+ } else {
67
+ graphHistory.value = [];
68
+ graphHistoryIndex.value = -1;
69
+ savedSnapshot.value = null;
70
+ }
71
+ }
72
+
73
+ const canUndo = computed<boolean>(() => graphHistoryIndex.value > 0);
74
+ const canRedo = computed<boolean>(
75
+ () => graphHistoryIndex.value < graphHistory.value.length - 1,
76
+ );
77
+ const isDirty = computed<boolean>(() => {
78
+ if (!selected.value || !savedSnapshot.value) return false;
79
+ return (
80
+ JSON.stringify(takeSnapshot(selected.value)) !==
81
+ JSON.stringify(savedSnapshot.value)
82
+ );
83
+ });
84
+
85
+ function onUndo() {
86
+ if (!canUndo.value || !selected.value) return;
87
+ lastChange = { key: null, at: 0 };
88
+ graphHistoryIndex.value--;
89
+ const g = graphHistory.value[graphHistoryIndex.value];
90
+ if (g) selected.value.graph = cloneGraph(g);
91
+ }
92
+
93
+ function onRedo() {
94
+ if (!canRedo.value || !selected.value) return;
95
+ lastChange = { key: null, at: 0 };
96
+ graphHistoryIndex.value++;
97
+ const g = graphHistory.value[graphHistoryIndex.value];
98
+ if (g) selected.value.graph = cloneGraph(g);
99
+ }
100
+
101
+ return {
102
+ takeSnapshot,
103
+ pushHistory,
104
+ initHistoryAndSaved,
105
+ canUndo,
106
+ canRedo,
107
+ isDirty,
108
+ onUndo,
109
+ onRedo,
110
+ };
111
+ }
112
+
113
+ export type GraphHistory = ReturnType<typeof useGraphHistory>;