@xyne/workflow-sdk 3.2.47 → 3.2.49

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 (178) hide show
  1. package/dist/agents/host-agent-step.d.ts +2 -2
  2. package/dist/client/index.d.ts +1 -1
  3. package/dist/client/index.d.ts.map +1 -1
  4. package/dist/client/types.d.ts +46 -34
  5. package/dist/client/types.d.ts.map +1 -1
  6. package/dist/client/workflow-client.d.ts +1 -1
  7. package/dist/client/workflow-client.d.ts.map +1 -1
  8. package/dist/client/workflow-client.js +34 -30
  9. package/dist/client/workflow-client.js.map +1 -1
  10. package/dist/connectors/builtin/steps/classifier-agent.step.d.ts +2 -2
  11. package/dist/connectors/builtin/steps/parallel.step.d.ts +4 -4
  12. package/dist/connectors/builtin/steps/wait.step.d.ts +28 -0
  13. package/dist/connectors/builtin/steps/wait.step.d.ts.map +1 -1
  14. package/dist/connectors/builtin/steps/wait.step.js +26 -1
  15. package/dist/connectors/builtin/steps/wait.step.js.map +1 -1
  16. package/dist/conversational-flow/calls.d.ts +20 -0
  17. package/dist/conversational-flow/calls.d.ts.map +1 -0
  18. package/dist/conversational-flow/calls.js +59 -0
  19. package/dist/conversational-flow/calls.js.map +1 -0
  20. package/dist/conversational-flow/conditions.d.ts +24 -0
  21. package/dist/conversational-flow/conditions.d.ts.map +1 -0
  22. package/dist/conversational-flow/conditions.js +69 -0
  23. package/dist/conversational-flow/conditions.js.map +1 -0
  24. package/dist/conversational-flow/create.d.ts +23 -18
  25. package/dist/conversational-flow/create.d.ts.map +1 -1
  26. package/dist/conversational-flow/create.js +73 -96
  27. package/dist/conversational-flow/create.js.map +1 -1
  28. package/dist/conversational-flow/edit/build.d.ts +112 -0
  29. package/dist/conversational-flow/edit/build.d.ts.map +1 -0
  30. package/dist/conversational-flow/edit/build.js +249 -0
  31. package/dist/conversational-flow/edit/build.js.map +1 -0
  32. package/dist/conversational-flow/edit/calls.d.ts +61 -0
  33. package/dist/conversational-flow/edit/calls.d.ts.map +1 -0
  34. package/dist/conversational-flow/edit/calls.js +296 -0
  35. package/dist/conversational-flow/edit/calls.js.map +1 -0
  36. package/dist/conversational-flow/edit/cases.d.ts +29 -0
  37. package/dist/conversational-flow/edit/cases.d.ts.map +1 -0
  38. package/dist/conversational-flow/edit/cases.js +83 -0
  39. package/dist/conversational-flow/edit/cases.js.map +1 -0
  40. package/dist/conversational-flow/edit/endings.d.ts +16 -0
  41. package/dist/conversational-flow/edit/endings.d.ts.map +1 -0
  42. package/dist/conversational-flow/edit/endings.js +53 -0
  43. package/dist/conversational-flow/edit/endings.js.map +1 -0
  44. package/dist/conversational-flow/edit/followups.d.ts +28 -0
  45. package/dist/conversational-flow/edit/followups.d.ts.map +1 -0
  46. package/dist/conversational-flow/edit/followups.js +103 -0
  47. package/dist/conversational-flow/edit/followups.js.map +1 -0
  48. package/dist/conversational-flow/edit/inputs.d.ts +24 -0
  49. package/dist/conversational-flow/edit/inputs.d.ts.map +1 -0
  50. package/dist/conversational-flow/edit/inputs.js +53 -0
  51. package/dist/conversational-flow/edit/inputs.js.map +1 -0
  52. package/dist/conversational-flow/edit/kinds.d.ts +29 -0
  53. package/dist/conversational-flow/edit/kinds.d.ts.map +1 -0
  54. package/dist/conversational-flow/edit/kinds.js +142 -0
  55. package/dist/conversational-flow/edit/kinds.js.map +1 -0
  56. package/dist/conversational-flow/edit/options.d.ts +68 -0
  57. package/dist/conversational-flow/edit/options.d.ts.map +1 -0
  58. package/dist/conversational-flow/edit/options.js +209 -0
  59. package/dist/conversational-flow/edit/options.js.map +1 -0
  60. package/dist/conversational-flow/edit/picklist.d.ts +28 -0
  61. package/dist/conversational-flow/edit/picklist.d.ts.map +1 -0
  62. package/dist/conversational-flow/edit/picklist.js +58 -0
  63. package/dist/conversational-flow/edit/picklist.js.map +1 -0
  64. package/dist/conversational-flow/edit/prompts.d.ts +24 -0
  65. package/dist/conversational-flow/edit/prompts.d.ts.map +1 -0
  66. package/dist/conversational-flow/edit/prompts.js +180 -0
  67. package/dist/conversational-flow/edit/prompts.js.map +1 -0
  68. package/dist/conversational-flow/edit/routers.d.ts +65 -0
  69. package/dist/conversational-flow/edit/routers.d.ts.map +1 -0
  70. package/dist/conversational-flow/edit/routers.js +208 -0
  71. package/dist/conversational-flow/edit/routers.js.map +1 -0
  72. package/dist/conversational-flow/edit/screens.d.ts +44 -0
  73. package/dist/conversational-flow/edit/screens.d.ts.map +1 -0
  74. package/dist/conversational-flow/edit/screens.js +137 -0
  75. package/dist/conversational-flow/edit/screens.js.map +1 -0
  76. package/dist/conversational-flow/edit/versions.d.ts +43 -0
  77. package/dist/conversational-flow/edit/versions.d.ts.map +1 -0
  78. package/dist/conversational-flow/edit/versions.js +189 -0
  79. package/dist/conversational-flow/edit/versions.js.map +1 -0
  80. package/dist/conversational-flow/index.d.ts +33 -16
  81. package/dist/conversational-flow/index.d.ts.map +1 -1
  82. package/dist/conversational-flow/index.js +42 -9
  83. package/dist/conversational-flow/index.js.map +1 -1
  84. package/dist/conversational-flow/json.d.ts +15 -0
  85. package/dist/conversational-flow/json.d.ts.map +1 -0
  86. package/dist/conversational-flow/json.js +14 -0
  87. package/dist/conversational-flow/json.js.map +1 -0
  88. package/dist/conversational-flow/jumps.d.ts +22 -0
  89. package/dist/conversational-flow/jumps.d.ts.map +1 -0
  90. package/dist/conversational-flow/jumps.js +160 -0
  91. package/dist/conversational-flow/jumps.js.map +1 -0
  92. package/dist/conversational-flow/locate.d.ts +30 -0
  93. package/dist/conversational-flow/locate.d.ts.map +1 -0
  94. package/dist/conversational-flow/locate.js +73 -0
  95. package/dist/conversational-flow/locate.js.map +1 -0
  96. package/dist/conversational-flow/paths.d.ts +31 -0
  97. package/dist/conversational-flow/paths.d.ts.map +1 -0
  98. package/dist/conversational-flow/paths.js +50 -0
  99. package/dist/conversational-flow/paths.js.map +1 -0
  100. package/dist/conversational-flow/prompt.d.ts +48 -0
  101. package/dist/conversational-flow/prompt.d.ts.map +1 -0
  102. package/dist/conversational-flow/prompt.js +161 -0
  103. package/dist/conversational-flow/prompt.js.map +1 -0
  104. package/dist/conversational-flow/read.d.ts +9 -17
  105. package/dist/conversational-flow/read.d.ts.map +1 -1
  106. package/dist/conversational-flow/read.js +217 -384
  107. package/dist/conversational-flow/read.js.map +1 -1
  108. package/dist/conversational-flow/rounds.d.ts +29 -0
  109. package/dist/conversational-flow/rounds.d.ts.map +1 -0
  110. package/dist/conversational-flow/rounds.js +41 -0
  111. package/dist/conversational-flow/rounds.js.map +1 -0
  112. package/dist/conversational-flow/routers.d.ts +24 -80
  113. package/dist/conversational-flow/routers.d.ts.map +1 -1
  114. package/dist/conversational-flow/routers.js +76 -262
  115. package/dist/conversational-flow/routers.js.map +1 -1
  116. package/dist/conversational-flow/server/conversation-service.d.ts +46 -18
  117. package/dist/conversational-flow/server/conversation-service.d.ts.map +1 -1
  118. package/dist/conversational-flow/server/conversation-service.js +56 -18
  119. package/dist/conversational-flow/server/conversation-service.js.map +1 -1
  120. package/dist/conversational-flow/server/routes.d.ts +15 -6
  121. package/dist/conversational-flow/server/routes.d.ts.map +1 -1
  122. package/dist/conversational-flow/server/routes.js +71 -74
  123. package/dist/conversational-flow/server/routes.js.map +1 -1
  124. package/dist/conversational-flow/settle.d.ts +21 -15
  125. package/dist/conversational-flow/settle.d.ts.map +1 -1
  126. package/dist/conversational-flow/settle.js +240 -38
  127. package/dist/conversational-flow/settle.js.map +1 -1
  128. package/dist/conversational-flow/shape.d.ts +114 -0
  129. package/dist/conversational-flow/shape.d.ts.map +1 -0
  130. package/dist/conversational-flow/shape.js +209 -0
  131. package/dist/conversational-flow/shape.js.map +1 -0
  132. package/dist/conversational-flow/testing.d.ts +54 -0
  133. package/dist/conversational-flow/testing.d.ts.map +1 -0
  134. package/dist/conversational-flow/testing.js +119 -0
  135. package/dist/conversational-flow/testing.js.map +1 -0
  136. package/dist/conversational-flow/tree.d.ts +28 -17
  137. package/dist/conversational-flow/tree.d.ts.map +1 -1
  138. package/dist/conversational-flow/tree.js +60 -47
  139. package/dist/conversational-flow/tree.js.map +1 -1
  140. package/dist/conversational-flow/turn.d.ts +30 -22
  141. package/dist/conversational-flow/turn.d.ts.map +1 -1
  142. package/dist/conversational-flow/turn.js +69 -48
  143. package/dist/conversational-flow/turn.js.map +1 -1
  144. package/dist/conversational-flow/types.d.ts +154 -118
  145. package/dist/conversational-flow/types.d.ts.map +1 -1
  146. package/dist/conversational-flow/types.js +27 -51
  147. package/dist/conversational-flow/types.js.map +1 -1
  148. package/dist/conversational-flow/typing.d.ts +31 -0
  149. package/dist/conversational-flow/typing.d.ts.map +1 -0
  150. package/dist/conversational-flow/typing.js +53 -0
  151. package/dist/conversational-flow/typing.js.map +1 -0
  152. package/dist/engine/variable-resolver.d.ts +16 -0
  153. package/dist/engine/variable-resolver.d.ts.map +1 -1
  154. package/dist/engine/variable-resolver.js +45 -1
  155. package/dist/engine/variable-resolver.js.map +1 -1
  156. package/dist/types/pause-state.d.ts +9 -0
  157. package/dist/types/pause-state.d.ts.map +1 -1
  158. package/package.json +1 -1
  159. package/dist/conversational-flow/edit.d.ts +0 -101
  160. package/dist/conversational-flow/edit.d.ts.map +0 -1
  161. package/dist/conversational-flow/edit.js +0 -785
  162. package/dist/conversational-flow/edit.js.map +0 -1
  163. package/dist/conversational-flow/internals.d.ts +0 -51
  164. package/dist/conversational-flow/internals.d.ts.map +0 -1
  165. package/dist/conversational-flow/internals.js +0 -163
  166. package/dist/conversational-flow/internals.js.map +0 -1
  167. package/dist/conversational-flow/lookup-edit.d.ts +0 -96
  168. package/dist/conversational-flow/lookup-edit.d.ts.map +0 -1
  169. package/dist/conversational-flow/lookup-edit.js +0 -266
  170. package/dist/conversational-flow/lookup-edit.js.map +0 -1
  171. package/dist/conversational-flow/lookups.d.ts +0 -46
  172. package/dist/conversational-flow/lookups.d.ts.map +0 -1
  173. package/dist/conversational-flow/lookups.js +0 -145
  174. package/dist/conversational-flow/lookups.js.map +0 -1
  175. package/dist/conversational-flow/router-edit.d.ts +0 -53
  176. package/dist/conversational-flow/router-edit.d.ts.map +0 -1
  177. package/dist/conversational-flow/router-edit.js +0 -191
  178. package/dist/conversational-flow/router-edit.js.map +0 -1
