@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,286 +1,100 @@
1
- import { breadcrumb, treeOrder } from './tree.js';
2
- import { AGENT_STEP_TYPE, CLASSIFIER_STEP_TYPE, optionsOf } from './types.js';
3
- import { asRec, asSteps, containerOf, findStep, walk } from './internals.js';
4
- /** Screens a router may send people to: everything they can be shown. */
5
- export function routableScreens(flow) {
6
- return flow.screens.filter((s) => s.kind !== 'router' && s.kind !== 'end');
7
- }
8
1
  /**
9
- * Finish screens a router may choose — the ones whose author said when to send
10
- * people there.
2
+ * Asking the flow about its routers.
11
3
  *
12
- * Every other screen can be described to the model by where it sits in the
13
- * menu; a Finish sits nowhere, so without that line there is nothing to
14
- * describe it by, and nothing to stop the model ending a conversation it merely
15
- * found hard to place. Writing the line is the author saying "typing may end it
16
- * here".
4
+ * A router is a screen like any other, so which screens it serves is not stored
5
+ * anywhere: it is the frame the router sits in. A router in the conversation's
6
+ * own frame reaches everything; one inside a section reaches that section and
7
+ * what is below it. Scoping is therefore a question about position, which is
8
+ * what every query here answers.
17
9
  */
18
- export function endsByTyping(flow) {
19
- return flow.screens.filter((s) => s.kind === 'end' && s.about !== undefined);
20
- }
21
- /** Every router in the flow, the one serving everything first. */
10
+ import { ancestorsOf, breadcrumb, subtreeOf } from './tree.js';
11
+ /** Every router in the flow, as the tree reads them. */
22
12
  export function routersOf(flow) {
23
- return flow.screens.filter((s) => s.router !== undefined).sort((a, b) => Number(a.router.scope !== undefined) - Number(b.router.scope !== undefined));
24
- }
25
- /**
26
- * Where a screen's typed text should go: the nearest scoped router at or above
27
- * it, else the one serving the flow.
28
- *
29
- * "At or above" is why a section's own screen uses its own router, and why a
30
- * screen beneath two scoped sections uses the nearer one.
31
- */
32
- export function routerFor(flow, screenId) {
33
- const byId = new Map(flow.screens.map((s) => [s.id, s]));
34
- const scoped = new Map(routersOf(flow).filter((r) => r.router.scope).map((r) => [r.router.scope, r]));
35
- for (let at = byId.get(screenId); at; at = at.parent ? byId.get(at.parent) : undefined) {
36
- const router = scoped.get(at.id);
37
- if (router)
38
- return router;
39
- }
40
- return flowRouter(flow);
13
+ return flow.screens.filter((s) => s.kind === 'router');
41
14
  }
