@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,59 +1,261 @@
1
- import { breadcrumb, mainMenuLabel, treeOrder } from './tree.js';
2
- import { findStep, mutate, requireFlow, selectOptions } from './internals.js';
3
- import { repairTypedRouting, writeRouters } from './routers.js';
4
- /** Recompute what is derived from the screens (headers, the router) without any other change. */
1
+ import { asArray, asRec } from './json.js';
2
+ import { writeJumps } from './jumps.js';
3
+ import { caseFor, cycleAt, sectionCaseFor } from './locate.js';
4
+ import { readConversationalFlow } from './read.js';
5
+ import { inheritedInput, textRef } from './rounds.js';
6
+ import { partsOf, sectionCase, stepsIn } from './shape.js';
7
+ import { breadcrumb, mainMenuLabel, subtreeOf, treeOrder } from './tree.js';
8
+ import { repairTypedRouting } from './typing.js';
9
+ export class ConversationalFlowEditError extends Error {
10
+ name = 'ConversationalFlowEditError';
11
+ }
12
+ /** Recompute everything derived, without any other change. */
5
13
  export function refreshDerived(config) {
6
14
  return settle(config, () => { });
7
15
  }
8
16
  /**
9
- * Apply `edit`, then refresh what is derived from the screens: breadcrumb
10
- * headers (only those that were the generated breadcrumb — a header the author
11
- * wrote is left alone) and the AI router's list of screens.
17
+ * Apply `edit`, then make the config true again.
18
+ *
19
+ * A heading the author wrote is left alone: only the ones that still read as
20
+ * the breadcrumb they were given are rewritten.
12
21
  */