@@ -1,108 +1,140 @@
1
- import { AGENT_STEP_TYPE, CLASSIFIER_STEP_TYPE, END_MESSAGE_KEY, optionsOf } from './types.js';
2
- const LOOP_INPUT = /^\{\{\s*steps\.__loop\.output\.input\.([A-Za-z0-9_]+)\s*\}\}$/;
3
- const WAIT_FIELD = /^\{\{\s*steps\.([^.\s}]+)\.output\.resumeData\.data\.([A-Za-z0-9_]+)\s*\}\}$/;
4
- const STEP_OUTPUT = /^\{\{\s*steps\.([^.\s}]+)\.output\b/;
5
- // ─── Entry ───
1
+ import { asArray, asRec, asSteps, str } from './json.js';
2
+ import { declaredFields, readCall } from './calls.js';
3
+ import { readPrompt, splitDestination } from './prompt.js';
4
+ import { caseKind, casesOf, isCall, isCycle, jumpsIn, partsOf, routeKeyOf, switchIn } from './shape.js';
5
+ import { AGENT_STEP_TYPE, CLASSIFIER_STEP_TYPE, END_MESSAGE_KEY, HANDOFF_SECTION, optionsOf, QUESTION_SEPARATOR, } from './types.js';
6
+ /**
7
+ * What the conversation is started with: the manual trigger's own input schema,
8
+ * under `context` — which is where the Conversation API puts what the caller
9
+ * passed in. Undeclared, a flow simply promises nothing.
10
+ */
11
+ function conversationInputs(config) {
12
+ const schema = asRec(asRec(asRec(config.trigger)['config'])['inputSchema']);
13
+ const context = asRec(asRec(schema['properties'])['context']);
14
+ return declaredFields(Object.keys(context).length > 0 ? context : {});
15
+ }
16
+ /** The way out an Ask AI agent answers with: `{{steps.<agent>.output.choice}}`. */
17
+ const AGENT_CHOICE = /^\{\{\s*steps\.[^.\s}]+\.output\.choice\s*\}\}$/;
6
18
  export function readConversationalFlow(config) {
7
- const steps = asSteps(config.steps);
8
- const typeOf = indexStepTypes(steps);
9
- for (const cycle of steps) {
10
- if (cycle.type !== 'CYCLE')
11
- continue;
12
- const cycleConfig = asRec(cycle.config);
13
- const router = asSteps(cycleConfig['body']).find((s) => s.type === 'SWITCH');
14
- if (!router)
19
+ const top = asSteps(config.steps);
20
+ const root = top.find(isCycle);
21
+ if (!root)
22
+ return null;
23
+ const routeKey = routeKeyOf(root);
24
+ if (!routeKey)
25
+ return null;
26
+ const ctx = { routeKey, typeOf: indexTypes(top) };
27
+ const screens = [];
28
+ // Calls before the conversation's own cycle belong to its first screen, and
29
+ // so are in scope for every screen in the flow.
30
+ const opening = top.slice(0, top.indexOf(root)).filter(isCall);
31
+ readSection(root, opening.map((step) => readCall(step, 'conversation', undefined)), undefined, 0, [], ctx, screens);
32
+ if (!screens.some((s) => s.id === root.id))
33
+ return null;
34
+ markBackLinks(screens);
35
+ settleMenusAndMessages(screens);
36
+ return { rootScreen: root.id, routeKey, screens, inputs: conversationInputs(config) };
37
+ }
38
+ /**
39
+ * One frame: the cycle whose id is this section's screen. Its own screen is a
40
+ * case inside it, so what the section fetched — which sits outside the cycle —
41
+ * is in scope for that screen and everything beside it.
42
+ */
43
+ function readSection(cycle, calls, parent, depth, frames, ctx, out) {
44
+ const routing = switchIn(cycle);
45
+ if (!routing)
46
+ return;
47
+ const inside = [...frames, cycle.id];
48
+ const children = [];
49
+ for (const entry of casesOf(routing)) {
50
+ const kind = caseKind(entry, ctx.routeKey);
51
+ if (!kind)
15
52
  continue;
16
- const routerConfig = asRec(router.config);
17
- const cases = asArray(routerConfig['cases']).map(asRec);
18
- const routeKey = routeKeyOf(cases);
19
- if (!routeKey)
53
+ const { calls: own, rest } = partsOf(entry, ctx.routeKey);
54
+ if (kind.kind === 'section') {
55
+ const inner = rest.find(isCycle);
56
+ if (!inner)
57
+ continue;
58
+ readSection(inner, own.map((c) => readCall(c.step, 'section', c.onFail)), cycle.id, depth + 1, inside, ctx, out);
59
+ children.push(inner.id);
20
60
  continue;
21
- const ctx = { cycleId: cycle.id, routeKey, typeOf };
22
- const screens = cases.map((c, i) => readScreen(c, i, ctx));
23
- const input = asRec(cycleConfig['input']);
24
- const rootScreen = typeof input[routeKey] === 'string' ? input[routeKey] : (screens[0]?.id ?? '');
25
- placeInTree(screens, rootScreen);
26
- markBackLinks(screens);
27
- settleMenusAndMessages(screens);
28
- return {
29
- cycleId: cycle.id,
30
- routeKey,
31
- routerId: router.id,
32
- rootScreen,
33
- screens,
34
- fallback: finalise(branchDestination(asSteps(routerConfig['default']), ctx, [])),
35
- };
36
- }
37
- return null;
38
- }
39
- /** The loop-input key every case switches on, or null if the cases disagree or aren't `eq`. */
40
- function routeKeyOf(cases) {
41
- if (cases.length === 0)
42
- return null;
43
- let key = null;
44
- for (const c of cases) {
45
- const cond = asRec(c['condition']);
46
- const match = typeof cond['variable'] === 'string' ? LOOP_INPUT.exec(cond['variable']) : null;
47
- if (!match || cond['operator'] !== 'eq' || cond['value'] === undefined)
48
- return null;
49
- if (key !== null && key !== match[1])
50
- return null;
51
- key = match[1];
61
+ }
62
+ // The section's own screen keeps the section's place; everything else in
63
+ // this switch sits one level in.
64
+ const isOwn = kind.screen === cycle.id;
65
+ out.push(readScreen(kind.screen, entry, rest, {
66
+ calls: [...(isOwn ? calls : []), ...own.map((c) => readCall(c.step, 'screen', c.onFail))],
67
+ parent: isOwn ? parent : cycle.id,
68
+ depth: isOwn ? depth : depth + 1,
69
+ frames: inside,
70
+ ctx,
71
+ }));
72
+ if (!isOwn)
73
+ children.push(kind.screen);
52
74
  }
53
- return key;
75
+ const own = out.find((s) => s.id === cycle.id);
76
+ if (own)
77
+ own.children = children;
54
78
  }
55
- // ─── Screens ───
56
- function readScreen(entry, caseIndex, ctx) {
57
- const id = String(asRec(entry['condition'])['value']);
58
- const label = typeof entry['label'] === 'string' ? entry['label'].trim() : '';
59
- const steps = asSteps(entry['steps']);
79
+ function readScreen(id, entry, rest, at) {
80
+ const label = str(entry['label']);
60
81
  const screen = {
61
82
  id,
62
- title: label && label !== id ? label : humanise(id),
83
+ title: label ?? humanise(id),
63
84
  kind: 'custom',
64
- caseIndex,
85
+ parent: at.parent,
86
+ depth: at.depth,
87
+ children: [],
88
+ frames: at.frames,
89
+ calls: at.calls,
65
90
  about: undefined,
66
91
  prompts: [],
92
+ versions: [],
67
93
  router: undefined,
68
94
  askAi: undefined,
69
95
  endValue: undefined,
70
96
  endMessage: undefined,
71
- parent: undefined,
72
- depth: -1,
73
97
  };
74
- const [first] = steps;
98
+ const [first] = rest;
75
99
  if (!first)
76
100
  return screen;
101
+ // A SWITCH before any question is the screen asking more than one way: one
102
+ // branch per condition, and `default` for when none of them hold. Follow-ups
103
+ // are a SWITCH too, but always *after* a WAIT, so position tells them apart.
104
+ if (first.type === 'SWITCH') {
105
+ const versions = versionsOf(first, at.ctx, screen);
106
+ if (versions) {
107
+ screen.kind = versions;
108
+ return screen;
109
+ }
110
+ }
77
111
  if (first.type === 'WAIT') {
78
- readPrompt(steps, ctx, screen.prompts);
79
- screen.about = typeof first.description === 'string' && first.description.trim() ? first.description : undefined;
112
+ readPrompt(rest, at.ctx, screen.prompts);
113
+ screen.about = str(first.description);
80
114
  if (isHandoff(first)) {
81
115
  screen.kind = 'handoff';
82
116
  return screen;
83
117
  }
84
- const askAi = readAskAi(steps, ctx, screen.prompts[0]);
118
+ const askAi = readAskAi(rest, screen.prompts[0], at.ctx);
85
119
  if (askAi) {
86
120
  screen.kind = 'ask-ai';
87
121
  screen.askAi = askAi;
88
122
  return screen;
89
123
  }
90
- // Menu vs message needs the tree; settled once every screen is placed.
91
124
  screen.kind = screen.prompts[0]?.pickSwitchId ? 'questions' : 'menu';
92
125
  return screen;
93
126
  }
94
- if (steps.length === 1 && first.type === 'FINISH' && isAimedAtCycle(first, ctx)) {
127
+ if (rest.length === 1 && first.type === 'FINISH') {
95
128
  screen.kind = 'end';
96
- // A Finish has no question to carry its "about", so its own step does — and
97
- // writing one is what lets a router end the conversation here.
98
- screen.about = typeof first.description === 'string' && first.description.trim() ? first.description : undefined;
129
+ // A Finish has no question to carry its "about", so its own step does —
130
+ // and writing one is what lets the router end a conversation here.
131
+ screen.about = str(first.description);
99
132
  const { [END_MESSAGE_KEY]: message, ...result } = asRec(asRec(first.config)['with']);
100
133
  screen.endValue = result;
101
- if (typeof message === 'string' && message)
102
- screen.endMessage = message;
134
+ screen.endMessage = str(message);
103
135
  return screen;
104
136
  }
105
- const router = readRouter(steps, ctx);
137
+ const router = readRouter(rest, at.ctx);
106
138
  if (router) {
107
139
  screen.kind = 'router';
108
140
  screen.router = router;
@@ -110,353 +142,138 @@ function readScreen(entry, caseIndex, ctx) {
110
142
  return screen;
111
143
  }
112
144
  /**
113
- * Read a WAIT-led run of steps into `out`, following nested questions. Returns
114
- * the prompt's id so a parent option can point at it.
145
+ * Every way a screen asks, read in the order the switch tries them: each case's
146
+ * question carries the condition it is asked under, and `default`'s carries
147
+ * none. What the screen *is* comes from the version asked when nothing else
148
+ * holds, since that is the one it always has.
115
149
  */
116
- function readPrompt(steps, ctx, out) {
117
- const wait = steps[0];
118
- const config = asRec(wait.config);
119
- const fields = asArray(asRec(config['form'])['fields']).map(asRec);
120
- const select = fields.find((f) => f['type'] === 'select');
121
- const text = fields.find((f) => f['type'] === 'text' || f['type'] === 'textarea');
122
- const choiceField = typeof select?.['name'] === 'string' ? select['name'] : undefined;
123
- const textField = typeof text?.['name'] === 'string' ? text['name'] : undefined;
124
- const prompt = {
125
- id: wait.id,
126
- text: typeof config['prompt'] === 'string' ? config['prompt'] : '',
127
- header: typeof config['reason'] === 'string' ? config['reason'] : undefined,
128
- choiceField,
129
- textField,
130
- options: [],
131
- freeText: undefined,
132
- pickSwitchId: undefined,
133
- };
134
- out.push(prompt);
135
- // Typed text is split off by a CONDITIONAL that tests this WAIT's text field;
136
- // its other branch is what happens when an option was picked.
137
- let picked = steps.slice(1);
138
- const [gate] = picked;
139
- if (gate?.type === 'CONDITIONAL' && textField && testsField(asRec(gate.config)['condition'], wait.id, textField)) {
140
- const gateConfig = asRec(gate.config);
141
- prompt.freeText = finalise(branchDestination(asSteps(gateConfig['if_true']), ctx, out));
142
- picked = asSteps(gateConfig['if_false']);
150
+ function versionsOf(switchStep, ctx, screen) {
151
+ const config = asRec(switchStep.config);
152
+ const branches = [
153
+ ...asArray(config['cases']).map(asRec).map((entry) => ({
154
+ when: asCondition(entry['condition']),
155
+ steps: asSteps(entry['steps']),
156
+ })),
157
+ { when: undefined, steps: asSteps(config['default']) },
158
+ ];
159
+ if (!branches.every((b) => b.steps[0]?.type === 'WAIT'))
160
+ return null;
161
+ let kind = 'custom';
162
+ for (const branch of branches) {
163
+ const from = screen.prompts.length;
164
+ readPrompt(branch.steps, ctx, screen.prompts);
165
+ const asked = screen.prompts[from];
166
+ if (!asked)
167
+ continue;
168
+ if (branch.when)
169
+ asked.when = branch.when;
170
+ screen.versions.push({ ...(branch.when ? { when: branch.when } : {}), promptId: asked.id });
171
+ // The version asked when nothing else holds is what the screen is.
172
+ if (!branch.when) {
173
+ screen.about = str(branch.steps[0]?.description) ?? screen.about;
174
+ kind = asked?.pickSwitchId ? 'questions' : 'menu';
175
+ }
143
176
  }
144
- const options = asArray(select?.['options']).map(asRec);
145
- const { byValue, switchId } = pickedDestinations(picked, wait.id, choiceField, ctx, out);
146
- prompt.pickSwitchId = switchId;
147
- prompt.options = options.map((o) => {
148
- const value = String(o['value'] ?? '');
149
- const resolved = byValue(value);
150
- return {
151
- label: String(o['label'] ?? value),
152
- value,
153
- to: resolved.kind === 'choice' ? { kind: 'screen', screen: value } : resolved,
154
- back: false,
155
- };
156
- });
157
- return prompt.id;
177
+ return kind;
158
178
  }
159
179
  /**
160
- * What picking each option does. Either one branch forwards the pick as the
161
- * next screen, or a SWITCH on the pick gives some options their own branch
162
- * (a follow-up question, a fixed jump) and sends the rest to its default.
180
+ * A router: a step that names the next screen, and a jump that carries its
181
+ * answer on. Its candidates are settled from the screens its switch can open.
163
182
  */
164
- function pickedDestinations(picked, waitId, choiceField, ctx, out) {
165
- const [only] = picked;
166
- if (only?.type === 'SWITCH' && picked.length === 1) {
167
- const config = asRec(only.config);
168
- const cases = asArray(config['cases']).map(asRec);
169
- const onPick = cases.every((c) => {
170
- const cond = asRec(c['condition']);
171
- return cond['operator'] === 'eq' && refersTo(cond['variable'], waitId, choiceField);
172
- });
173
- if (onPick) {
174
- const branches = new Map();
175
- for (const c of cases) {
176
- branches.set(String(asRec(c['condition'])['value']), branchDestination(asSteps(c['steps']), ctx, out));
177
- }
178
- const fallback = branchDestination(asSteps(config['default']), ctx, out);
179
- return { byValue: (value) => branches.get(value) ?? fallback, switchId: only.id };
180
- }
181
- }
182
- const whole = branchDestination(picked, ctx, out);
183
- return { byValue: () => whole, switchId: undefined };
184
- }
185
- /** Where a branch sends the user, reading one nested prompt if that's what it holds. */
186
- function branchDestination(steps, ctx, out) {
187
- const [first] = steps;
188
- if (!first)
189
- return { kind: 'none' };
190
- if (first.type === 'WAIT')
191
- return { kind: 'prompt', prompt: readPrompt(steps, ctx, out) };
192
- if (steps.length === 1)
193
- return jumpDestination(first, ctx);
194
- return { kind: 'none' };
195
- }
196
- function jumpDestination(step, ctx) {
197
- if (!isAimedAtCycle(step, ctx))
198
- return { kind: 'none' };
199
- const carried = asRec(asRec(step.config)['with']);
200
- if (step.type === 'FINISH')
201
- return { kind: 'end', value: carried };
202
- if (step.type !== 'GO_TO')
203
- return { kind: 'none' };
204
- const next = carried[ctx.routeKey];
205
- if (typeof next !== 'string' || next === '')
206
- return { kind: 'none' };
207
- if (!next.includes('{{'))
208
- return { kind: 'screen', screen: next };
209
- const pick = WAIT_FIELD.exec(next);
210
- if (pick && ctx.typeOf.get(pick[1]) === 'WAIT')
211
- return { kind: 'choice', wait: pick[1], field: pick[2] };
212
- const source = STEP_OUTPUT.exec(next);
213
- if (source)
214
- return { kind: 'dynamic', from: source[1], fromType: ctx.typeOf.get(source[1]) };
215
- return { kind: 'none' };
216
- }
217
- /** A pick forwarded outside an option list has no single screen to name. */
218
- function finalise(resolved) {
219
- return resolved.kind === 'choice' ? { kind: 'none' } : resolved;
220
- }
221
- // ─── Routers ───
222
- function readRouter(steps, ctx) {
223
- /** The step's answer is carried on by a GO_TO in the same case. */
224
- const forwards = (id) => steps.some((s) => {
225
- const d = jumpDestination(s, ctx);
226
- return d.kind === 'dynamic' && d.from === id;
227
- });
228
- // A classifier picks the screen: CLASSIFIER_AGENT, then a GO_TO carrying
229
- // `{{steps.<it>.output.class}}`. Its classes are the screens it may choose.
230
- const classifier = steps.find((s) => s.type === CLASSIFIER_STEP_TYPE);
231
- if (classifier && forwards(classifier.id)) {
232
- const config = asRec(classifier.config);
233
- return {
234
- mode: 'classifier',
235
- stepId: classifier.id,
236
- scope: typeof config['scope'] === 'string' ? config['scope'] : undefined,
237
- agent: undefined,
238
- model: typeof config['model'] === 'string' ? config['model'] : undefined,
239
- credentialName: typeof config['credentialName'] === 'string' ? config['credentialName'] : undefined,
240
- minConfidence: typeof config['minConfidence'] === 'number' ? config['minConfidence'] : undefined,
241
- task: typeof config['instructions'] === 'string' ? config['instructions'] : undefined,
242
- guidance: typeof classifier.description === 'string' && classifier.description.trim() ? classifier.description : undefined,
243
- candidates: Object.keys(asRec(config['classes'])),
244
- rules: [],
245
- };
246
- }
247
- // An agent picks the screen: RUN_AGENT, then a GO_TO carrying its answer.
248
- const agent = steps.find((s) => s.type === AGENT_STEP_TYPE);
249
- if (agent) {
250
- if (forwards(agent.id)) {
251
- const config = asRec(agent.config);
252
- return {
253
- mode: 'agent',
254
- stepId: agent.id,
255
- scope: typeof config['scope'] === 'string' ? config['scope'] : undefined,
256
- agent: typeof config['agentSlug'] === 'string' ? config['agentSlug'] : undefined,
257
- model: undefined,
258
- credentialName: undefined,
259
- minConfidence: undefined,
260
- task: typeof config['task'] === 'string' ? config['task'] : undefined,
261
- guidance: typeof agent.description === 'string' && agent.description.trim() ? agent.description : undefined,
262
- candidates: enumOf(asRec(config['outputSchema']), ctx.routeKey),
263
- rules: [],
264
- };
265
- }
266
- }
267
- // Keywords pick the screen: a SWITCH whose every case ends in a fixed jump.
268
- const [only] = steps;
269
- if (only?.type === 'SWITCH' && steps.length === 1) {
270
- const cases = asArray(asRec(only.config)['cases']).map(asRec);
271
- const rules = [];
272
- for (const c of cases) {
273
- const branch = asSteps(c['steps']);
274
- const dest = branch.length === 1 ? jumpDestination(branch[0], ctx) : { kind: 'none' };
275
- if (dest.kind !== 'screen')
276
- return undefined;
277
- rules.push({ words: literalValues(c['condition']), screen: dest.screen });
278
- }
279
- if (rules.length > 0) {
280
- return {
281
- mode: 'keywords',
282
- stepId: only.id,
283
- scope: undefined,
284
- agent: undefined,
285
- model: undefined,
286
- credentialName: undefined,
287
- minConfidence: undefined,
288
- task: undefined,
289
- guidance: undefined,
290
- candidates: [...new Set(rules.map((r) => r.screen))],
291
- rules,
292
- };
293
- }
294
- }
295
- return undefined;
183
+ function readRouter(rest, ctx) {
184
+ const deciding = rest.find((s) => s.type === CLASSIFIER_STEP_TYPE || s.type === AGENT_STEP_TYPE);
185
+ if (!deciding)
186
+ return undefined;
187
+ const carried = splitDestination(rest, ctx);
188
+ if (carried?.kind !== 'dynamic' || carried.from !== deciding.id)
189
+ return undefined;
190
+ const config = asRec(deciding.config);
191
+ // A classifier's choices are its classes; an agent's are the ids its answer
192
+ // is allowed to name.
193
+ const classes = deciding.type === CLASSIFIER_STEP_TYPE
194
+ ? Object.keys(asRec(config['classes']))
195
+ : enumOf(asRec(config['outputSchema']), ctx.routeKey);
196
+ return {
197
+ mode: deciding.type === CLASSIFIER_STEP_TYPE ? 'classifier' : 'agent',
198
+ stepId: deciding.id,
199
+ agent: str(config['agentSlug']),
200
+ model: str(config['model']),
201
+ credentialName: str(config['credentialName']),
202
+ minConfidence: typeof config['minConfidence'] === 'number' ? config['minConfidence'] : undefined,
203
+ task: str(config['instructions']) ?? str(config['task']),
204
+ guidance: str(deciding.description),
205
+ candidates: classes,
206
+ };
296
207
  }
297
208
  /**
298
- * An agent that answers until they leave. The screen asks first (its WAIT:
299
- * the intro, the ways out, a text box); a typed question goes to an agent
300
- * step that waits for a reply after each answer (`waitForReply`) — a reply
301
- * continues the conversation, a way out ends it — and a GO_TO then carries the
302
- * chosen way out (`{{steps.<agent>.output.choice}}`). The ways out are the
303
- * WAIT's options — offered again after every answer.
209
+ * An agent that answers until they leave: the screen asks first, a typed
210
+ * question goes to an agent that waits for a reply after each answer, and a
211
+ * jump then carries the way out the agent was told to name.
304
212
  */
305
- function readAskAi(steps, ctx, prompt) {
306
- const gate = steps[1];
213
+ function readAskAi(rest, prompt, ctx) {
214
+ const gate = rest[1];
307
215
  if (!prompt || gate?.type !== 'CONDITIONAL')
308
216
  return undefined;
309
217
  const typed = asSteps(asRec(gate.config)['if_true']);
310
218
  const agent = typed[0];
311
219
  if (agent?.type !== AGENT_STEP_TYPE || asRec(agent.config)['waitForReply'] !== true)
312
220
  return undefined;
313
- const leave = typed[1];
314
- const carried = leave && isAimedAtCycle(leave, ctx) ? asRec(asRec(leave.config)['with'])[ctx.routeKey] : undefined;
315
- if (typeof carried !== 'string' || !REVIEW_CHOICE.test(carried) || !carried.includes(`steps.${agent.id}.`))
221
+ // What it answers with leaves the screen — as one jump, or as the chain of
222
+ // them a deep screen needs; either way they all carry the same answer.
223
+ const leaving = splitDestination(typed.slice(1), ctx);
224
+ if (leaving?.kind !== 'dynamic' || leaving.from !== agent.id)
225
+ return undefined;
226
+ if (!AGENT_CHOICE.test(agentChoice(typed.slice(1), ctx.routeKey)))
316
227
  return undefined;
317
228
  const config = asRec(agent.config);
318
- const task = typeof config['task'] === 'string' ? config['task'] : '';
229
+ const task = str(config['task']) ?? '';
230
+ const at = task.lastIndexOf(QUESTION_SEPARATOR);
319
231
  return {
320
232
  stepId: agent.id,
321
- agent: typeof config['agentSlug'] === 'string' ? config['agentSlug'] : undefined,
322
- task: instructionsOf(task),
233
+ agent: str(config['agentSlug']),
234
+ task: at === -1 ? task : task.slice(0, at),
323
235
  intro: prompt.text,
324
236
  exits: prompt.options,
325
237
  };
326
238
  }
327
- /** The display section that marks a WAIT as a hand off. */
328
- export const HANDOFF_SECTION = 'handoff';
329
- /**
330
- * A hand off: a WAIT that shows the `handoff` section. The person who takes the
331
- * conversation answers it — with a Finish to close it, or a screen to send it
332
- * back to — and a GO_TO carries that on.
333
- *
334
- * The section doubles as the marker. `display` is for showing data, but it is
335
- * the one thing that tells a hand off from a plain screen: everything else is
336
- * the same WAIT, options and GO_TO. A WAIT field of its own for this, or the
337
- * `webhook` mode, would make it structural instead.
338
- */
239
+ /** A hand off is a WAIT that shows the `handoff` section — its one marker. */
339
240
  function isHandoff(wait) {
340
- return asArray(asRec(wait.config)['display']).some((d) => asRec(d)['id'] === HANDOFF_SECTION);
341
- }
342
- const REVIEW_CHOICE = /^\{\{\s*steps\.[^.\s}]+\.output\.choice\s*\}\}$/;
343
- /** An Ask AI task is the author's instructions, then the question the person typed. */
344
- export const QUESTION_SEPARATOR = '\n\nTheir question: ';
345
- function instructionsOf(task) {
346
- const at = task.lastIndexOf(QUESTION_SEPARATOR);
347
- return at === -1 ? task : task.slice(0, at);
348
- }
349
- /** The allowed values the agent may answer with for `key`, from its output schema. */
350
- function enumOf(schema, key) {
351
- const property = asRec(asRec(schema['properties'])[key]);
352
- return asArray(property['enum']).filter((v) => typeof v === 'string');
353
- }
354
- // ─── Tree ───
355
- /**
356
- * Parent = the screen that first links to this one, walking options outward
357
- * from the root. Routers, ends and hand offs sit outside the menu tree: every
358
- * screen can reach them, so "first to link" would be meaningless.
359
- *
360
- * That is a statement about the menu, not about the picture: the map draws an
361
- * ending that exactly one screen leads to beside that screen (see the layout's
362
- * `leadsTo`), because that is where the author put it. Keeping it out of the
363
- * tree keeps the meaning stable — an ending does not change what it is when a
364
- * second screen starts offering it.
365
- */
366
- function placeInTree(screens, rootScreen) {
367
- const byId = new Map(screens.map((s) => [s.id, s]));
368
- const inTree = (s) => !!s && s.kind !== 'router' && s.kind !== 'end' && s.kind !== 'handoff';
369
- const root = byId.get(rootScreen);
370
- if (!inTree(root))
371
- return;
372
- root.depth = 0;
373
- const queue = [root];
374
- while (queue.length > 0) {
375
- const current = queue.shift();
376
- for (const option of optionsOf(current)) {
377
- if (option.to.kind !== 'screen')
378
- continue;
379
- const next = byId.get(option.to.screen);
380
- if (!inTree(next) || next.depth !== -1)
381
- continue;
382
- next.parent = current.id;
383
- next.depth = current.depth + 1;
384
- queue.push(next);
385
- }
386
- }
241
+ return asArray(asRec(wait.config)['display']).map(asRec).some((section) => section['id'] === HANDOFF_SECTION);
387
242
  }
243
+ // ─── Settling what the walk cannot know alone ───
244
+ /** An option leads "back" when it opens one of the screen's own ancestors. */
388
245
  function markBackLinks(screens) {
389
- const parentOf = new Map(screens.map((s) => [s.id, s.parent]));
390
- const isAncestor = (candidate, of) => {
391
- for (let at = parentOf.get(of); at !== undefined; at = parentOf.get(at)) {
392
- if (at === candidate)
393
- return true;
394
- }
395
- return false;
396
- };
246
+ const parent = new Map(screens.map((s) => [s.id, s.parent]));
397
247
  for (const screen of screens) {
248
+ const ancestors = new Set();
249
+ for (let at = parent.get(screen.id); at !== undefined; at = parent.get(at))
250
+ ancestors.add(at);
398
251
  for (const option of optionsOf(screen)) {
399
- option.back = option.to.kind === 'screen' && isAncestor(option.to.screen, screen.id);
252
+ if (option.to.kind === 'screen' && ancestors.has(option.to.screen))
253
+ option.back = true;
400
254
  }
401
255
  }
402
256
  }
403
- /**
404
- * Menu and message are the same nodes; the difference is what the author is
405
- * doing with them. A screen with screens beneath it is a menu. Without any, a
406
- * screen that offers to finish ("That's all" → an end) is a message — and one
407
- * that doesn't is a menu still waiting for its first screen.
408
- */
257
+ /** A screen with screens inside it is a menu; one without says something. */
409
258
  function settleMenusAndMessages(screens) {
410
- const ends = new Set(screens.filter((s) => s.kind === 'end').map((s) => s.id));
411
259
  for (const screen of screens) {
412
- if (screen.kind !== 'menu')
413
- continue;
414
- const hasChildren = screens.some((s) => s.parent === screen.id);
415
- const offersToFinish = optionsOf(screen).some((o) => o.to.kind === 'screen' && ends.has(o.to.screen));
416
- screen.kind = !hasChildren && offersToFinish ? 'message' : 'menu';
260
+ if (screen.kind === 'menu' && screen.children.length === 0)
261
+ screen.kind = 'message';
417
262
  }
418
263
  }
419
- // ─── Helpers ───
420
- function isAimedAtCycle(step, ctx) {
421
- return (step.type === 'GO_TO' || step.type === 'FINISH') && asRec(step.config)['target'] === ctx.cycleId;
422
- }
423
- function refersTo(variable, waitId, field) {
424
- if (typeof variable !== 'string' || !field)
425
- return false;
426
- const match = WAIT_FIELD.exec(variable);
427
- return !!match && match[1] === waitId && match[2] === field;
428
- }
429
- /** Whether any leaf of a condition tree reads this WAIT's field. */
430
- function testsField(condition, waitId, field) {
431
- const c = asRec(condition);
432
- if (refersTo(c['variable'], waitId, field))
433
- return true;
434
- for (const key of ['all', 'any']) {
435
- if (asArray(c[key]).some((child) => testsField(child, waitId, field)))
436
- return true;
437
- }
438
- return c['not'] !== undefined && testsField(c['not'], waitId, field);
439
- }
440
- /** The literal values a condition tree compares against — a keyword rule's words. */
441
- function literalValues(condition) {
442
- const c = asRec(condition);
443
- const own = typeof c['value'] === 'string' ? [c['value']] : [];
444
- return [...own, ...asArray(c['all']).flatMap(literalValues), ...asArray(c['any']).flatMap(literalValues)];
445
- }
446
- function indexStepTypes(steps, into = new Map()) {
264
+ // ─── Plumbing ───
265
+ function indexTypes(steps, into = new Map()) {
447
266
  for (const step of steps) {
448
267
  into.set(step.id, step.type);
449
- const config = asRec(step.config);
450
- for (const value of Object.values(config)) {
268
+ for (const value of Object.values(asRec(step.config))) {
451
269
  if (Array.isArray(value)) {
452
- indexStepTypes(asSteps(value), into);
270
+ indexTypes(asSteps(value), into);
453
271
  for (const entry of value)
454
- indexStepTypes(asSteps(asRec(entry)['steps']), into);
272
+ indexTypes(asSteps(asRec(entry)['steps']), into);
455
273
  }
456
274
  else if (value && typeof value === 'object') {
457
- // PARALLEL keeps its branches in a record of step lists.
458
- for (const branch of Object.values(value))
459
- indexStepTypes(asSteps(branch), into);
275
+ for (const branch of Object.values(asRec(value)))
276
+ indexTypes(asSteps(branch), into);
460
277
  }
461
278
  }
462
279
  }
@@ -464,15 +281,31 @@ function indexStepTypes(steps, into = new Map()) {
464
281
  }
465
282
  function humanise(id) {
466
283
  const words = id.replace(/[_-]+/g, ' ').trim();
467
- return words ? words[0].toUpperCase() + words.slice(1) : id;
284
+ return words ? words.charAt(0).toUpperCase() + words.slice(1) : id;
468
285
  }
469
- function asRec(value) {
470
- return value && typeof value === 'object' && !Array.isArray(value) ? value : {};
286
+ /** The ids an answer may name, from the shape the agent was told to reply in. */
287
+ function enumOf(schema, key) {
288
+ return asArray(asRec(asRec(schema['properties'])[key])['enum']).filter((v) => typeof v === 'string');
471
289
  }
472
- function asArray(value) {
473
- return Array.isArray(value) ? value : [];
290
+ /** What a branch's jumps carry as the next screen — they all carry the same. */
291
+ function agentChoice(steps, routeKey) {
292
+ const jump = jumpsIn(steps)[0];
293
+ const carried = jump ? asRec(asRec(jump.config)['with'])[routeKey] : undefined;
294
+ return typeof carried === 'string' ? carried : '';
474
295
  }
475
- function asSteps(value) {
476
- return asArray(value).filter((s) => !!s && typeof s === 'object' && typeof s['id'] === 'string' && typeof s['type'] === 'string');
296
+ /** A case's test, when it is one the layer can show: a leaf, or a group of them. */
297
+ function asCondition(value) {
298
+ const rec = asRec(value);
299
+ for (const join of ['all', 'any']) {
300
+ if (!Array.isArray(rec[join]))
301
+ continue;
302
+ const parts = asArray(rec[join]).map(asCondition).filter((c) => c !== undefined);
303
+ return parts.length > 0 ? { [join]: parts } : undefined;
304
+ }
305
+ const variable = str(rec['variable']);
306
+ const operator = str(rec['operator']);
307
+ return variable && operator
308
+ ? { variable, operator, ...('value' in rec ? { value: rec['value'] } : {}) }
309
+ : undefined;
477
310
  }
478
311
  //# sourceMappingURL=read.js.map