42
- /**
43
- * What a router may choose. Scoped: its section and everything beneath it, plus
44
- * the first screen — "when the message isn't about this section" — every hand
45
- * off, so asking for a person works wherever they are, and the endings its own
46
- * screens lead to. Unscoped: every screen they can be shown, and every ending.
47
- *
48
- * An ending is not automatically global: a Finish that says something ("Your
49
- * driver is 3 minutes away") belongs to the part of the flow that links to it,
50
- * and a router serving Payments has no business closing the conversation on a
51
- * Rides screen. A shared "End of chat", linked from inside the section, stays
52
- * available because the link is what puts it in reach.
53
- *
54
- * Not the same set as the screens that route *to* it: the flow's router must
55
- * still reach a leaf inside a scoped section, or typing at the main menu could
56
- * only ever reach the section itself.
57
- */
58
- export function candidatesOf(flow, router) {
59
- const scope = router.router?.scope;
60
- // Ending is not part of a section: a conversation ends wherever it ends.
61
- const ends = endsByTyping(flow);
62
- if (!scope)
63
- return [...routableScreens(flow), ...ends];
64
- const section = flow.screens.find((s) => s.id === scope);
65
- if (!section)
66
- return [...routableScreens(flow), ...ends];
67
- const inside = new Set([section.id]);
68
- for (const s of treeOrder(flow))
69
- if (s.parent !== undefined && inside.has(s.parent))
70
- inside.add(s.id);
71
- const linkedFromInside = (end) => flow.screens.some((s) => inside.has(s.id) && optionsOf(s).some((o) => o.to.kind === 'screen' && o.to.screen === end.id));
72
- return [
73
- ...routableScreens(flow).filter((s) => inside.has(s.id) || s.id === flow.rootScreen || s.kind === 'handoff'),
74
- ...ends.filter(linkedFromInside),
75
- ];
15
+ /** The router in the conversation's own frame — the one that reaches everything. */
16
+ export function flowRouter(flow) {
17
+ return routersOf(flow).find((s) => frameOf(flow, s) === flow.rootScreen);
76
18
  }
77
- /**
78
- * Whether a screen's typed text may keep going where it goes. Anything else is
79
- * repaired by `settle` — a router that was removed, or a scoped one whose
80
- * section is no longer above this screen.
81
- *
82
- * A screen pointed at the flow's router is always valid: that is how an author
83
- * says "from here, typing may open anything", and it survives every move.
84
- */
85
- export function routingIsValid(flow, screenId, targetId) {
86
- const target = flow.screens.find((s) => s.id === targetId);
87
- if (!target?.router)
88
- return false;
89
- const scope = target.router.scope;
90
- if (!scope)
91
- return true;
92
- const byId = new Map(flow.screens.map((s) => [s.id, s]));
93
- for (let at = byId.get(screenId); at; at = at.parent ? byId.get(at.parent) : undefined) {
94
- if (at.id === scope)
95
- return true;
96
- }
97
- return false;
19
+ /** The screen whose frame something sits in: the conversation, or a section. */
20
+ export function frameOf(flow, screen) {
21
+ return screen.frames[screen.frames.length - 1] ?? flow.rootScreen;
98
22
  }
99
23
  /**
100
- * Point every screen's typed text at a router that can serve it. Only links
101
- * that have become invalid are touched, so an author's own choice — "from this
102
- * screen, typing may open anything" — is left standing.
24
+ * The section a router serves — undefined for the one that serves the whole
25
+ * flow, and for anything that is not a router: only a router has a scope.
103
26
  */
104
- export function repairTypedRouting(draft, flow) {
105
- for (const screen of flow.screens) {
106
- for (const prompt of screen.prompts) {
107
- if (prompt.freeText?.kind !== 'screen')
108
- continue;
109
- if (routingIsValid(flow, screen.id, prompt.freeText.screen))
110
- continue;
111
- const target = routerFor(flow, screen.id);
112
- if (!target)
113
- continue;
114
- const jump = typedJumpOf(draft, prompt.id);
115
- if (jump)
116
- asRec(jump.config['with'])[flow.routeKey] = target.id;
117
- }
118
- }
119
- }
120
- /** The GO_TO a prompt's typed branch carries — the one naming a router. */
121
- export function typedJumpOf(draft, promptId) {
122
- const around = asSteps(containerOf(draft, promptId));
123
- const at = around.findIndex((s) => s.id === promptId);
124
- const gate = around[at + 1];
125
- if (gate?.type !== 'CONDITIONAL')
27
+ export function scopeOf(flow, router) {
28
+ if (!router.router)
126
29
  return undefined;
127
- return asSteps(asRec(gate.config)['if_true']).find((s) => s.type === 'GO_TO');
30
+ const frame = frameOf(flow, router);
31
+ return frame === flow.rootScreen ? undefined : flow.screens.find((s) => s.id === frame);
128
32
  }
129
33
  /**
130
- * The AI router's instructions and output contract, generated from the screens.
131
- * The author's own guidance (the router step's description) goes first; the
132
- * `enum` is what stops the agent naming a screen that doesn't exist.
34
+ * Where typed text goes from a screen: the router its question names.
35
+ *
36
+ * It is authored — one screen inside Payments may keep its typing in Payments
37
+ * while its neighbour sends everything to the top — so it is read, not derived.
133
38
  */
134
- export function writeRouters(draft, flow) {
135
- const textKey = typedTextKey(draft, flow);
136
- const service = serviceOf(flow);
137
- for (const screen of routersOf(flow)) {
138
- const router = screen.router;
139
- if (router.mode === 'keywords')
39
+ export function routerFor(flow, screenId) {
40
+ const screen = flow.screens.find((s) => s.id === screenId);
41
+ for (const prompt of screen?.prompts ?? []) {
42
+ const to = prompt.freeText;
43
+ if (to?.kind !== 'screen')
140
44
  continue;
141
- if (router.mode === 'classifier')
142
- writeClassifier(draft, flow, screen, candidatesOf(flow, screen), textKey, service);
143
- else
144
- writeAgentRouter(draft, flow, router, candidatesOf(flow, screen), textKey);
45
+ const router = flow.screens.find((s) => s.id === to.screen && s.kind === 'router');
46
+ if (router)
47
+ return router;
145
48
  }
49
+ return undefined;
146
50
  }
147
51
  /**
148
- * The service every router uses: the flow router's model, credential and how
149
- * sure it must be. Configured once, so scoping a section asks the author for
150
- * nothing.
151
- */
152
- function serviceOf(flow) {
153
- const main = flowRouter(flow)?.router;
154
- if (main?.mode !== 'classifier')
155
- return {};
156
- return {
157
- ...(main.model !== undefined ? { model: main.model } : {}),
158
- ...(main.credentialName !== undefined ? { credentialName: main.credentialName } : {}),
159
- ...(main.minConfidence !== undefined ? { minConfidence: main.minConfidence } : {}),
160
- };
161
- }
162
- /** An agent asked for JSON: its instructions and the screens it may name. */
163
- function writeAgentRouter(draft, flow, router, screens, textKey) {
164
- const step = findStep(draft, router.stepId, AGENT_STEP_TYPE);
165
- const lines = screens.map((s) => describeScreen(flow, s));
166
- step.config['task'] = [
167
- ...(router.guidance ? [router.guidance, ''] : []),
168
- textKey
169
- ? `Someone in this conversation typed: "{{steps.__loop.output.input.${textKey}}}"`
170
- : 'Someone in this conversation typed a message.',
171
- '',
172
- 'Send them to the screen that best answers it. The screens, with where each sits:',
173
- ...lines,
174
- '',
175
- 'Prefer the most specific screen. Only choose a [section] when the message is too vague for any of its screens.',
176
- `If nothing fits, choose "${flow.rootScreen}".`,
177
- `Answer with JSON only: {"${flow.routeKey}": "<id from the list>", "reason": "<one short sentence>"}.`,
178
- ].join('\n');
179
- step.config['outputType'] = 'json';
180
- step.config['outputSchema'] = {
181
- type: 'object',
182
- properties: {
183
- [flow.routeKey]: { type: 'string', enum: screens.map((s) => s.id) },
184
- reason: { type: 'string' },
185
- },
186
- required: [flow.routeKey, 'reason'],
187
- };
188
- }
189
- /**
190
- * A classifier router: the screens become its classes, each described the way
191
- * the agent router's list describes them, and the question it is asked carries
192
- * the author's guidance. Its endpoint, model and credential are the author's
193
- * and are never touched here.
52
+ * The routers a screen may send typing to: the ones whose frame encloses it.
53
+ *
54
+ * Innermost first, so "keep this in Payments" reads before "look anywhere".
194
55
  */
195
- function writeClassifier(draft, flow, screen, screens, textKey, service) {
196
- const router = screen.router;
197
- const step = findStep(draft, router.stepId, CLASSIFIER_STEP_TYPE);
198
- const section = router.scope ? flow.screens.find((s) => s.id === router.scope) : undefined;
199
- step.config['input'] = textKey ? `{{steps.__loop.output.input.${textKey}}}` : '';
200
- step.config['instructions'] = [
201
- ...(router.guidance ? [router.guidance, ''] : []),
202
- section
203
- ? `Which screen best answers what they typed? They are in ${breadcrumb(flow, section)}; prefer the most specific screen there, a [section] only when the message is too vague for any screen beneath it, and the first screen only when the message isn't about ${section.title} at all.`
204
- : 'Which screen best answers what they typed? Prefer the most specific one; a [section] only when the message is too vague for any screen beneath it.',
205
- ].join('\n');
206
- step.config['classes'] = Object.fromEntries(screens.map((s) => [s.id, describeClass(flow, s)]));
207
- // Every router asks the same service; only the flow's router is configured.
208
- for (const key of ['model', 'credentialName', 'minConfidence']) {
209
- if (service[key] !== undefined)
210
- step.config[key] = service[key];
211
- else
212
- delete step.config[key];
213
- }
214
- // Somewhere to send a message it can't place: a scoped router keeps them in
215
- // its own section, the flow's sends them back to the start.
216
- if (typeof step.config['minConfidence'] === 'number')
217
- step.config['fallbackClass'] = section?.id ?? flow.rootScreen;
218
- else
219
- delete step.config['fallbackClass'];
220
- }
221
- /** "payments_double_charge (Payments › Charges › Charged twice) [section] — the same trip was charged twice" */
222
- function describeScreen(flow, s) {
223
- if (s.kind === 'end')
224
- return `${s.id} — ends the conversation: ${s.about ?? 'they are done'}`;
225
- const section = flow.screens.some((c) => c.parent === s.id) ? ' [section]' : '';
226
- const about = s.about ?? `the ${breadcrumb(flow, s)} screen`;
227
- return `${s.id} (${breadcrumb(flow, s)})${section} — ${about}`;
228
- }
229
- /** The same description, without repeating the id the class key already carries. */
230
- function describeClass(flow, s) {
231
- if (s.kind === 'end')
232
- return `Ends the conversation — ${s.about ?? 'they are done'}`;
233
- const section = flow.screens.some((c) => c.parent === s.id) ? ' [section]' : '';
234
- const about = s.about ?? `the ${breadcrumb(flow, s)} screen`;
235
- return `${breadcrumb(flow, s)}${section} — ${about}`;
56
+ export function routersFor(flow, screenId) {
57
+ const enclosing = [screenId, ...ancestorsOf(flow, screenId)];
58
+ return routersOf(flow)
59
+ .filter((router) => enclosing.includes(frameOf(flow, router)))
60
+ .sort((a, b) => enclosing.indexOf(frameOf(flow, a)) - enclosing.indexOf(frameOf(flow, b)));
236
61
  }
237
- /** The cycle input that carries what they typed — read from a typed-text GO_TO. */
238
- function typedTextKey(draft, flow) {
239
- for (const s of flow.screens) {
240
- for (const p of s.prompts) {
241
- if (p.freeText?.kind !== 'screen' || !p.textField)
242
- continue;
243
- const jump = walk(asSteps(draft.steps)).find((x) => x.type === 'GO_TO' && Object.values(asRec(x.config['with'])).some((v) => typeof v === 'string' && v.includes(`steps.${p.id}.output.resumeData.data.${p.textField}`)));
244
- const carried = asRec(jump?.config['with']);
245
- const key = Object.keys(carried).find((k) => k !== flow.routeKey && typeof carried[k] === 'string' && carried[k].includes('{{'));
246
- if (key)
247
- return key;
248
- }
249
- }
250
- return undefined;
62
+ /** Whether typing on `screenId` could reach `routerId` at all. */
63
+ export function routingIsValid(flow, screenId, routerId) {
64
+ return routersFor(flow, screenId).some((router) => router.id === routerId);
251
65
  }
252
- export function agentRouter(flow) {
253
- return flow.screens.find((s) => s.router?.mode === 'agent');
254
- }
255
- /** The router that reads typed text, however it decides — classifier or agent. */
256
- export function aiRouter(flow) {
257
- return flow.screens.find((s) => s.router?.mode === 'classifier' || s.router?.mode === 'agent');
66
+ /** The screens a router may open: its own frame's subtree, as `settle` wrote it. */
67
+ export function candidatesOf(flow, router) {
68
+ const byId = new Map(flow.screens.map((s) => [s.id, s]));
69
+ return router.router?.candidates.map((id) => byId.get(id)).filter((s) => s !== undefined) ?? [];
258
70
  }
259
- /** The router that serves the flow as a whole — the one with no scope. */
260
- export function flowRouter(flow) {
261
- return flow.screens.find((s) => s.router !== undefined && s.router.scope === undefined);
71
+ /** The screens a router anywhere in the flow could open. */
72
+ export function routableScreens(flow) {
73
+ const byId = new Map(flow.screens.map((s) => [s.id, s]));
74
+ return subtreeOf(flow, flow.rootScreen)
75
+ .map((id) => byId.get(id))
76
+ .filter((s) => s !== undefined)
77
+ .filter((s) => s.kind !== 'router')
78
+ .filter((s) => s.kind !== 'end' || (s.about ?? '') !== '');
262
79
  }
263
- /**
264
- * Point a section and everything beneath it at one router. A bulk action —
265
- * scoping a section, un-scoping it, moving a branch into another section —
266
- * rather than something `settle` does, so a per-screen choice survives edits
267
- * elsewhere.
268
- */
269
- export function pointSubtreeAt(draft, flow, sectionId, routerId) {
270
- const inside = new Set([sectionId]);
271
- for (const s of treeOrder(flow))
272
- if (s.parent !== undefined && inside.has(s.parent))
273
- inside.add(s.id);
274
- for (const screen of flow.screens) {
275
- if (!inside.has(screen.id))
276
- continue;
277
- for (const prompt of screen.prompts) {
278
- if (prompt.freeText?.kind !== 'screen')
279
- continue;
280
- const jump = typedJumpOf(draft, prompt.id);
281
- if (jump)
282
- asRec(jump.config['with'])[flow.routeKey] = routerId;
283
- }
284
- }
80
+ /** The endings a router may choose — the ones whose author said when they apply. */
81
+ export function endsByTyping(flow) {
82
+ return flow.screens.filter((s) => s.kind === 'end' && (s.about ?? '') !== '');
83
+ }
84
+ /** How to name a router in the editor: what it asks, and what it reaches. */
85
+ export function routerName(flow, router) {
86
+ const section = scopeOf(flow, router);
87
+ const reach = section ? `${breadcrumb(flow, section)} and below` : 'anywhere';
88
+ return `${serviceName(router)} · ${reach}`;
89
+ }
90
+ function serviceName(router) {
91
+ const info = router.router;
92
+ if (!info)
93
+ return 'Router';
94
+ if (info.mode === 'classifier')
95
+ return info.credentialName ?? info.model ?? 'Classifier';
96
+ if (info.mode === 'agent')
97
+ return info.agent ?? 'AI agent';
98
+ return 'Keywords';
285
99
  }
286
100
  //# sourceMappingURL=routers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"routers.js","sourceRoot":"","sources":["../../src/conversational-flow/routers.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAElD,OAAO,EAAE,eAAe,EAAE,oBAAoB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAC9E,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,IAAI,EAAuB,MAAM,gBAAgB,CAAC;AAElG,yEAAyE;AACzE,MAAM,UAAU,eAAe,CAAC,IAAwB;IACtD,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ,IAAI,CAAC,CAAC,IAAI,KAAK,KAAK,CAAC,CAAC;AAC7E,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,YAAY,CAAC,IAAwB;IACnD,OAAO,IAAI,CA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CAAC,CAAC;AAChF,CAAC;AAED,6EAA6E;AAC7E,MAAM,UAAU,UAAU,CAAC,IAAwB,EAAE,MAAc;IACjE,MAAM,OAAO,GAAG,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACtC,MAAM,KAAK,GAAG,OAAO,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,UAAU,CAAC;IAC9E,OAAO,GAAG,WAAW,CAAC,MAAM,CAAC,MAAM,KAAK,EAAE,CAAC;AAC7C,CAAC;AAED,SAAS,WAAW,CAAC,MAAc;IACjC,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC;IAC3B,IAAI,CAAC,IAAI;QAAE,OAAO,QAAQ,CAAC;IAC3B,IAAI,IAAI,CAAC,IAAI,KAAK,YAAY;QAAE,OAAO,IAAI,CAAC,cAAc,IAAI,IAAI,CAAC,KAAK,IAAI,YAAY,CAAC;IACzF,IAAI,IAAI,CAAC,IAAI,KAAK,OAAO;QAAE,OAAO,IAAI,CAAC,KAAK,IAAI,UAAU,CAAC;IAC3D,OAAO,UAAU,CAAC;AACpB,CAAC"}
@@ -12,22 +12,16 @@ import type { WorkflowRuntime } from '../../runtime/workflow-runtime.js';
12
12
  import { type ConversationStreamEvent, type Transcript, type Turn, type TurnAnswer } from '../turn.js';
13
13
  export interface StartConversation {
14
14
  workflowId: string;
15
- /** Open with a message instead of waiting to be asked: it answers the first turn as typed text. */
16
- text?: string;
17
- /** Data for the flow's steps and agents, reachable as `{{trigger.context.<key>}}`. */
18
- context?: Record<string, unknown>;
19
- /** Who is on the other side, reachable as `{{trigger.user.id}}`. */
20
- user?: {
21
- id: string;
22
- name?: string;
23
- };
24
- /** The caller's own id for this conversation (a ticket, a chat session). */
25
- externalId?: string;
26
15
  /**
27
- * Where it runs — `api` unless said otherwise; the builder's Test says `test`.
28
- * Runs can't label test runs by it yet: see “Not done yet” in the section README.
16
+ * What the conversation is started with — the trigger's own payload, exactly
17
+ * as `POST /workflows/:id/trigger` takes it, validated against the same
18
+ * `inputSchema` and read as `{{trigger.context.<key>}}`.
19
+ *
20
+ * Everything the caller wants to carry goes here: an order id, who is asking,
21
+ * their ticket. One place, which is the place the builder can offer in its
22
+ * value picker and the validator can check.
29
23
  */
30
- channel?: string;
24
+ context?: Record<string, unknown>;
31
25
  }
32
26
  export interface AnswerTurn extends TurnAnswer {
33
27
  turnId: string;
@@ -43,20 +37,54 @@ export declare const MAX_WAIT_MS = 25000;
43
37
  export declare class ConversationService<TCtx> {
44
38
  private readonly runtime;
45
39
  constructor(runtime: WorkflowRuntime<any, TCtx>);
46
- start(ctx: TCtx, input: StartConversation, waitMs: number): Promise<Turn>;
40
+ /**
41
+ * Starting a conversation is triggering the workflow: the same payload, the
42
+ * same validation, the same references. Nothing is added on the way through.
43
+ */
44
+ start(ctx: TCtx, input: StartConversation, waitMs: number): Promise<{
45
+ id: string;
46
+ turn: Turn;
47
+ }>;
47
48
  /** The current turn, waiting up to `waitMs` while the run is still working. */
48
49
  get(ctx: TCtx, id: string, waitMs: number): Promise<Turn>;
49
50
  answer(ctx: TCtx, id: string, input: AnswerTurn, waitMs: number): Promise<Turn>;
51
+ /**
52
+ * Everything said so far.
53
+ *
54
+ * Not an endpoint of its own — a caller following the conversation already has
55
+ * every turn, and the one case that needs history without having watched it,
56
+ * a person taking over a hand off, gets it on the turn as `handoff.context`.
57
+ * It stays here for a host that wants to expose it for its own reasons.
58
+ */
50
59
  transcript(ctx: TCtx, id: string): Promise<Transcript>;
51
60
  end(ctx: TCtx, id: string): Promise<Turn>;
52
61
  /**
53
- * The conversation as it happens: the current turn, then what is working and
54
- * what agents write while it does, then each new turn — until it ends.
62
+ * The conversation as it happens: what is working, what agents write while it
63
+ * does, and each new turn.
64
+ *
65
+ * Two shapes, for two different questions:
66
+ *
67
+ * - `until: 'their-move'` — the response to an action. It ends as soon as the
68
+ * conversation is waiting on someone again, so the close *is* the "your
69
+ * move" signal and a reply arrives exactly once, on the connection that
70
+ * asked for it. This is what a chat client uses.
71
+ * - `until: 'the-end'` — watching. It runs until the conversation is over, for
72
+ * picking the thread back up after a reload, or a second viewer.
55
73
  *
56
74
  * Yields `undefined` when nothing happened for a while, so a transport can
57
75
  * keep an idle connection open. Stop iterating to disconnect.
58
76
  */
59
- stream(ctx: TCtx, id: string): AsyncGenerator<ConversationStreamEvent | undefined>;
77
+ stream(ctx: TCtx, id: string, until?: 'their-move' | 'the-end',
78
+ /**
79
+ * The turn just answered, when there is one.
80
+ *
81
+ * A resume is applied a moment after it is accepted, so a stream opened
82
+ * straight afterwards can still find the answered turn sitting there, open.
83
+ * Without knowing which turn that was it would close immediately and the
84
+ * client would be told it was their move again. So "their move" means *a
85
+ * different* turn, not merely an open one.
86
+ */
87
+ after?: string): AsyncGenerator<ConversationStreamEvent | undefined>;
60
88
  private load;
61
89
  /**
62
90
  * Read the turn; while the run is working, wait for its next event and read
@@ -1 +1 @@
1
- {"version":3,"file":"conversation-service.d.ts","sourceRoot":"","sources":["../../../src/conversational-flow/server/conversation-service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;AAMzE,OAAO,EAML,KAAK,uBAAuB,EAE5B,KAAK,UAAU,EACf,KAAK,IAAI,EACT,KAAK,UAAU,EAChB,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,iBAAiB;IAChC,UAAU,EAAE,MAAM,CAAC;IACnB,mGAAmG;IACnG,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,sFAAsF;IACtF,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,oEAAoE;IACpE,IAAI,CAAC,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IACrC,4EAA4E;IAC5E,UAAU,CAAC,EAAE,MAAM,CAAC;I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,OAAO,CAAC,IAAI,CAAC;IAsBrF;;;;;;;OAOG;IACG,UAAU,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IAItD,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAM/C;;;;;;;;;;;;;;;OAeG;IACI,MAAM,CACX,GAAG,EAAE,IAAI,EACT,EAAE,EAAE,MAAM,EACV,KAAK,GAAE,YAAY,GAAG,SAAqB;IAC3C;;;;;;;;OAQG;IACH,KAAK,CAAC,EAAE,MAAM,GACb,cAAc,CAAC,uBAAuB,GAAG,SAAS,CAAC;YAwDxC,IAAI;IAMlB;;;;;;OAMG;YACW,WAAW;CA6B1B"}
@@ -30,20 +30,14 @@ export class ConversationService {
30
30
  constructor(runtime) {
31
31
  this.runtime = runtime;
32
32
  }
33
+ /**
34
+ * Starting a conversation is triggering the workflow: the same payload, the
35
+ * same validation, the same references. Nothing is added on the way through.
36
+ */
33
37
  async start(ctx, input, waitMs) {
34
- const deadline = Date.now() + clampWait(waitMs);
35
- const payload = { channel: input.channel ?? 'api' };
36
- if (input.context)
37
- payload['context'] = input.context;
38
- if (input.user)
39
- payload['user'] = input.user;
40
- if (input.externalId)
41
- payload['externalId'] = input.externalId;
38
+ const payload = input.context ? { context: input.context } : {};
42
39
  const id = await this.runtime.triggerManual(ctx, input.workflowId, payload);
43
- const first = await this.waitForTurn(ctx, id, deadline);
44
- if (!input.text || first.status !== 'awaiting_reply' || !first.reply?.text.allowed || !first.turnId)
45
- return first;
46
- return this.answer(ctx, id, { turnId: first.turnId, text: input.text }, deadline - Date.now());
40
+ return { id, turn: await this.waitForTurn(ctx, id, Date.now() + clampWait(waitMs)) };
47
41
  }
48
42
  /** The current turn, waiting up to `waitMs` while the run is still working. */
49
43
  async get(ctx, id, waitMs) {
@@ -71,6 +65,14 @@ export class ConversationService {
71
65
  return this.waitForTurn(ctx, id, deadline);
72
66
  throw new ConversationConflictError(turn);
73
67
  }
68
+ /**
69
+ * Everything said so far.
70
+ *
71
+ * Not an endpoint of its own — a caller following the conversation already has
72
+ * every turn, and the one case that needs history without having watched it,
73
+ * a person taking over a hand off, gets it on the turn as `handoff.context`.
74
+ * It stays here for a host that wants to expose it for its own reasons.
75
+ */
74
76
  async transcript(ctx, id) {
75
77
  return readTranscript(await this.load(ctx, id));
76
78
  }
@@ -81,24 +83,51 @@ export class ConversationService {
81
83
  return readTurn(await this.load(ctx, id));
82
84
  }
83
85
  /**
84
- * The conversation as it happens: the current turn, then what is working and
85
- * what agents write while it does, then each new turn — until it ends.
86
+ * The conversation as it happens: what is working, what agents write while it
87
+ * does, and each new turn.
88
+ *
89
+ * Two shapes, for two different questions:
90
+ *
91
+ * - `until: 'their-move'` — the response to an action. It ends as soon as the
92
+ * conversation is waiting on someone again, so the close *is* the "your
93
+ * move" signal and a reply arrives exactly once, on the connection that
94
+ * asked for it. This is what a chat client uses.
95
+ * - `until: 'the-end'` — watching. It runs until the conversation is over, for
96
+ * picking the thread back up after a reload, or a second viewer.
86
97
  *
87
98
  * Yields `undefined` when nothing happened for a while, so a transport can
88
99
  * keep an idle connection open. Stop iterating to disconnect.
89
100
  */
90
- async *stream(ctx, id) {
101
+ async *stream(ctx, id, until = 'the-end',
102
+ /**
103
+ * The turn just answered, when there is one.
104
+ *
105
+ * A resume is applied a moment after it is accepted, so a stream opened
106
+ * straight afterwards can still find the answered turn sitting there, open.
107
+ * Without knowing which turn that was it would close immediately and the
108
+ * client would be told it was their move again. So "their move" means *a
109
+ * different* turn, not merely an open one.
110
+ */
111
+ after) {
112
+ const done = (turn) => FINAL.has(turn.status) || (until === 'their-move' && OPEN.has(turn.status) && turn.turnId !== after);
91
113
  const channel = createEventChannel();
92
114
  // Subscribe before the first read, so nothing between them is missed.
93
115
  const unsubscribe = await this.runtime.eventBus?.subscribe(executionTopic(id), channel.push);
94
116
  const backstop = setInterval(() => channel.push('tick'), BACKSTOP_MS);
95
117
  let settle;
118
+ let begun = false;
96
119
  try {
97
120
  const run = await this.load(ctx, id);
98
121
  const flow = run.config ? readConversationalFlow(run.config) : null;
99
122
  let last = readTurn(run);
100
- yield { type: 'turn', turn: last };
101
- if (FINAL.has(last.status))
123
+ // Answering an action, the turn being moved past is not news: the caller
124
+ // has just sent it. So the stream carries only what happened after.
125
+ if (last.turnId !== after)
126
+ yield { type: 'turn', turn: last };
127
+ // Past this point the transport has answered, so a failure has to be said
128
+ // in the stream. Before it — access, not found — it is still a status.
129
+ begun = true;
130
+ if (done(last))
102
131
  return;
103
132
  for await (const event of channel) {
104
133
  const progress = event === 'tick' ? null : streamEventOf(event, flow);
@@ -110,7 +139,7 @@ export class ConversationService {
110
139
  if (turn.turnId !== last.turnId || turn.status !== last.status) {
111
140
  last = turn;
112
141
  yield { type: 'turn', turn };
113
- if (FINAL.has(turn.status))
142
+ if (done(turn))
114
143
  return;
115
144
  }
116
145
  else if (event === 'tick') {
@@ -122,6 +151,15 @@ export class ConversationService {
122
151
  }
123
152
  }
124
153
  }
154
+ catch (err) {
155
+ // Nothing has been sent yet, so this can still be a status — which is what
156
+ // the first read is for: access and not-found refuse properly.
157
+ if (!begun)
158
+ throw err;
159
+ // Otherwise the status line is long gone, and the stream itself is the
160
+ // only honest place left to say it.
161
+ yield { type: 'error', code: 'stream_failed', message: err instanceof Error ? err.message : 'The conversation could not be followed.' };
162
+ }
125
163
  finally {
126
164
  clearInterval(backstop);
127
165
  clearTimeout(settle);