@xyne/workflow-sdk 3.2.47 → 3.2.48

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 (162) hide show
  1. package/dist/agents/host-agent-step.d.ts +2 -2
  2. package/dist/connectors/builtin/steps/classifier-agent.step.d.ts +2 -2
  3. package/dist/connectors/builtin/steps/parallel.step.d.ts +4 -4
  4. package/dist/connectors/builtin/steps/wait.step.d.ts +28 -0
  5. package/dist/connectors/builtin/steps/wait.step.d.ts.map +1 -1
  6. package/dist/connectors/builtin/steps/wait.step.js +26 -1
  7. package/dist/connectors/builtin/steps/wait.step.js.map +1 -1
  8. package/dist/conversational-flow/calls.d.ts +20 -0
  9. package/dist/conversational-flow/calls.d.ts.map +1 -0
  10. package/dist/conversational-flow/calls.js +59 -0
  11. package/dist/conversational-flow/calls.js.map +1 -0
  12. package/dist/conversational-flow/conditions.d.ts +24 -0
  13. package/dist/conversational-flow/conditions.d.ts.map +1 -0
  14. package/dist/conversational-flow/conditions.js +69 -0
  15. package/dist/conversational-flow/conditions.js.map +1 -0
  16. package/dist/conversational-flow/create.d.ts +23 -18
  17. package/dist/conversational-flow/create.d.ts.map +1 -1
  18. package/dist/conversational-flow/create.js +73 -96
  19. package/dist/conversational-flow/create.js.map +1 -1
  20. package/dist/conversational-flow/edit/build.d.ts +112 -0
  21. package/dist/conversational-flow/edit/build.d.ts.map +1 -0
  22. package/dist/conversational-flow/edit/build.js +249 -0
  23. package/dist/conversational-flow/edit/build.js.map +1 -0
  24. package/dist/conversational-flow/edit/calls.d.ts +61 -0
  25. package/dist/conversational-flow/edit/calls.d.ts.map +1 -0
  26. package/dist/conversational-flow/edit/calls.js +296 -0
  27. package/dist/conversational-flow/edit/calls.js.map +1 -0
  28. package/dist/conversational-flow/edit/cases.d.ts +29 -0
  29. package/dist/conversational-flow/edit/cases.d.ts.map +1 -0
  30. package/dist/conversational-flow/edit/cases.js +83 -0
  31. package/dist/conversational-flow/edit/cases.js.map +1 -0
  32. package/dist/conversational-flow/edit/endings.d.ts +16 -0
  33. package/dist/conversational-flow/edit/endings.d.ts.map +1 -0
  34. package/dist/conversational-flow/edit/endings.js +53 -0
  35. package/dist/conversational-flow/edit/endings.js.map +1 -0
  36. package/dist/conversational-flow/edit/followups.d.ts +28 -0
  37. package/dist/conversational-flow/edit/followups.d.ts.map +1 -0
  38. package/dist/conversational-flow/edit/followups.js +103 -0
  39. package/dist/conversational-flow/edit/followups.js.map +1 -0
  40. package/dist/conversational-flow/edit/inputs.d.ts +24 -0
  41. package/dist/conversational-flow/edit/inputs.d.ts.map +1 -0
  42. package/dist/conversational-flow/edit/inputs.js +53 -0
  43. package/dist/conversational-flow/edit/inputs.js.map +1 -0
  44. package/dist/conversational-flow/edit/kinds.d.ts +29 -0
  45. package/dist/conversational-flow/edit/kinds.d.ts.map +1 -0
  46. package/dist/conversational-flow/edit/kinds.js +142 -0
  47. package/dist/conversational-flow/edit/kinds.js.map +1 -0
  48. package/dist/conversational-flow/edit/options.d.ts +68 -0
  49. package/dist/conversational-flow/edit/options.d.ts.map +1 -0
  50. package/dist/conversational-flow/edit/options.js +209 -0
  51. package/dist/conversational-flow/edit/options.js.map +1 -0
  52. package/dist/conversational-flow/edit/picklist.d.ts +28 -0
  53. package/dist/conversational-flow/edit/picklist.d.ts.map +1 -0
  54. package/dist/conversational-flow/edit/picklist.js +58 -0
  55. package/dist/conversational-flow/edit/picklist.js.map +1 -0
  56. package/dist/conversational-flow/edit/prompts.d.ts +24 -0
  57. package/dist/conversational-flow/edit/prompts.d.ts.map +1 -0
  58. package/dist/conversational-flow/edit/prompts.js +180 -0
  59. package/dist/conversational-flow/edit/prompts.js.map +1 -0
  60. package/dist/conversational-flow/edit/routers.d.ts +65 -0
  61. package/dist/conversational-flow/edit/routers.d.ts.map +1 -0
  62. package/dist/conversational-flow/edit/routers.js +208 -0
  63. package/dist/conversational-flow/edit/routers.js.map +1 -0
  64. package/dist/conversational-flow/edit/screens.d.ts +44 -0
  65. package/dist/conversational-flow/edit/screens.d.ts.map +1 -0
  66. package/dist/conversational-flow/edit/screens.js +137 -0
  67. package/dist/conversational-flow/edit/screens.js.map +1 -0
  68. package/dist/conversational-flow/edit/versions.d.ts +43 -0
  69. package/dist/conversational-flow/edit/versions.d.ts.map +1 -0
  70. package/dist/conversational-flow/edit/versions.js +189 -0
  71. package/dist/conversational-flow/edit/versions.js.map +1 -0
  72. package/dist/conversational-flow/index.d.ts +33 -16
  73. package/dist/conversational-flow/index.d.ts.map +1 -1
  74. package/dist/conversational-flow/index.js +42 -9
  75. package/dist/conversational-flow/index.js.map +1 -1
  76. package/dist/conversational-flow/json.d.ts +15 -0
  77. package/dist/conversational-flow/json.d.ts.map +1 -0
  78. package/dist/conversational-flow/json.js +14 -0
  79. package/dist/conversational-flow/json.js.map +1 -0
  80. package/dist/conversational-flow/jumps.d.ts +22 -0
  81. package/dist/conversational-flow/jumps.d.ts.map +1 -0
  82. package/dist/conversational-flow/jumps.js +160 -0
  83. package/dist/conversational-flow/jumps.js.map +1 -0
  84. package/dist/conversational-flow/locate.d.ts +30 -0
  85. package/dist/conversational-flow/locate.d.ts.map +1 -0
  86. package/dist/conversational-flow/locate.js +73 -0
  87. package/dist/conversational-flow/locate.js.map +1 -0
  88. package/dist/conversational-flow/paths.d.ts +31 -0
  89. package/dist/conversational-flow/paths.d.ts.map +1 -0
  90. package/dist/conversational-flow/paths.js +50 -0
  91. package/dist/conversational-flow/paths.js.map +1 -0
  92. package/dist/conversational-flow/prompt.d.ts +48 -0
  93. package/dist/conversational-flow/prompt.d.ts.map +1 -0
  94. package/dist/conversational-flow/prompt.js +161 -0
  95. package/dist/conversational-flow/prompt.js.map +1 -0
  96. package/dist/conversational-flow/read.d.ts +9 -17
  97. package/dist/conversational-flow/read.d.ts.map +1 -1
  98. package/dist/conversational-flow/read.js +217 -384
  99. package/dist/conversational-flow/read.js.map +1 -1
  100. package/dist/conversational-flow/rounds.d.ts +29 -0
  101. package/dist/conversational-flow/rounds.d.ts.map +1 -0
  102. package/dist/conversational-flow/rounds.js +41 -0
  103. package/dist/conversational-flow/rounds.js.map +1 -0
  104. package/dist/conversational-flow/routers.d.ts +24 -80
  105. package/dist/conversational-flow/routers.d.ts.map +1 -1
  106. package/dist/conversational-flow/routers.js +76 -262
  107. package/dist/conversational-flow/routers.js.map +1 -1
  108. package/dist/conversational-flow/settle.d.ts +21 -15
  109. package/dist/conversational-flow/settle.d.ts.map +1 -1
  110. package/dist/conversational-flow/settle.js +240 -38
  111. package/dist/conversational-flow/settle.js.map +1 -1
  112. package/dist/conversational-flow/shape.d.ts +114 -0
  113. package/dist/conversational-flow/shape.d.ts.map +1 -0
  114. package/dist/conversational-flow/shape.js +209 -0
  115. package/dist/conversational-flow/shape.js.map +1 -0
  116. package/dist/conversational-flow/testing.d.ts +54 -0
  117. package/dist/conversational-flow/testing.d.ts.map +1 -0
  118. package/dist/conversational-flow/testing.js +119 -0
  119. package/dist/conversational-flow/testing.js.map +1 -0
  120. package/dist/conversational-flow/tree.d.ts +28 -17
  121. package/dist/conversational-flow/tree.d.ts.map +1 -1
  122. package/dist/conversational-flow/tree.js +60 -47
  123. package/dist/conversational-flow/tree.js.map +1 -1
  124. package/dist/conversational-flow/turn.d.ts +20 -22
  125. package/dist/conversational-flow/turn.d.ts.map +1 -1
  126. package/dist/conversational-flow/turn.js +69 -48
  127. package/dist/conversational-flow/turn.js.map +1 -1
  128. package/dist/conversational-flow/types.d.ts +154 -118
  129. package/dist/conversational-flow/types.d.ts.map +1 -1
  130. package/dist/conversational-flow/types.js +27 -51
  131. package/dist/conversational-flow/types.js.map +1 -1
  132. package/dist/conversational-flow/typing.d.ts +31 -0
  133. package/dist/conversational-flow/typing.d.ts.map +1 -0
  134. package/dist/conversational-flow/typing.js +53 -0
  135. package/dist/conversational-flow/typing.js.map +1 -0
  136. package/dist/engine/variable-resolver.d.ts +16 -0
  137. package/dist/engine/variable-resolver.d.ts.map +1 -1
  138. package/dist/engine/variable-resolver.js +45 -1
  139. package/dist/engine/variable-resolver.js.map +1 -1
  140. package/dist/types/pause-state.d.ts +9 -0
  141. package/dist/types/pause-state.d.ts.map +1 -1
  142. package/package.json +1 -1
  143. package/dist/conversational-flow/edit.d.ts +0 -101
  144. package/dist/conversational-flow/edit.d.ts.map +0 -1
  145. package/dist/conversational-flow/edit.js +0 -785
  146. package/dist/conversational-flow/edit.js.map +0 -1
  147. package/dist/conversational-flow/internals.d.ts +0 -51
  148. package/dist/conversational-flow/internals.d.ts.map +0 -1
  149. package/dist/conversational-flow/internals.js +0 -163
  150. package/dist/conversational-flow/internals.js.map +0 -1
  151. package/dist/conversational-flow/lookup-edit.d.ts +0 -96
  152. package/dist/conversational-flow/lookup-edit.d.ts.map +0 -1
  153. package/dist/conversational-flow/lookup-edit.js +0 -266
  154. package/dist/conversational-flow/lookup-edit.js.map +0 -1
  155. package/dist/conversational-flow/lookups.d.ts +0 -46
  156. package/dist/conversational-flow/lookups.d.ts.map +0 -1
  157. package/dist/conversational-flow/lookups.js +0 -145
  158. package/dist/conversational-flow/lookups.js.map +0 -1
  159. package/dist/conversational-flow/router-edit.d.ts +0 -53
  160. package/dist/conversational-flow/router-edit.d.ts.map +0 -1
  161. package/dist/conversational-flow/router-edit.js +0 -191
  162. package/dist/conversational-flow/router-edit.js.map +0 -1