13
22
  export function settle(config, edit,
14
- /** The config as the author last saw it, when `config` is already part-way through a multi-step edit. */
23
+ /** The config as the author last saw it, when this is part-way through a larger edit. */
15
24
  baseline = config) {
16
25
  const before = requireFlow(baseline);
17
- const wasGenerated = new Set();
18
- for (const s of before.screens) {
19
- const crumb = breadcrumb(before, s);
20
- for (const p of s.prompts)
21
- if (p.header === crumb)
22
- wasGenerated.add(p.id);
26
+ const generated = new Set();
27
+ for (const screen of before.screens) {
28
+ const heading = headingFor(before, screen);
29
+ for (const prompt of screen.prompts)
30
+ if (prompt.header === heading)
31
+ generated.add(prompt.id);
32
+ }
33
+ const draft = clone(config);
34
+ edit(draft);
35
+ const after = requireFlow(draft);
36
+ writeConditions(draft, after);
37
+ writeFrames(draft, after);
38
+ writeHeaders(draft, after, generated);
39
+ writeRouters(draft, after);
40
+ writeHandoffs(draft, after);
41
+ repairTypedRouting(draft, after);
42
+ writeJumps(draft, after);
43
+ return draft;
44
+ }
45
+ export function requireFlow(config) {
46
+ const flow = readConversationalFlow(config);
47
+ if (!flow)
48
+ throw new ConversationalFlowEditError('This workflow is not a conversational flow.');
49
+ return flow;
50
+ }
51
+ // ─── What each pass owns ───
52
+ /**
53
+ * A section opens when the target is anywhere inside it, so its case names its
54
+ * subtree as it stands — this is what makes a jump to a screen four levels down
55
+ * find its way there without anyone writing a path.
56
+ */
57
+ function writeConditions(draft, flow) {
58
+ for (const screen of flow.screens) {
59
+ if (screen.children.length === 0)
60
+ continue;
61
+ const entry = sectionCaseFor(draft, flow, screen);
62
+ if (!entry)
63
+ continue;
64
+ const rebuilt = sectionCase(subtreeOf(flow, screen.id), screen.title, [], flow.routeKey);
65
+ entry['condition'] = rebuilt['condition'];
66
+ entry['label'] = screen.title;
67
+ }
68
+ }
69
+ /**
70
+ * Every frame starts a round with the same keys, each passed in from the round
71
+ * around it. A cycle refuses a jump carrying anything else, so this is what
72
+ * lets a jump name any frame at all.
73
+ */
74
+ function writeFrames(draft, flow) {
75
+ const input = inheritedInput(draft, flow);
76
+ if (Object.keys(input).length === 0)
77
+ return;
78
+ for (const screen of flow.screens) {
79
+ if (screen.children.length === 0 || screen.id === flow.rootScreen)
80
+ continue;
81
+ const cycle = cycleAt(draft, screen.frames, flow.routeKey);
82
+ if (cycle)
83
+ cycle.config['input'] = { ...input };
23
84
  }
24
- const edited = mutate(config, edit);
25
- const after = requireFlow(edited);
26
- return mutate(edited, (draft) => {
27
- for (const s of after.screens) {
28
- const crumb = breadcrumb(after, s);
29
- for (const p of s.prompts) {
30
- if (wasGenerated.has(p.id) || p.header === undefined)
31
- findStep(draft, p.id, 'WAIT').config['reason'] = crumb;
32
- }
85
+ }
86
+ /** The heading above a question: where they are, unless the author said otherwise. */
87
+ function writeHeaders(draft, flow, generated) {
88
+ for (const screen of flow.screens) {
89
+ const heading = headingFor(flow, screen);
90
+ const entry = caseFor(draft, flow, screen);
91
+ if (!entry)
92
+ continue;
93
+ for (const step of stepsIn(partsOf(entry, flow.routeKey).rest, 'WAIT')) {
94
+ if (generated.has(step.id) || step.config['reason'] === undefined)
95
+ step.config['reason'] = heading;
33
96
  }
34
- repairTypedRouting(draft, after);
35
- writeRouters(draft, after);
36
- writeHandoffs(draft, after);
37
- });
97
+ }
38
98
  }
39
99
  /**
40
- * Where a hand off can go once the person is done: a Finish closes the
41
- * conversation; any other screen hands it back. Finishes first, then the
42
- * screens as the menu reads — never another hand off.
100
+ * Where they are, as a heading. The first screen is the conversation rather than
101
+ * a place inside it, so it is headed by its own name.
102
+ */
103
+ function headingFor(flow, screen) {
104
+ return breadcrumb(flow, screen) || screen.title;
105
+ }
106
+ /**
107
+ * What a router may choose: the screens its own frame can open. A router at the
108
+ * conversation's level reaches the whole flow; one inside a section reaches that
109
+ * section — which is all scoping is, now that the frame says it.
110
+ */
111
+ function writeRouters(draft, flow) {
112
+ for (const screen of flow.screens) {
113
+ if (!screen.router || screen.router.mode !== 'classifier')
114
+ continue;
115
+ const entry = caseFor(draft, flow, screen);
116
+ const step = entry ? partsOf(entry, flow.routeKey).rest.find((s) => s.id === screen.router?.stepId) : undefined;
117
+ if (!step)
118
+ continue;
119
+ const reachable = openableFrom(flow, screen);
120
+ const classes = {};
121
+ for (const to of reachable)
122
+ classes[to.id] = describe(flow, to);
123
+ step.config['classes'] = classes;
124
+ step.config['instructions'] = instructionsFor(flow, screen, reachable);
125
+ step.config['input'] = textRef(draft, flow);
126
+ // Somewhere to send a message it cannot place, when the author set a bar at
127
+ // all: its own frame, so an unsure message stays where the person is.
128
+ if (typeof step.config['minConfidence'] === 'number')
129
+ step.config['fallbackClass'] = frameOf(flow, screen);
130
+ else
131
+ delete step.config['fallbackClass'];
132
+ }
133
+ for (const screen of flow.screens) {
134
+ if (!screen.router || screen.router.mode !== 'agent')
135
+ continue;
136
+ const entry = caseFor(draft, flow, screen);
137
+ const step = entry ? partsOf(entry, flow.routeKey).rest.find((s) => s.id === screen.router?.stepId) : undefined;
138
+ if (step)
139
+ writeAgentRouter(draft, flow, screen, step, openableFrom(flow, screen));
140
+ }
141
+ }
142
+ /**
143
+ * An agent asked to name the next screen. Unlike a classifier it is told in
144
+ * prose, so the screens it may choose are both in what it is asked and in the
145
+ * shape of the answer it must give.
146
+ */
147
+ function writeAgentRouter(draft, flow, router, step, reachable) {
148
+ const guidance = router.router?.guidance?.trim();
149
+ step.config['task'] = [
150
+ ...(guidance ? [guidance, ''] : []),
151
+ `Someone in this conversation typed: "${textRef(draft, flow)}"`,
152
+ '',
153
+ 'Send them to the screen that best answers it. The screens, with where each sits:',
154
+ ...reachable.map((s) => `${s.id} (${breadcrumb(flow, s) || s.title})${s.children.length > 0 ? ' [section]' : ''} — ${s.about ?? aboutFor(flow, s)}`),
155
+ '',
156
+ 'Prefer the deepest screen that matches; choose a [section] only when the message is too vague for anything inside it.',
157
+ `If nothing fits, choose "${frameOf(flow, router)}".`,
158
+ `Answer with JSON only: {"${flow.routeKey}": "<id from the list>", "reason": "<one short sentence>"}.`,
159
+ ].join('\n');
160
+ step.config['outputType'] = 'json';
161
+ step.config['outputSchema'] = {
162
+ type: 'object',
163
+ properties: {
164
+ [flow.routeKey]: { type: 'string', enum: reachable.map((s) => s.id) },
165
+ reason: { type: 'string' },
166
+ },
167
+ required: [flow.routeKey, 'reason'],
168
+ };
169
+ }
170
+ /** The screen whose frame a router sits in — the conversation itself, or a section. */
171
+ function frameOf(flow, router) {
172
+ return router.frames[router.frames.length - 1] ?? flow.rootScreen;
173
+ }
174
+ /**
175
+ * Where a hand off can go once the person is done: an ending closes the
176
+ * conversation, any other screen hands it back into the flow. Endings first,
177
+ * then the screens as the tree reads — never another hand off.
43
178
  */
44
179
  function writeHandoffs(draft, flow) {
45
180
  const handoffs = flow.screens.filter((s) => s.kind === 'handoff' && s.prompts[0]);
46
181
  if (handoffs.length === 0)
47
182
  return;
48
- const ends = flow.screens.filter((s) => s.kind === 'end');
49
- const canBeShown = (s) => s.kind === 'menu' || s.kind === 'message' || s.kind === 'questions' || s.kind === 'ask-ai';
50
- // Endings are listed first, above; the menu follows, each screen once.
51
- const screens = [...treeOrder(flow).filter(canBeShown), ...flow.screens.filter((s) => s.depth === -1 && canBeShown(s))];
183
+ const answerable = (s) => s.kind === 'menu' || s.kind === 'message' || s.kind === 'questions' || s.kind === 'ask-ai';
52
184
  const options = [
53
- ...ends.map((s) => ({ label: s.title, value: s.id })),
54
- ...screens.map((s) => ({ label: s.id === flow.rootScreen ? (mainMenuLabel(flow) ?? s.title) : breadcrumb(flow, s), value: s.id })),
185
+ ...flow.screens.filter((s) => s.kind === 'end').map((s) => ({ label: s.title, value: s.id })),
186
+ ...treeOrder(flow)
187
+ .filter(answerable)
188
+ .map((s) => ({
189
+ label: s.id === flow.rootScreen ? (mainMenuLabel(flow) ?? s.title) : breadcrumb(flow, s),
190
+ value: s.id,
191
+ })),
55
192
  ];
56
- for (const h of handoffs)
57
- selectOptions(findStep(draft, h.prompts[0].id, 'WAIT')).splice(0, Infinity, ...options);
193
+ for (const handoff of handoffs) {
194
+ const wait = stepIn(draft, flow, handoff, handoff.prompts[0].id);
195
+ if (wait)
196
+ selectOptions(wait).splice(0, Infinity, ...options);
197
+ }
198
+ }
199
+ /** The options of a question, live, so a pass can replace them in place. */
200
+ function selectOptions(wait) {
201
+ const fields = asArray(asRec(wait.config['form'])['fields']).map(asRec);
202
+ const select = fields.find((f) => f['type'] === 'select');
203
+ if (!select)
204
+ return [];
205
+ if (!Array.isArray(select['options']))
206
+ select['options'] = [];
207
+ return select['options'];
208
+ }
209
+ /** One step of a screen's own case. */
210
+ function stepIn(draft, flow, screen, stepId) {
211
+ const entry = caseFor(draft, flow, screen);
212
+ return entry ? partsOf(entry, flow.routeKey).rest.find((s) => s.id === stepId) : undefined;
213
+ }
214
+ /**
215
+ * The screens a router in this frame can send someone to: everything its own
216
+ * switch holds, and everything inside those — but never another router, and
217
+ * never an ending whose author has not said when it applies.
218
+ */
219
+ function openableFrom(flow, router) {
220
+ const frame = frameOf(flow, router);
221
+ const byId = new Map(flow.screens.map((s) => [s.id, s]));
222
+ const inside = subtreeOf(flow, frame).map((id) => byId.get(id));
223
+ // A router inside a section still needs a way out: a message that isn't about
224
+ // this section at all belongs on the first screen, and "talk to a person"
225
+ // works from anywhere. Without those it would have to pick the least bad
226
+ // screen in the section, which is worse than saying "not here".
227
+ const escapes = frame === flow.rootScreen ? [] : [byId.get(flow.rootScreen), ...flow.screens.filter((s) => s.kind === 'handoff')];
228
+ const seen = new Set();
229
+ return [...inside, ...escapes]
230
+ .filter((s) => s !== undefined)
231
+ .filter((s) => s.id !== router.id && s.kind !== 'router')
232
+ .filter((s) => s.kind !== 'end' || (s.about ?? '') !== '')
233
+ .filter((s) => !seen.has(s.id) && seen.add(s.id));
234
+ }
235
+ function describe(flow, screen) {
236
+ if (screen.kind === 'end')
237
+ return `Ends the conversation — ${screen.about ?? 'they are done'}`;
238
+ const section = screen.children.length > 0 ? ' [section]' : '';
239
+ return `${breadcrumb(flow, screen)}${section} — ${screen.about ?? aboutFor(flow, screen)}`;
240
+ }
241
+ /** What to say about a screen whose author has not said anything. */
242
+ function aboutFor(flow, screen) {
243
+ return `the ${breadcrumb(flow, screen) || screen.title} screen`;
244
+ }
245
+ function instructionsFor(flow, router, reachable) {
246
+ const guidance = router.router?.guidance?.trim();
247
+ const list = reachable.map((s) => `${s.id} (${breadcrumb(flow, s)})`).join('\n');
248
+ return [
249
+ guidance,
250
+ 'Which screen answers this message? The screens are a tree; the path is shown in brackets.',
251
+ list,
252
+ 'Prefer the deepest screen that matches.',
253
+ ]
254
+ .filter(Boolean)
255
+ .join('\n\n');
256
+ }
257
+ // ─── Plumbing ───
258
+ function clone(value) {
259
+ return JSON.parse(JSON.stringify(value));
58
260
  }
59
261
  //# sourceMappingURL=settle.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"settle.js","sourceRoot":"","sources":["../../src/conversational-flow/settle.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEjE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC9E,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEhE,iGAAiG;AACjG,MAAM,UAAU,cAAc,CAAC,MAAsB;IACnD,OAAO,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,GA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AAC,EAAE,CAAC;AAC7F,CAAC;AAED,qEAAqE;AACrE,SAAS,QAAQ,CAAC,IAAwB,EAAE,MAAc;IACxD,OAAO,OAAO,UAAU,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,KAAK,SAAS,CAAC;AAClE,CAAC;AAED,SAAS,eAAe,CAAC,IAAwB,EAAE,MAAc,EAAE,SAAmB;IACpF,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;IACjD,MAAM,IAAI,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACjF,OAAO;QACL,QAAQ;QACR,2FAA2F;QAC3F,IAAI;QACJ,yCAAyC;KAC1C;SACE,MAAM,CAAC,OAAO,CAAC;SACf,IAAI,CAAC,MAAM,CAAC,CAAC;AAClB,CAAC;AAED,mBAAmB;AAEnB,SAAS,KAAK,CAAI,KAAQ;IACxB,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAM,CAAC;AAChD,CAAC"}
@@ -0,0 +1,114 @@
1
+ /**
2
+ * The one place that knows what the config looks like.
3
+ *
4
+ * Reading and editing both go through here, so the shape is stated once:
5
+ *
6
+ * a section = a case whose condition is its subtree, holding
7
+ * [its calls…, a CYCLE whose id is the section's screen]
8
+ * a screen = a case whose condition is that screen, holding
9
+ * [its calls…, the WAIT (or FINISH, or the router's steps)]
10
+ * a jump = a GO_TO naming the nearest cycle enclosing both ends
11
+ *
12
+ * A section's CYCLE takes the section's screen id, which is what makes a jump
13
+ * read as `target: 'payments'` — the frame and the screen are the same thing.
14
+ */
15
+ import { type Rec, type Step } from './json.js';
16
+ /** The round-state key a switch tests, and how a condition names it. */
17
+ export declare const DEFAULT_ROUTE_KEY = "screen";
18
+ export declare function screenRef(routeKey?: string): string;
19
+ /** How many rounds one frame may run before it gives up. */
20
+ export declare const MAX_ROUNDS = 200;
21
+ export declare function cycleStep(id: string, input: Rec, body: unknown[]): Step;
22
+ export declare function switchStep(id: string, cases: Rec[], fallback: unknown[]): Step;
23
+ /** A case for one screen: it opens only when that screen is the target. */
24
+ export declare function screenCase(screen: string, label: string, steps: unknown[], routeKey?: string): Rec;
25
+ /**
26
+ * A case for a section: it opens when the target is anywhere inside it, so the
27
+ * descent runs the section's calls on the way to whatever it holds.
28
+ */
29
+ export declare function sectionCase(subtree: readonly string[], label: string, steps: unknown[], routeKey?: string): Rec;
30
+ /** A jump to `screen`, caught by the cycle named `target`. */
31
+ export declare function jumpStep(id: string, target: string, carried: Rec): Step;
32
+ /**
33
+ * What a jump carries: the screen it opens, and every other key the round
34
+ * started with passed on unchanged. A cycle refuses a jump that carries
35
+ * anything else, which is what keeps every frame's state the same shape.
36
+ */
37
+ export declare function carrying(keys: readonly string[], routeKey: string, screen: string): Rec;
38
+ export declare function finishStep(id: string, target: string, value: Rec): Step;
39
+ export type CaseKind = {
40
+ kind: 'screen';
41
+ screen: string;
42
+ } | {
43
+ kind: 'section';
44
+ subtree: string[];
45
+ };
46
+ /**
47
+ * What a case is for, from its condition alone: one screen, or every screen in
48
+ * a section. Anything else is a shape this view does not write and cannot read.
49
+ */
50
+ export declare function caseKind(entry: Rec, routeKey: string): CaseKind | null;
51
+ export declare const isCycle: (step: Step | undefined) => boolean;
52
+ export declare const isCall: (step: Step | undefined) => boolean;
53
+ /** The SWITCH a cycle routes with: the one control step in its body. */
54
+ export declare function switchIn(cycle: Step): Step | undefined;
55
+ /** The cases of a switch, to read. */
56
+ export declare function casesOf(switchStep: Step): Rec[];
57
+ /**
58
+ * The switch's own list of cases, to change — `casesOf` hands back a copy, which
59
+ * is what you want for reading and useless for anything else.
60
+ */
61
+ export declare function caseListIn(switchStep: Step): Rec[];
62
+ /** A call, and where a failed one sends people. */
63
+ export interface CallSteps {
64
+ step: Step;
65
+ /** The screen its guard opens, when it has one. */
66
+ onFail: string | undefined;
67
+ }
68
+ /**
69
+ * The guard after a call: when it did not come back, open a screen instead.
70
+ * An answer that never arrived has no `ok` at all, which is not `true` either.
71
+ */
72
+ export declare function guardTarget(step: Step | undefined, callId: string, routeKey: string): string | undefined;
73
+ /**
74
+ * A case's steps, split where the frame it feeds begins: the calls it makes on
75
+ * the way in — each with the guard that says where a failed one goes — and then
76
+ * whatever they are for.
77
+ *
78
+ * For a section that is its CYCLE; for a screen, its question. Preparation
79
+ * always precedes the thing it prepares, which is what puts a call's answer in
80
+ * scope for it.
81
+ */
82
+ export declare function partsOf(entry: Rec, routeKey?: string): {
83
+ calls: CallSteps[];
84
+ rest: Step[];
85
+ };
86
+ /** The key a cycle starts each round with — how a switch knows what to open. */
87
+ export declare function routeKeyOf(cycle: Step): string | undefined;
88
+ /**
89
+ * Every step of a kind in a run of steps, however deeply branched — through both
90
+ * halves of a conditional and through a switch's cases, which is where a
91
+ * follow-up question and everything it leads to lives.
92
+ */
93
+ export declare function stepsIn(steps: unknown[], type: string, out?: Step[]): Step[];
94
+ /**
95
+ * The gates that tell typing from tapping: a CONDITIONAL on what was typed into
96
+ * a question. A call's guard tests `output.ok` and so is never one of these.
97
+ */
98
+ export declare function typedGatesIn(steps: unknown[]): Step[];
99
+ /** One step by id, anywhere in a run of steps — a follow-up's question included. */
100
+ export declare function stepIn(steps: unknown[], id: string): Step | undefined;
101
+ /**
102
+ * Every jump in a run of steps.
103
+ *
104
+ * One destination is sometimes written as several jumps — the same answer
105
+ * carried on, tested by which frame it lands in — so both halves of the layer
106
+ * need to see them all.
107
+ */
108
+ export declare function jumpsIn(steps: unknown[], out?: Step[]): Step[];
109
+ /** Where a jump sends someone, when it names a literal screen. */
110
+ export declare function jumpTarget(step: Step, routeKey: string): {
111
+ target: string;
112
+ screen: string;
113
+ } | undefined;
114
+ //# sourceMappingURL=shape.d.ts.map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AAgF;AAChF,wBAAgB,UAAU,CAAC,KAAK,EAAE,IAAI,GAAG,MAAM,GAAG,SAAS,CAG1D;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,GAAE,IAAI,EAAO,GAAG,IAAI,EAAE,CAWhF;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,CAOrD;AAED,oFAAoF;AACpF,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,EAAE,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAUrE;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,GAAG,GAAE,IAAI,EAAO,GAAG,IAAI,EAAE,CAElE;AAED,kEAAkE;AAClE,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,CAMvG"}
@@ -0,0 +1,209 @@
1
+ /**
2
+ * The one place that knows what the config looks like.
3
+ *
4
+ * Reading and editing both go through here, so the shape is stated once:
5
+ *
6
+ * a section = a case whose condition is its subtree, holding
7
+ * [its calls…, a CYCLE whose id is the section's screen]
8
+ * a screen = a case whose condition is that screen, holding
9
+ * [its calls…, the WAIT (or FINISH, or the router's steps)]
10
+ * a jump = a GO_TO naming the nearest cycle enclosing both ends
11
+ *
12
+ * A section's CYCLE takes the section's screen id, which is what makes a jump
13
+ * read as `target: 'payments'` — the frame and the screen are the same thing.
14
+ */
15
+ import { asArray, asRec, asSteps, str } from './json.js';
16
+ /** The round-state key a switch tests, and how a condition names it. */
17
+ export const DEFAULT_ROUTE_KEY = 'screen';
18
+ export function screenRef(routeKey = DEFAULT_ROUTE_KEY) {
19
+ return `{{steps.__loop.output.input.${routeKey}}}`;
20
+ }
21
+ /** How many rounds one frame may run before it gives up. */
22
+ export const MAX_ROUNDS = 200;
23
+ // ─── Building ───
24
+ export function cycleStep(id, input, body) {
25
+ return { id, type: 'CYCLE', config: { input, maxIterations: MAX_ROUNDS, body } };
26
+ }
27
+ export function switchStep(id, cases, fallback) {
28
+ return { id, type: 'SWITCH', config: { cases, default: fallback } };
29
+ }
30
+ /** A case for one screen: it opens only when that screen is the target. */
31
+ export function screenCase(screen, label, steps, routeKey = DEFAULT_ROUTE_KEY) {
32
+ return { label, condition: { variable: screenRef(routeKey), operator: 'eq', value: screen }, steps };
33
+ }
34
+ /**
35
+ * A case for a section: it opens when the target is anywhere inside it, so the
36
+ * descent runs the section's calls on the way to whatever it holds.
37
+ */
38
+ export function sectionCase(subtree, label, steps, routeKey = DEFAULT_ROUTE_KEY) {
39
+ return {
40
+ label,
41
+ condition: { any: subtree.map((screen) => ({ variable: screenRef(routeKey), operator: 'eq', value: screen })) },
42
+ steps,
43
+ };
44
+ }
45
+ /** A jump to `screen`, caught by the cycle named `target`. */
46
+ export function jumpStep(id, target, carried) {
47
+ return { id, type: 'GO_TO', config: { target, with: carried } };
48
+ }
49
+ /**
50
+ * What a jump carries: the screen it opens, and every other key the round
51
+ * started with passed on unchanged. A cycle refuses a jump that carries
52
+ * anything else, which is what keeps every frame's state the same shape.
53
+ */
54
+ export function carrying(keys, routeKey, screen) {
55
+ const out = {};
56
+ for (const key of keys)
57
+ out[key] = key === routeKey ? screen : screenRef(key);
58
+ out[routeKey] = screen;
59
+ return out;
60
+ }
61
+ export function finishStep(id, target, value) {
62
+ return { id, type: 'FINISH', config: { target, with: value } };
63
+ }
64
+ /**
65
+ * What a case is for, from its condition alone: one screen, or every screen in
66
+ * a section. Anything else is a shape this view does not write and cannot read.
67
+ */
68
+ export function caseKind(entry, routeKey) {
69
+ const condition = asRec(entry['condition']);
70
+ const ref = screenRef(routeKey);
71
+ const named = (c) => c['variable'] === ref && c['operator'] === 'eq' && typeof c['value'] === 'string' ? c['value'] : undefined;
72
+ const one = named(condition);
73
+ if (one !== undefined)
74
+ return { kind: 'screen', screen: one };
75
+ const group = asArray(condition['any']).map(asRec).map(named);
76
+ if (group.length > 0 && group.every((v) => v !== undefined))
77
+ return { kind: 'section', subtree: group };
78
+ return null;
79
+ }
80
+ export const isCycle = (step) => step?.type === 'CYCLE';
81
+ export const isCall = (step) => step?.type === 'HTTP_REQUEST';
82
+ /** The SWITCH a cycle routes with: the one control step in its body. */
83
+ export function switchIn(cycle) {
84
+ return asSteps(cycle.config['body']).find((s) => s.type === 'SWITCH');
85
+ }
86
+ /** The cases of a switch, to read. */
87
+ export function casesOf(switchStep) {
88
+ return asArray(switchStep.config['cases']).map(asRec);
89
+ }
90
+ /**
91
+ * The switch's own list of cases, to change — `casesOf` hands back a copy, which
92
+ * is what you want for reading and useless for anything else.
93
+ */
94
+ export function caseListIn(switchStep) {
95
+ if (!Array.isArray(switchStep.config['cases']))
96
+ switchStep.config['cases'] = [];
97
+ return switchStep.config['cases'];
98
+ }
99
+ /**
100
+ * The guard after a call: when it did not come back, open a screen instead.
101
+ * An answer that never arrived has no `ok` at all, which is not `true` either.
102
+ */
103
+ export function guardTarget(step, callId, routeKey) {
104
+ if (step?.type !== 'CONDITIONAL')
105
+ return undefined;
106
+ const condition = asRec(asRec(step.config)['condition']);
107
+ const expected = `{{steps.${callId}.output.ok}}`;
108
+ if (condition['variable'] !== expected || condition['operator'] !== 'neq' || condition['value'] !== true)
109
+ return undefined;
110
+ const branch = asSteps(asRec(step.config)['if_true']);
111
+ if (branch.length !== 1 || branch[0].type !== 'GO_TO')
112
+ return undefined;
113
+ const screen = asRec(branch[0].config['with'])[routeKey];
114
+ return typeof screen === 'string' ? screen : undefined;
115
+ }
116
+ /**
117
+ * A case's steps, split where the frame it feeds begins: the calls it makes on
118
+ * the way in — each with the guard that says where a failed one goes — and then
119
+ * whatever they are for.
120
+ *
121
+ * For a section that is its CYCLE; for a screen, its question. Preparation
122
+ * always precedes the thing it prepares, which is what puts a call's answer in
123
+ * scope for it.
124
+ */
125
+ export function partsOf(entry, routeKey = DEFAULT_ROUTE_KEY) {
126
+ const steps = asSteps(entry['steps']);
127
+ const calls = [];
128
+ let at = 0;
129
+ while (isCall(steps[at])) {
130
+ const step = steps[at];
131
+ const onFail = guardTarget(steps[at + 1], step.id, routeKey);
132
+ calls.push({ step, onFail });
133
+ at += onFail === undefined ? 1 : 2;
134
+ }
135
+ return { calls, rest: steps.slice(at) };
136
+ }
137
+ /** The key a cycle starts each round with — how a switch knows what to open. */
138
+ export function routeKeyOf(cycle) {
139
+ const keys = Object.keys(asRec(cycle.config['input']));
140
+ return keys.find((k) => k === DEFAULT_ROUTE_KEY) ?? keys[0];
141
+ }
142
+ /**
143
+ * Every step of a kind in a run of steps, however deeply branched — through both
144
+ * halves of a conditional and through a switch's cases, which is where a
145
+ * follow-up question and everything it leads to lives.
146
+ */
147
+ export function stepsIn(steps, type, out = []) {
148
+ for (const step of asSteps(steps)) {
149
+ if (step.type === type)
150
+ out.push(step);
151
+ for (const value of Object.values(asRec(step.config))) {
152
+ if (!Array.isArray(value))
153
+ continue;
154
+ stepsIn(value, type, out);
155
+ // A switch's cases are not steps; what each opens is.
156
+ for (const entry of value)
157
+ stepsIn(asArray(asRec(entry)['steps']), type, out);
158
+ }
159
+ }
160
+ return out;
161
+ }
162
+ /**
163
+ * The gates that tell typing from tapping: a CONDITIONAL on what was typed into
164
+ * a question. A call's guard tests `output.ok` and so is never one of these.
165
+ */
166
+ export function typedGatesIn(steps) {
167
+ return stepsIn(steps, 'CONDITIONAL').filter((step) => {
168
+ const condition = asRec(step.config['condition']);
169
+ const first = asRec(asArray(condition['all'])[0]);
170
+ const variable = str(first['variable']) ?? str(condition['variable']) ?? '';
171
+ return variable.includes('.output.resumeData.data.');
172
+ });
173
+ }
174
+ /** One step by id, anywhere in a run of steps — a follow-up's question included. */
175
+ export function stepIn(steps, id) {
176
+ for (const step of asSteps(steps)) {
177
+ if (step.id === id)
178
+ return step;
179
+ for (const value of Object.values(asRec(step.config))) {
180
+ if (!Array.isArray(value))
181
+ continue;
182
+ const found = stepIn(value, id) ?? value.map((entry) => stepIn(asArray(asRec(entry)['steps']), id)).find(Boolean);
183
+ if (found)
184
+ return found;
185
+ }
186
+ }
187
+ return undefined;
188
+ }
189
+ /**
190
+ * Every jump in a run of steps.
191
+ *
192
+ * One destination is sometimes written as several jumps — the same answer
193
+ * carried on, tested by which frame it lands in — so both halves of the layer
194
+ * need to see them all.
195
+ */
196
+ export function jumpsIn(steps, out = []) {
197
+ return stepsIn(steps, 'GO_TO', out);
198
+ }
199
+ /** Where a jump sends someone, when it names a literal screen. */
200
+ export function jumpTarget(step, routeKey) {
201
+ if (step.type !== 'GO_TO')
202
+ return undefined;
203
+ const target = str(step.config['target']);
204
+ const screen = asRec(step.config['with'])[routeKey];
205
+ if (!target || typeof screen !== 'string')
206
+ return undefined;
207
+ return { target, screen };
208
+ }
209
+ //# sourceMappingURL=shape.js.map