@@ -1,785 +0,0 @@
1
- import { HANDOFF_SECTION, QUESTION_SEPARATOR } from './read.js';
2
- import { linkerOf, mainMenuLabel } from './tree.js';
3
- import { AGENT_STEP_TYPE, END_MESSAGE_KEY, optionsOf } from './types.js';
4
- import { allStepIds, asArray, asRec, asSteps, caseOf, clamp, clone, containerOf, ConversationalFlowEditError, fieldOf, findStep, firstBackIndex, formFields, list, mutate, optionAt, promptIn, requireFlow, requireScreen, retarget, routerCases, selectOptions, slug, uniqueName, } from './internals.js';
5
- import { agentRouter, pointSubtreeAt, routerFor } from './routers.js';
6
- import { settle } from './settle.js';
7
- // ─── Screens ───
8
- /** Rename a screen. Breadcrumb headers and the router's list follow the new name. */
9
- export function renameScreen(config, screenId, title) {
10
- const trimmed = title.trim();
11
- if (!trimmed)
12
- throw new ConversationalFlowEditError('A screen needs a title.');
13
- return settle(config, (draft) => {
14
- caseOf(draft, screenId)['label'] = trimmed;
15
- });
16
- }
17
- /** "AI sends people here when…" — what the router reads about this screen. */
18
- export function setAbout(config, screenId, about) {
19
- const flow = requireFlow(config);
20
- const screen = requireScreen(flow, screenId);
21
- return settle(config, (draft) => {
22
- const step = aboutCarrier(draft, screen);
23
- const text = about.trim();
24
- if (text)
25
- step.description = text;
26
- else
27
- delete step.description;
28
- });
29
- }
30
- /**
31
- * Add a screen of `type` under `parent` and link the parent to it. It sends
32
- * typed text wherever the rest of the flow does and offers the way back.
33
- */
34
- export function addScreen(config, args) {
35
- const title = args.title.trim();
36
- if (!title)
37
- throw new ConversationalFlowEditError('A screen needs a title.');
38
- const type = args.type ?? 'menu';
39
- const flow = requireFlow(config);
40
- const parent = requireScreen(flow, args.parent);
41
- const parentPrompt = parent.kind === 'handoff' ? undefined : parent.prompts[0];
42
- if (!parentPrompt)
43
- throw new ConversationalFlowEditError(`“${parent.title}” doesn’t show options, so it can’t lead anywhere.`);
44
- const taken = new Set(flow.screens.map((s) => s.id));
45
- const screenId = uniqueName(args.id ?? slug(title), taken);
46
- let next = mutate(config, (draft) => {
47
- const cases = routerCases(draft, flow);
48
- const entry = {
49
- label: title,
50
- condition: { ...asRec(cases[0]?.['condition']), value: screenId },
51
- steps: buildSteps(draft, flow, type, { screenId, title, parent, prompt: '' }),
52
- };
53
- // Keep system screens (routers, ends, hand offs) last, where authors expect them.
54
- const firstSystem = flow.screens.find((s) => s.kind === 'router' || s.kind === 'end' || s.kind === 'handoff');
55
- cases.splice(firstSystem ? firstSystem.caseIndex : cases.length, 0, entry);
56
- // Typed text goes where its parent's does — into the section's router when
57
- // the parent is inside a scoped one, and to the flow's router otherwise.
58
- pointTypedAt(entry['steps'], flow.routeKey, routerFor(flow, parent.id)?.id);
59
- });
60
- // Link from the parent, above its way-back options.
61
- const updated = requireFlow(next);
62
- next = addOption(next, parentPrompt.id, { label: title, screen: screenId }, firstBackIndex(promptIn(updated, parentPrompt.id)));
63
- return { config: settle(next, () => { }, config), screenId };
64
- }
65
- /**
66
- * Change what a screen is. The title, "about" and wording carry over. A screen
67
- * that other screens hang beneath can only become another screen with options.
68
- */
69
- export function setScreenType(config, screenId, type) {
70
- const flow = requireFlow(config);
71
- const screen = requireScreen(flow, screenId);
72
- if (screen.kind === 'router')
73
- throw new ConversationalFlowEditError('The AI router can’t be turned into another kind of screen.');
74
- if (kindMatches(screen.kind, type))
75
- return config;
76
- const children = flow.screens.filter((s) => s.parent === screenId);
77
- const keepsOptions = type === 'menu' || type === 'message' || type === 'questions';
78
- if (!keepsOptions && children.length > 0) {
79
- throw new ConversationalFlowEditError(`Move ${list(children.map((c) => c.title))} somewhere else first — ${children.length === 1 ? 'it is' : 'they are'} only reachable from “${screen.title}”.`);
80
- }
81
- if (screen.kind === 'questions' && type !== 'questions' && screen.prompts.length > 1) {
82
- throw new ConversationalFlowEditError('Remove the follow-up questions first — they only exist inside this screen.');
83
- }
84
- // A Finish sits outside the tree, so on the way back to a screen with options
85
- // its way back comes from whoever leads to it.
86
- const parent = screen.parent ? requireScreen(flow, screen.parent) : linkerOf(flow, screen.id);
87
- const prompt = screen.prompts[0]?.text ?? screen.askAi?.intro ?? screen.endMessage ?? '';
88
- return settle(config, (draft) => {
89
- const entry = routerCases(draft, flow)[screen.caseIndex];
90
- const hadOptions = screen.kind === 'menu' || screen.kind === 'message' || screen.kind === 'questions';
91
- const kept = keepsOptions && hadOptions && screen.prompts[0] ? selectOptions(findStep(draft, screen.prompts[0].id, 'WAIT')) : undefined;
92
- const steps = buildSteps(draft, flow, type, { screenId, title: screen.title, parent, prompt });
93
- // Converting between the three option screens keeps every option the author made.
94
- if (kept)
95
- selectOptions(steps[0]).splice(0, Infinity, ...kept);
96
- if (screen.about && steps[0])
97
- steps[0].description = screen.about;
98
- entry['steps'] = steps;
99
- });
100
- }
101
- /** Remove a screen, every option that led to it, and the router's permission to pick it. */
102
- export function removeScreen(config, screenId) {
103
- const flow = requireFlow(config);
104
- const screen = requireScreen(flow, screenId);
105
- if (screen.id === flow.rootScreen)
106
- throw new ConversationalFlowEditError('The first screen can’t be removed.');
107
- if (screen.kind === 'router')
108
- throw new ConversationalFlowEditError('The AI router can’t be removed; typed messages would have nowhere to go.');
109
- const orphaned = flow.screens.filter((s) => s.parent === screenId);
110
- if (orphaned.length > 0) {
111
- throw new ConversationalFlowEditError(`Move or remove ${list(orphaned.map((s) => s.title))} first — ${orphaned.length === 1 ? 'it is' : 'they are'} only reachable from “${screen.title}”.`);
112
- }
113
- const scoped = flow.screens.find((s) => s.router?.scope === screenId);
114
- return settle(config, (draft) => {
115
- for (const other of flow.screens) {
116
- if (other.id === screenId)
117
- continue;
118
- for (const prompt of other.prompts) {
119
- const options = selectOptions(findStep(draft, prompt.id));
120
- for (let i = options.length - 1; i >= 0; i--) {
121
- const to = prompt.options[i]?.to;
122
- if (to?.kind === 'screen' && to.screen === screenId)
123
- options.splice(i, 1);
124
- }
125
- }
126
- }
127
- // Its own case, and the router that served only it. Highest index first,
128
- // so removing one doesn't move the other.
129
- const cases = routerCases(draft, flow);
130
- for (const at of [screen.caseIndex, scoped?.caseIndex].filter((i) => i !== undefined).sort((a, b) => b - a)) {
131
- cases.splice(at, 1);
132
- }
133
- });
134
- }
135
- /**
136
- * Hang a screen beneath another. The old parent stops linking to it, the new one
137
- * starts, and the way back from it — and from everything beneath it — follows.
138
- */
139
- export function moveScreen(config, screenId, newParentId) {
140
- const flow = requireFlow(config);
141
- const screen = requireScreen(flow, screenId);
142
- const newParent = requireScreen(flow, newParentId);
143
- if (screen.id === flow.rootScreen)
144
- throw new ConversationalFlowEditError('The first screen can’t be moved.');
145
- if (newParent.id === screenId || isBeneath(flow, newParentId, screenId)) {
146
- throw new ConversationalFlowEditError('A screen can’t move beneath itself.');
147
- }
148
- const target = newParent.kind === 'handoff' ? undefined : newParent.prompts[0];
149
- if (!target)
150
- throw new ConversationalFlowEditError(`“${newParent.title}” doesn’t show options, so it can’t lead anywhere.`);
151
- if (screen.parent === newParentId)
152
- return config;
153
- let next = mutate(config, (draft) => {
154
- const old = screen.parent ? requireScreen(flow, screen.parent) : undefined;
155
- for (const prompt of old?.prompts ?? []) {
156
- const options = selectOptions(findStep(draft, prompt.id));
157
- for (let i = options.length - 1; i >= 0; i--) {
158
- const o = prompt.options[i];
159
- if (o && !o.back && o.to.kind === 'screen' && o.to.screen === screenId)
160
- options.splice(i, 1);
161
- }
162
- }
163
- });
164
- next = addOption(next, target.id, { label: screen.title, screen: screenId }, firstBackIndex(target));
165
- // It has joined another part of the tree, so its typed text follows the
166
- // router that serves its new home — as its way back follows its new parent.
167
- next = mutate(next, (draft) => {
168
- const moved = requireFlow(next);
169
- pointSubtreeAt(draft, moved, screenId, routerFor(moved, newParentId)?.id ?? '');
170
- });
171
- // The old way back (↑ the old parent, …) no longer leads back once moved, so
172
- // it has to be recognised from before the move to be replaced.
173
- const formerlyBack = new Map();
174
- for (const s of flow.screens) {
175
- if (s.id !== screenId && !isBeneath(flow, s.id, screenId))
176
- continue;
177
- for (const p of s.prompts)
178
- formerlyBack.set(p.id, new Set(p.options.filter((o) => o.back).map((o) => o.value)));
179
- }
180
- next = refreshWayBack(next, screenId, { formerlyBack });
181
- return settle(next, () => { }, config);
182
- }
183
- /** Offer (or stop offering) the way back — ↑ each ancestor, then the first screen. */
184
- export function setWayBack(config, screenId, on) {
185
- const flow = requireFlow(config);
186
- const screen = requireScreen(flow, screenId);
187
- if (screen.kind === 'ask-ai' && !on) {
188
- throw new ConversationalFlowEditError('An Ask AI screen needs a way out; its exits are the way back.');
189
- }
190
- if (screen.kind === 'handoff')
191
- throw new ConversationalFlowEditError(HANDOFF_OPTIONS);
192
- if (on)
193
- return refreshWayBack(config, screenId, { subtree: false });
194
- return mutate(config, (draft) => {
195
- for (const prompt of screen.prompts) {
196
- const options = selectOptions(findStep(draft, prompt.id));
197
- for (let i = prompt.options.length - 1; i >= 0; i--)
198
- if (prompt.options[i].back)
199
- options.splice(i, 1);
200
- }
201
- });
202
- }
203
- /**
204
- * Whether a screen takes typed text, or only its options.
205
- *
206
- * Nothing records this: a screen takes typing because its WAIT has a text field
207
- * and a CONDITIONAL sends what was typed to the router. Turning it off removes
208
- * both, which is what every layer below already reads — the turn answers
209
- * `reply.text.allowed: false`, and text sent anyway is refused.
210
- *
211
- * Turning it back on copies the field and the typed branch from a screen that
212
- * still has them, the way a new screen is built; with none left, both are
213
- * rebuilt from the CYCLE's own input keys.
214
- */
215
- export function setTypingAllowed(config, screenId, on) {
216
- const flow = requireFlow(config);
217
- const screen = requireScreen(flow, screenId);
218
- if (screen.kind === 'ask-ai') {
219
- throw new ConversationalFlowEditError('An Ask AI screen is typing — that is what it does. Change the screen’s type instead.');
220
- }
221
- if (screen.prompts.length === 0)
222
- throw new ConversationalFlowEditError(`“${screen.title}” asks nothing, so there is nothing to type into.`);
223
- if (!on && optionsOf(screen).length === 0) {
224
- throw new ConversationalFlowEditError(`“${screen.title}” offers no options, so turning typing off would leave no way to answer it.`);
225
- }
226
- return mutate(config, (draft) => {
227
- const taken = allStepIds(draft);
228
- for (const prompt of screen.prompts) {
229
- if (on === (prompt.textField !== undefined))
230
- continue;
231
- const wait = findStep(draft, prompt.id, 'WAIT');
232
- const fields = formFields(wait);
233
- const around = containerOf(draft, prompt.id);
234
- const at = around.findIndex((step) => asRec(step)['id'] === prompt.id);
235
- if (!on) {
236
- // The gate tests a field that is about to go, so what it guarded moves up.
237
- const gate = asRec(around[at + 1]);
238
- if (gate['type'] === 'CONDITIONAL')
239
- around.splice(at + 1, 1, ...asArray(asRec(gate['config'])['if_false']).map(asRec));
240
- fields.splice(0, Infinity, ...fields.filter((f) => f['type'] !== 'text' && f['type'] !== 'textarea'));
241
- continue;
242
- }
243
- const typing = typedShape(draft, flow, prompt.id);
244
- // Copied from another screen, so it may name another section's router.
245
- pointTypedAt([typing.typedJump], flow.routeKey, routerFor(flow, screen.id)?.id);
246
- fields.push(typing.field);
247
- const gate = typing.gate;
248
- gate.id = uniqueName(`typed_${screen.id}`, taken);
249
- if (typing.typedJump) {
250
- typing.typedJump.id = uniqueName(`route_${screen.id}`, taken);
251
- gate.config['if_true'] = [typing.typedJump];
252
- }
253
- else {
254
- gate.config['if_true'] = [];
255
- }
256
- gate.config['if_false'] = around.splice(at + 1);
257
- around.push(gate);
258
- }
259
- });
260
- }
261
- /**
262
- * Point whichever GO_TO carries a typed message at `routerId` — a screen's
263
- * typed branch names the router that serves it, and a copied branch names
264
- * whichever router the screen it was copied from used.
265
- */
266
- function pointTypedAt(steps, routeKey, routerId) {
267
- if (!routerId)
268
- return;
269
- for (const step of asSteps(steps)) {
270
- if (step.type === 'GO_TO') {
271
- // A literal screen id is a router link. An expression is a screen taking
272
- // the person where their own answer said — an Ask AI's way out, say.
273
- const carried = asRec(step.config['with']);
274
- const current = carried[routeKey];
275
- if (typeof current === 'string' && !current.includes('{{'))
276
- carried[routeKey] = routerId;
277
- continue;
278
- }
279
- if (step.type === 'CONDITIONAL')
280
- pointTypedAt(asRec(step.config)['if_true'], routeKey, routerId);
281
- }
282
- }
283
- /**
284
- * What a screen that takes typing is made of, built for `waitId`: the text
285
- * field, the CONDITIONAL that splits typed from tapped, and the GO_TO carrying
286
- * the message to the router.
287
- *
288
- * Copied from a screen that still takes typing, so the flow's own field names
289
- * and keys are kept. With none left to copy — the author turned typing off
290
- * everywhere — the same shape is built from the CYCLE's input keys and the
291
- * router's id, which is all it ever depended on.
292
- */
293
- function typedShape(draft, flow, waitId) {
294
- for (const s of flow.screens) {
295
- for (const p of s.prompts) {
296
- if (!p.textField || p.freeText?.kind !== 'screen' || p.id === waitId)
297
- continue;
298
- const gate = asSteps(containerOf(draft, p.id)).find((step) => step.type === 'CONDITIONAL');
299
- const field = formFields(findStep(draft, p.id, 'WAIT')).find((f) => f['name'] === p.textField);
300
- if (!gate || !field)
301
- continue;
302
- const copy = retarget(clone(gate), p.id, waitId);
303
- const typedJump = asSteps(asRec(copy.config)['if_true'])[0];
304
- copy.config['if_true'] = [];
305
- copy.config['if_false'] = [];
306
- return { field: clone(field), gate: copy, typedJump };
307
- }
308
- }
309
- const router = flow.screens.find((s) => s.kind === 'router');
310
- if (!router)
311
- throw new ConversationalFlowEditError('This flow has no router, so typed messages would have nowhere to go.');
312
- const textKey = Object.keys(asRec(findStep(draft, flow.cycleId, 'CYCLE').config['input'])).find((k) => k !== flow.routeKey);
313
- if (!textKey)
314
- throw new ConversationalFlowEditError('This flow carries no typed text, so a screen cannot take any.');
315
- const name = 'message';
316
- const ref = `{{steps.${waitId}.output.resumeData.data.${name}}}`;
317
- return {
318
- field: { type: 'text', name, label: 'Or type your question' },
319
- gate: {
320
- id: '',
321
- type: 'CONDITIONAL',
322
- config: {
323
- condition: { all: [{ variable: ref, operator: 'exists' }, { variable: ref, operator: 'neq', value: '' }] },
324
- if_true: [],
325
- if_false: [],
326
- },
327
- },
328
- typedJump: {
329
- id: '',
330
- type: 'GO_TO',
331
- config: { target: flow.cycleId, with: { [flow.routeKey]: router.id, [textKey]: ref } },
332
- },
333
- };
334
- }
335
- /** What a Finish screen returns as the run's outcome. */
336
- export function setEndResult(config, screenId, value) {
337
- const flow = requireFlow(config);
338
- const screen = requireScreen(flow, screenId);
339
- if (screen.kind !== 'end')
340
- throw new ConversationalFlowEditError(`“${screen.title}” isn’t a Finish screen.`);
341
- return mutate(config, (draft) => {
342
- const finish = asSteps(routerCases(draft, flow)[screen.caseIndex]['steps'])[0];
343
- // The farewell rides in the same object, so editing the result keeps it.
344
- const { [END_MESSAGE_KEY]: message } = asRec(finish.config['with']);
345
- finish.config['with'] = { ...value, ...(typeof message === 'string' && message ? { [END_MESSAGE_KEY]: message } : {}) };
346
- });
347
- }
348
- /**
349
- * What a Finish screen says as it closes the conversation. Empty for a silent
350
- * one — the conversation simply ends, as it always did.
351
- */
352
- export function setEndMessage(config, screenId, message) {
353
- const flow = requireFlow(config);
354
- const screen = requireScreen(flow, screenId);
355
- if (screen.kind !== 'end')
356
- throw new ConversationalFlowEditError(`“${screen.title}” isn’t a Finish screen.`);
357
- return mutate(config, (draft) => {
358
- const finish = asSteps(routerCases(draft, flow)[screen.caseIndex]['steps'])[0];
359
- const carried = asRec(finish.config['with']);
360
- const text = message.trim();
361
- if (text)
362
- carried[END_MESSAGE_KEY] = text;
363
- else
364
- delete carried[END_MESSAGE_KEY];
365
- finish.config['with'] = carried;
366
- });
367
- }
368
- // ─── The AI router and Ask AI ───
369
- export function setAskAi(config, screenId, patch) {
370
- const flow = requireFlow(config);
371
- const screen = requireScreen(flow, screenId);
372
- if (!screen.askAi)
373
- throw new ConversationalFlowEditError(`“${screen.title}” isn’t an Ask AI screen.`);
374
- return mutate(config, (draft) => {
375
- const step = findStep(draft, screen.askAi.stepId, AGENT_STEP_TYPE);
376
- if (patch.agent !== undefined)
377
- step.config['agentSlug'] = patch.agent.trim();
378
- if (patch.task !== undefined) {
379
- // The instructions change; the question the person typed stays appended.
380
- const task = typeof step.config['task'] === 'string' ? step.config['task'] : '';
381
- const at = task.lastIndexOf(QUESTION_SEPARATOR);
382
- step.config['task'] = patch.task + (at === -1 ? '' : task.slice(at));
383
- }
384
- if (patch.intro !== undefined)
385
- findStep(draft, screen.prompts[0].id, 'WAIT').config['prompt'] = patch.intro;
386
- });
387
- }
388
- // ─── Prompts ───
389
- export function setPromptText(config, promptId, text) {
390
- return mutate(config, (draft) => {
391
- findStep(draft, promptId, 'WAIT').config['prompt'] = text;
392
- });
393
- }
394
- /**
395
- * Add an answer to a question that opens a follow-up question, asked in the
396
- * same round. The prompt must already branch on its answers (a Questions screen).
397
- */
398
- export function addFollowUp(config, promptId, args) {
399
- const flow = requireFlow(config);
400
- const prompt = promptIn(flow, promptId);
401
- if (!prompt.pickSwitchId) {
402
- throw new ConversationalFlowEditError('Only a Questions screen can ask a follow-up. Change the screen’s type first.');
403
- }
404
- const label = args.label.trim();
405
- if (!label)
406
- throw new ConversationalFlowEditError('An answer needs a label.');
407
- const screen = flow.screens.find((s) => s.prompts.some((p) => p.id === promptId));
408
- const value = uniqueName(slug(label), new Set(prompt.options.map((o) => o.value)));
409
- let newPromptId = '';
410
- const next = mutate(config, (draft) => {
411
- const taken = allStepIds(draft);
412
- const wait = findStep(draft, promptId, 'WAIT');
413
- const options = selectOptions(wait);
414
- options.splice(firstBackIndex(prompt), 0, { label, value });
415
- const pick = findStep(draft, prompt.pickSwitchId, 'SWITCH');
416
- const template = templateScreen(draft, flow);
417
- const followUp = buildPromptSteps(template, {
418
- waitId: uniqueName(`ask_${screen.id}__${value}`, taken),
419
- suffix: `${screen.id}__${value}`,
420
- prompt: args.question,
421
- options: prompt.options.filter((o) => o.back).map((o) => ({ label: o.label, value: o.value })),
422
- header: prompt.header,
423
- branches: true,
424
- taken,
425
- });
426
- newPromptId = followUp[0].id;
427
- const choiceRef = `{{steps.${promptId}.output.resumeData.data.${prompt.choiceField ?? 'choice'}}}`;
428
- const cases = asArray(pick.config['cases']);
429
- cases.push({ label: value, condition: { variable: choiceRef, operator: 'eq', value }, steps: followUp });
430
- pick.config['cases'] = cases;
431
- });
432
- return { config: next, promptId: newPromptId };
433
- }
434
- // ─── Options ───
435
- /** Add an option that goes to a screen. */
436
- export function addOption(config, promptId, option, at) {
437
- const flow = requireFlow(config);
438
- requireScreen(flow, option.screen);
439
- requireNotHandoff(flow, promptId);
440
- const prompt = promptIn(flow, promptId);
441
- requireForwardsPick(prompt);
442
- if (prompt.options.some((o) => o.value === option.screen)) {
443
- throw new ConversationalFlowEditError('This question already has an option that goes there.');
444
- }
445
- return mutate(config, (draft) => {
446
- const options = selectOptions(findStep(draft, promptId, 'WAIT'));
447
- const entry = { label: option.label.trim() || option.screen, value: option.screen };
448
- options.splice(at === undefined ? options.length : clamp(at, 0, options.length), 0, entry);
449
- });
450
- }
451
- export function relabelOption(config, promptId, index, label) {
452
- const trimmed = label.trim();
453
- if (!trimmed)
454
- throw new ConversationalFlowEditError('An option needs a label.');
455
- requireNotHandoff(requireFlow(config), promptId);
456
- return mutate(config, (draft) => {
457
- optionAt(findStep(draft, promptId, 'WAIT'), index)['label'] = trimmed;
458
- });
459
- }
460
- /**
461
- * Point an option at another screen. For a plain screen the option's value *is*
462
- * its destination, so this rewrites the value. An option that opens a
463
- * follow-up question can't be repointed — that would drop the question.
464
- */
465
- export function retargetOption(config, promptId, index, screen) {
466
- const flow = requireFlow(config);
467
- requireScreen(flow, screen);
468
- requireNotHandoff(flow, promptId);
469
- const prompt = promptIn(flow, promptId);
470
- const current = prompt.options[index];
471
- if (!current)
472
- throw new ConversationalFlowEditError('No such option.');
473
- if (current.to.kind !== 'screen') {
474
- throw new ConversationalFlowEditError('This option opens a follow-up question. Remove the question to point it somewhere else.');
475
- }
476
- requireForwardsPick(prompt);
477
- if (prompt.options.some((o, i) => i !== index && o.value === screen)) {
478
- throw new ConversationalFlowEditError('Another option already goes there.');
479
- }
480
- requireNotSoleLink(flow, promptId, current);
481
- return settle(config, (draft) => {
482
- optionAt(findStep(draft, promptId, 'WAIT'), index)['value'] = screen;
483
- });
484
- }
485
- export function removeOption(config, promptId, index) {
486
- const flow = requireFlow(config);
487
- requireNotHandoff(flow, promptId);
488
- const prompt = promptIn(flow, promptId);
489
- const option = prompt.options[index];
490
- if (!option)
491
- throw new ConversationalFlowEditError('No such option.');
492
- if (option.to.kind === 'prompt') {
493
- throw new ConversationalFlowEditError('This option opens a follow-up question. Remove the question instead.');
494
- }
495
- requireNotSoleLink(flow, promptId, option);
496
- return mutate(config, (draft) => {
497
- selectOptions(findStep(draft, promptId, 'WAIT')).splice(index, 1);
498
- });
499
- }
500
- export function moveOption(config, promptId, from, to) {
501
- requireNotHandoff(requireFlow(config), promptId);
502
- return mutate(config, (draft) => {
503
- const options = selectOptions(findStep(draft, promptId, 'WAIT'));
504
- if (!options[from])
505
- throw new ConversationalFlowEditError('No such option.');
506
- const [moved] = options.splice(from, 1);
507
- options.splice(clamp(to, 0, options.length), 0, moved);
508
- });
509
- }
510
- /**
511
- * A plain screen to copy the shape from — the root if it is plain, else the
512
- * first plain screen. Its WAIT fields, its typed-text gate and the keys its
513
- * GO_TOs carry all come across; only ids, texts and options change.
514
- */
515
- function templateScreen(draft, flow) {
516
- const plain = [flow.screens.find((s) => s.id === flow.rootScreen), ...flow.screens].find((s) => !!s && (s.kind === 'menu' || s.kind === 'message'));
517
- if (!plain)
518
- throw new ConversationalFlowEditError('This flow has no plain screen to model a new one on.');
519
- const steps = asSteps(routerCases(draft, flow)[plain.caseIndex]['steps']);
520
- const [wait, second] = steps;
521
- const gate = second?.type === 'CONDITIONAL' ? second : undefined;
522
- const picked = gate ? asSteps(gate.config['if_false']) : steps.slice(1);
523
- const typed = gate ? asSteps(gate.config['if_true']) : [];
524
- const pickJump = picked.find((s) => s.type === 'GO_TO');
525
- if (!wait || !pickJump)
526
- throw new ConversationalFlowEditError('This flow has no plain screen to model a new one on.');
527
- return { wait, gate, typedJump: typed.find((s) => s.type === 'GO_TO'), pickJump };
528
- }
529
- function buildSteps(draft, flow, type, args) {
530
- const taken = allStepIds(draft);
531
- const back = args.parent ? wayBack(flow, args.parent) : [];
532
- if (type === 'end') {
533
- // Whatever the screen said becomes its last word, so turning a Message into
534
- // a Finish keeps its wording instead of silently dropping it.
535
- const carried = { ...(args.prompt ? { [END_MESSAGE_KEY]: args.prompt } : {}), outcome: args.screenId };
536
- return [{
537
- id: uniqueName(`finish_${args.screenId}`, taken),
538
- type: 'FINISH',
539
- config: { target: flow.cycleId, with: carried },
540
- }];
541
- }
542
- const template = templateScreen(draft, flow);
543
- if (type === 'handoff') {
544
- // A person has it from here. The run pauses on a WAIT only they answer —
545
- // no text box, since the customer is talking to them now — and a GO_TO
546
- // carries their answer on: a Finish closes it, a screen sends it back.
547
- // The options are written by `settle` (writeHandoffs).
548
- const waitId = uniqueName(`handoff_${args.screenId}`, taken);
549
- const wait = clone(template.wait);
550
- wait.id = waitId;
551
- delete wait.description;
552
- wait.config['prompt'] = '';
553
- delete wait.config['reason'];
554
- wait.config['display'] = [{ id: HANDOFF_SECTION, label: 'Handed off to a person', value: '' }];
555
- const form = asRec(wait.config['form']);
556
- form['fields'] = asArray(form['fields']).map(asRec).filter((f) => f['type'] === 'select').map((f) => ({ ...f, options: [] }));
557
- const go = retarget(clone(template.pickJump), template.wait.id, waitId);
558
- go.id = uniqueName(`go_${args.screenId}`, taken);
559
- return [wait, go];
560
- }
561
- if (type === 'ask-ai') {
562
- // Opens like any screen — the intro, the ways out, a text box — and a typed
563
- // question goes to an agent that answers, then waits for a reply
564
- // (`waitForReply`): a reply continues the conversation; picking one of this
565
- // screen's ways out ends it and moves on there.
566
- if (!template.gate)
567
- throw new ConversationalFlowEditError('Ask AI needs a flow that takes typed messages.');
568
- const waitId = uniqueName(`ask_${args.screenId}`, taken);
569
- const steps = buildPromptSteps(template, {
570
- waitId,
571
- suffix: args.screenId,
572
- prompt: args.prompt || `Ask me anything about ${args.title.toLowerCase()}.`,
573
- options: back,
574
- header: undefined,
575
- branches: false,
576
- taken,
577
- });
578
- const textField = fieldOf(template.wait, 'text') ?? 'message';
579
- const agentId = uniqueName(`ai_${args.screenId}`, taken);
580
- const leave = retarget(clone(template.pickJump), template.wait.id, agentId);
581
- leave.id = uniqueName(`leave_${args.screenId}`, taken);
582
- const carried = asRec(leave.config['with']);
583
- for (const key of Object.keys(carried))
584
- if (key !== flow.routeKey)
585
- carried[key] = '';
586
- carried[flow.routeKey] = `{{steps.${agentId}.output.choice}}`;
587
- steps[1].config['if_true'] = [
588
- {
589
- id: agentId,
590
- type: AGENT_STEP_TYPE,
591
- config: {
592
- agentSlug: agentRouter(flow)?.router?.agent ?? '',
593
- task: `Answer their questions about ${args.title}. Use your connected systems and sources, and say so plainly when you are not sure.`
594
- + QUESTION_SEPARATOR
595
- + `{{steps.${waitId}.output.resumeData.data.${textField}}}`,
596
- outputType: 'string',
597
- waitForReply: true,
598
- },
599
- },
600
- leave,
601
- ];
602
- return steps;
603
- }
604
- const options = [...back];
605
- if (type === 'message') {
606
- const end = sharedExit(flow, args.screenId);
607
- if (end)
608
- options.push({ label: 'That’s all', value: end.id });
609
- }
610
- return buildPromptSteps(template, {
611
- waitId: uniqueName(`ask_${args.screenId}`, taken),
612
- suffix: args.screenId,
613
- prompt: args.prompt,
614
- options,
615
- header: undefined,
616
- branches: type === 'questions',
617
- taken,
618
- });
619
- }
620
- /**
621
- * WAIT (+ typed-text gate) + what a pick does: forwarded as the next screen,
622
- * or — for questions — branched by a SWITCH whose default forwards the pick.
623
- */
624
- function buildPromptSteps(t, args) {
625
- const rename = (step, base) => {
626
- const copy = retarget(clone(step), t.wait.id, args.waitId);
627
- copy.id = uniqueName(`${base}_${args.suffix}`, args.taken);
628
- return copy;
629
- };
630
- const wait = clone(t.wait);
631
- wait.id = args.waitId;
632
- delete wait.description;
633
- wait.config['prompt'] = args.prompt;
634
- // No header unless given: the refresh after insertion writes the breadcrumb.
635
- if (args.header)
636
- wait.config['reason'] = args.header;
637
- else
638
- delete wait.config['reason'];
639
- selectOptions(wait).splice(0, Infinity, ...args.options);
640
- const pick = rename(t.pickJump, args.branches ? 'leave' : 'go');
641
- const picked = args.branches
642
- ? [{ id: uniqueName(`pick_${args.suffix}`, args.taken), type: 'SWITCH', config: { cases: [], default: [pick] } }]
643
- : [pick];
644
- if (!t.gate)
645
- return [wait, ...picked];
646
- const gate = rename(t.gate, 'typed');
647
- gate.config['if_true'] = t.typedJump ? [rename(t.typedJump, 'route')] : [];
648
- gate.config['if_false'] = picked;
649
- return [wait, gate];
650
- }
651
- /**
652
- * The ending a new Message screen offers as "That's all": a way out of the
653
- * conversation that belongs to no branch in particular.
654
- *
655
- * A Finish that says something ("Your driver is 3 minutes away") is that
656
- * branch's own ending, and offering it from an unrelated screen would put words
657
- * in its mouth. So: a silent Finish first, then whichever ending the most
658
- * screens already lead to, and none at all when every ending belongs to
659
- * somewhere — the screen simply keeps its way back.
660
- */
661
- function sharedExit(flow, exclude) {
662
- const ends = flow.screens.filter((s) => s.kind === 'end' && s.id !== exclude);
663
- const silent = ends.filter((s) => s.endMessage === undefined);
664
- if (silent.length > 0)
665
- return silent[0];
666
- const leads = (id) => flow.screens.filter((s) => optionsOf(s).some((o) => o.to.kind === 'screen' && o.to.screen === id)).length;
667
- const shared = ends.filter((s) => leads(s.id) > 1).sort((a, b) => leads(b.id) - leads(a.id));
668
- return shared[0];
669
- }
670
- /**
671
- * The options that lead back from a new child of `parent`: back to the parent
672
- * itself, then whatever way back the parent already offers. Labels follow the
673
- * flow's own wording — if the parent writes "↑ Payments", the child writes
674
- * "↑ <parent>".
675
- */
676
- function wayBack(flow, parent) {
677
- const inherited = optionsOf(parent).filter((o) => o.back);
678
- if (parent.id === flow.rootScreen) {
679
- const root = flow.screens.find((s) => s.id === flow.rootScreen);
680
- return [{ label: mainMenuLabel(flow) ?? root.title, value: root.id }];
681
- }
682
- const byId = new Map(flow.screens.map((s) => [s.id, s]));
683
- const example = inherited.find((o) => {
684
- const target = o.to.kind === 'screen' ? byId.get(o.to.screen) : undefined;
685
- return target && target.id !== flow.rootScreen && o.label.endsWith(target.title);
686
- });
687
- const prefix = example && example.to.kind === 'screen'
688
- ? example.label.slice(0, example.label.length - byId.get(example.to.screen).title.length)
689
- : '↑ ';
690
- return [
691
- { label: `${prefix}${parent.title}`, value: parent.id },
692
- ...inherited.map((o) => ({ label: o.label, value: o.value })),
693
- ];
694
- }
695
- /**
696
- * Rewrite the way-back options of `screenId` — and, by default, of everything
697
- * beneath it — from its current place in the tree. Top-down, so each screen
698
- * inherits the freshly written way back of its parent.
699
- */
700
- function refreshWayBack(config, screenId, opts = {}) {
701
- let next = config;
702
- const queue = [screenId];
703
- while (queue.length > 0) {
704
- const id = queue.shift();
705
- const flow = requireFlow(next);
706
- const screen = requireScreen(flow, id);
707
- const parent = screen.parent ? flow.screens.find((s) => s.id === screen.parent) : undefined;
708
- if (parent) {
709
- const fresh = wayBack(flow, parent);
710
- next = mutate(next, (draft) => {
711
- for (const prompt of screen.prompts) {
712
- const options = selectOptions(findStep(draft, prompt.id));
713
- const stale = opts.formerlyBack?.get(prompt.id);
714
- const isWayBack = (o) => o.back || (stale?.has(o.value) ?? false);
715
- const at = Math.min(firstBackIndex(prompt), ...prompt.options.map((o, i) => (isWayBack(o) ? i : Infinity)));
716
- for (let i = prompt.options.length - 1; i >= 0; i--)
717
- if (isWayBack(prompt.options[i]))
718
- options.splice(i, 1);
719
- const kept = new Set(options.map((o) => o['value']));
720
- options.splice(Math.min(at, options.length), 0, ...fresh.filter((o) => !kept.has(o['value'])));
721
- }
722
- });
723
- }
724
- if (opts.subtree !== false) {
725
- for (const child of requireFlow(next).screens.filter((s) => s.parent === id))
726
- queue.push(child.id);
727
- }
728
- }
729
- return next;
730
- }
731
- // ─── Derived: headers and the router ───
732
- // ─── Lookups ───
733
- const HANDOFF_OPTIONS = 'A hand off can be sent to every screen; its options follow the flow on their own.';
734
- /** A hand off's options are generated; they aren't edited one by one. */
735
- function requireNotHandoff(flow, promptId) {
736
- if (flow.screens.some((s) => s.kind === 'handoff' && s.prompts.some((p) => p.id === promptId))) {
737
- throw new ConversationalFlowEditError(HANDOFF_OPTIONS);
738
- }
739
- }
740
- /** Options can only name a screen when the prompt forwards the pick as the next screen. */
741
- function requireForwardsPick(prompt) {
742
- if (!prompt.choiceField)
743
- throw new ConversationalFlowEditError('This question has no options to choose from.');
744
- const forwards = prompt.options.length === 0 || prompt.pickSwitchId !== undefined || prompt.options.some((o) => o.to.kind === 'screen');
745
- if (!forwards) {
746
- throw new ConversationalFlowEditError('Options on this question each open a follow-up. Edit them in the step view.');
747
- }
748
- }
749
- /**
750
- * Refuse to drop or repoint the option a child screen hangs from: the screen
751
- * would be left with no way in.
752
- */
753
- function requireNotSoleLink(flow, promptId, option) {
754
- if (option.to.kind !== 'screen' || option.back)
755
- return;
756
- const target = flow.screens.find((s) => s.id === option.to.screen);
757
- const owner = flow.screens.find((s) => s.prompts.some((p) => p.id === promptId));
758
- if (target && owner && target.parent === owner.id) {
759
- throw new ConversationalFlowEditError(`“${target.title}” is only reachable through this option. Move or remove it first.`);
760
- }
761
- }
762
- /** The step that carries a screen's "about": its first WAIT, or a hand off's FINISH. */
763
- function aboutCarrier(draft, screen) {
764
- if (screen.prompts[0])
765
- return findStep(draft, screen.prompts[0].id, 'WAIT');
766
- // A Finish has no question to carry it; its own step does. Writing one is
767
- // also what lets the router end a conversation here — see `endsByTyping`.
768
- if (screen.kind === 'end')
769
- return asSteps(caseOf(draft, screen.id)['steps'])[0];
770
- throw new ConversationalFlowEditError(`The AI router doesn’t send people to “${screen.title}”.`);
771
- }
772
- function kindMatches(kind, type) {
773
- if (type === 'menu' || type === 'message')
774
- return kind === 'menu' || kind === 'message';
775
- return kind === type;
776
- }
777
- function isBeneath(flow, id, ancestor) {
778
- const parent = new Map(flow.screens.map((s) => [s.id, s.parent]));
779
- for (let at = parent.get(id); at !== undefined; at = parent.get(at))
780
- if (at === ancestor)
781
- return true;
782
- return false;
783
- }
784
- // ─── Utilities ───
785
- //# sourceMappingURL=edit.js.map