@superpanel-sdk/runtime 0.0.1-internal.1

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 (169) hide show
  1. package/README.md +35 -0
  2. package/bin/superpanel-sdk-runtime.mjs +22 -0
  3. package/dist/builder/codegen/emit.d.ts +17 -0
  4. package/dist/builder/codegen/emit.js +143 -0
  5. package/dist/builder/codegen/generate.d.ts +21 -0
  6. package/dist/builder/codegen/generate.js +90 -0
  7. package/dist/builder/compile/compile-agenda.d.ts +15 -0
  8. package/dist/builder/compile/compile-agenda.js +59 -0
  9. package/dist/builder/compile/compile-sequence.d.ts +24 -0
  10. package/dist/builder/compile/compile-sequence.js +121 -0
  11. package/dist/builder/compile/function-code.d.ts +6 -0
  12. package/dist/builder/compile/function-code.js +30 -0
  13. package/dist/builder/compile/registry.d.ts +13 -0
  14. package/dist/builder/compile/registry.js +42 -0
  15. package/dist/builder/load.d.ts +22 -0
  16. package/dist/builder/load.js +169 -0
  17. package/dist/builder/merge.d.ts +4 -0
  18. package/dist/builder/merge.js +19 -0
  19. package/dist/builder/paths.d.ts +10 -0
  20. package/dist/builder/paths.js +22 -0
  21. package/dist/builder/types.d.ts +31 -0
  22. package/dist/builder/types.js +2 -0
  23. package/dist/cli/compile/index.d.ts +4 -0
  24. package/dist/cli/compile/index.js +11 -0
  25. package/dist/cli/index.d.ts +3 -0
  26. package/dist/cli/index.js +64 -0
  27. package/dist/cli/serve/index.d.ts +9 -0
  28. package/dist/cli/serve/index.js +340 -0
  29. package/dist/cli/type-gen/index.d.ts +5 -0
  30. package/dist/cli/type-gen/index.js +90 -0
  31. package/dist/compute-host/compute-host.d.ts +38 -0
  32. package/dist/compute-host/compute-host.js +45 -0
  33. package/dist/compute-host/index.d.ts +5 -0
  34. package/dist/compute-host/index.js +3 -0
  35. package/dist/compute-host/source-compute-host.d.ts +29 -0
  36. package/dist/compute-host/source-compute-host.js +72 -0
  37. package/dist/core/context/account-fields.d.ts +12 -0
  38. package/dist/core/context/account-fields.js +2 -0
  39. package/dist/core/context/contact.d.ts +60 -0
  40. package/dist/core/context/contact.js +34 -0
  41. package/dist/core/context/db.d.ts +69 -0
  42. package/dist/core/context/db.js +44 -0
  43. package/dist/core/context/docusign.d.ts +134 -0
  44. package/dist/core/context/docusign.js +108 -0
  45. package/dist/core/context/env.d.ts +9 -0
  46. package/dist/core/context/env.js +19 -0
  47. package/dist/core/context/filevine.d.ts +323 -0
  48. package/dist/core/context/filevine.js +254 -0
  49. package/dist/core/context/http.d.ts +9 -0
  50. package/dist/core/context/http.js +4 -0
  51. package/dist/core/context/hubspot.d.ts +250 -0
  52. package/dist/core/context/hubspot.js +191 -0
  53. package/dist/core/context/platform-registry.d.ts +67 -0
  54. package/dist/core/context/platform-registry.js +11 -0
  55. package/dist/core/context/platform.d.ts +33 -0
  56. package/dist/core/context/platform.js +2 -0
  57. package/dist/core/context/twilio.d.ts +50 -0
  58. package/dist/core/context/twilio.js +42 -0
  59. package/dist/core/context/types.d.ts +87 -0
  60. package/dist/core/context/types.js +9 -0
  61. package/dist/core/functions/define/case-context.d.ts +14 -0
  62. package/dist/core/functions/define/case-context.js +15 -0
  63. package/dist/core/functions/define/compute.d.ts +7 -0
  64. package/dist/core/functions/define/compute.js +17 -0
  65. package/dist/core/functions/define/custom.d.ts +4 -0
  66. package/dist/core/functions/define/custom.js +8 -0
  67. package/dist/core/functions/define/system.d.ts +7 -0
  68. package/dist/core/functions/define/system.js +12 -0
  69. package/dist/core/functions/func-ref.d.ts +16 -0
  70. package/dist/core/functions/func-ref.js +20 -0
  71. package/dist/core/functions/types.d.ts +78 -0
  72. package/dist/core/functions/types.js +5 -0
  73. package/dist/core/slot/compile-schema.d.ts +24 -0
  74. package/dist/core/slot/compile-schema.js +336 -0
  75. package/dist/core/slot/deps.d.ts +15 -0
  76. package/dist/core/slot/deps.js +88 -0
  77. package/dist/core/slot/derived.d.ts +32 -0
  78. package/dist/core/slot/derived.js +115 -0
  79. package/dist/core/slot/field.d.ts +36 -0
  80. package/dist/core/slot/field.js +97 -0
  81. package/dist/core/slot/id-ref.d.ts +4 -0
  82. package/dist/core/slot/id-ref.js +15 -0
  83. package/dist/core/slot/index.d.ts +15 -0
  84. package/dist/core/slot/index.js +14 -0
  85. package/dist/core/slot/path.d.ts +18 -0
  86. package/dist/core/slot/path.js +22 -0
  87. package/dist/core/slot/row.d.ts +22 -0
  88. package/dist/core/slot/row.js +54 -0
  89. package/dist/core/slot/schema.d.ts +102 -0
  90. package/dist/core/slot/schema.js +17 -0
  91. package/dist/core/slot/sf-schema.d.ts +23 -0
  92. package/dist/core/slot/sf-schema.js +125 -0
  93. package/dist/core/slot/sf-validate.d.ts +14 -0
  94. package/dist/core/slot/sf-validate.js +132 -0
  95. package/dist/core/slot/snapshot.d.ts +18 -0
  96. package/dist/core/slot/snapshot.js +138 -0
  97. package/dist/core/slot/types.d.ts +144 -0
  98. package/dist/core/slot/types.js +2 -0
  99. package/dist/core/slot/value.d.ts +9 -0
  100. package/dist/core/slot/value.js +97 -0
  101. package/dist/core/slot/write.d.ts +41 -0
  102. package/dist/core/slot/write.js +319 -0
  103. package/dist/core/stage/builders.d.ts +36 -0
  104. package/dist/core/stage/builders.js +28 -0
  105. package/dist/core/stage/condition.d.ts +17 -0
  106. package/dist/core/stage/condition.js +198 -0
  107. package/dist/core/stage/drive.d.ts +43 -0
  108. package/dist/core/stage/drive.js +77 -0
  109. package/dist/core/stage/finish-when.d.ts +20 -0
  110. package/dist/core/stage/finish-when.js +82 -0
  111. package/dist/core/stage/flatten.d.ts +27 -0
  112. package/dist/core/stage/flatten.js +464 -0
  113. package/dist/core/stage/index.d.ts +11 -0
  114. package/dist/core/stage/index.js +10 -0
  115. package/dist/core/stage/simulate.d.ts +24 -0
  116. package/dist/core/stage/simulate.js +33 -0
  117. package/dist/core/stage/transfer-fields.d.ts +14 -0
  118. package/dist/core/stage/transfer-fields.js +88 -0
  119. package/dist/core/stage/types.d.ts +177 -0
  120. package/dist/core/stage/types.js +2 -0
  121. package/dist/core/stage/validate.d.ts +11 -0
  122. package/dist/core/stage/validate.js +226 -0
  123. package/dist/core/stage/walk.d.ts +34 -0
  124. package/dist/core/stage/walk.js +96 -0
  125. package/dist/dev-overlay/protocol.d.ts +53 -0
  126. package/dist/dev-overlay/protocol.js +21 -0
  127. package/dist/fn-context/build-context.d.ts +11 -0
  128. package/dist/fn-context/build-context.js +21 -0
  129. package/dist/fn-context/fake/empty-api.d.ts +9 -0
  130. package/dist/fn-context/fake/empty-api.js +50 -0
  131. package/dist/fn-context/fake/index.d.ts +12 -0
  132. package/dist/fn-context/fake/index.js +22 -0
  133. package/dist/fn-context/index.d.ts +9 -0
  134. package/dist/fn-context/index.js +8 -0
  135. package/dist/fn-context/log.d.ts +8 -0
  136. package/dist/fn-context/log.js +29 -0
  137. package/dist/fn-context/make-ctx.d.ts +9 -0
  138. package/dist/fn-context/make-ctx.js +51 -0
  139. package/dist/fn-context/rpc/errors.d.ts +25 -0
  140. package/dist/fn-context/rpc/errors.js +23 -0
  141. package/dist/fn-context/rpc/index.d.ts +10 -0
  142. package/dist/fn-context/rpc/index.js +9 -0
  143. package/dist/fn-context/rpc/integration/index.d.ts +51 -0
  144. package/dist/fn-context/rpc/integration/index.js +19 -0
  145. package/dist/fn-context/rpc/integration/list-bindings.d.ts +21 -0
  146. package/dist/fn-context/rpc/integration/list-bindings.js +18 -0
  147. package/dist/fn-context/rpc/log.d.ts +36 -0
  148. package/dist/fn-context/rpc/log.js +16 -0
  149. package/dist/fn-context/rpc/platform.d.ts +28 -0
  150. package/dist/fn-context/rpc/platform.js +15 -0
  151. package/dist/fn-context/rpc/request-id.d.ts +5 -0
  152. package/dist/fn-context/rpc/request-id.js +13 -0
  153. package/dist/fn-context/rpc/trpc-client.d.ts +7 -0
  154. package/dist/fn-context/rpc/trpc-client.js +109 -0
  155. package/dist/fn-context/rpc/trpc-init.d.ts +17 -0
  156. package/dist/fn-context/rpc/trpc-init.js +17 -0
  157. package/dist/fn-context/rpc/trpc-router-factory.d.ts +84 -0
  158. package/dist/fn-context/rpc/trpc-router-factory.js +9 -0
  159. package/dist/fn-context/types.d.ts +17 -0
  160. package/dist/fn-context/types.js +2 -0
  161. package/dist/index.d.ts +47 -0
  162. package/dist/index.js +34 -0
  163. package/dist/voice-service/actions.d.ts +20 -0
  164. package/dist/voice-service/actions.js +2 -0
  165. package/dist/voice-service/index.d.ts +3 -0
  166. package/dist/voice-service/index.js +3 -0
  167. package/dist/voice-service/types.d.ts +1010 -0
  168. package/dist/voice-service/types.js +16 -0
  169. package/package.json +55 -0
@@ -0,0 +1,464 @@
1
+ import { resolveFuncName } from '../functions/func-ref.js';
2
+ import { isOtherwise, jsonRules } from './condition.js';
3
+ import { andCompiledWhen, compileAuthoringWhen, compileWhen, whenFields } from './finish-when.js';
4
+ import { transferResultField } from './transfer-fields.js';
5
+ function isTopicNode(node) {
6
+ return node.kind === 'topic';
7
+ }
8
+ function isActionNode(node) {
9
+ return node.kind === 'action';
10
+ }
11
+ function isTransferNode(node) {
12
+ return node.kind === 'transfer';
13
+ }
14
+ function isSectionNode(node) {
15
+ return node.kind === 'section';
16
+ }
17
+ function applyWhenOf(node) {
18
+ if (node.kind === 'branch' || node.kind === 'switchOn')
19
+ return null;
20
+ return node.applyWhen;
21
+ }
22
+ function toAgendaBlock(node) {
23
+ if (node.kind === 'topic') {
24
+ return {
25
+ kind: 'topic',
26
+ id: node.id,
27
+ label: node.label,
28
+ instructions: node.instructions,
29
+ fields: node.fields,
30
+ finishWhen: node.finishWhen,
31
+ onNoProgress: node.onNoProgress,
32
+ };
33
+ }
34
+ if (node.kind === 'action') {
35
+ return {
36
+ kind: 'action',
37
+ id: node.id,
38
+ label: node.label,
39
+ run: node.run,
40
+ finishWhen: node.finishWhen,
41
+ say: node.say,
42
+ };
43
+ }
44
+ return {
45
+ kind: 'transfer',
46
+ id: node.id,
47
+ label: node.label,
48
+ numbers: node.numbers,
49
+ mode: node.mode,
50
+ say: node.say,
51
+ unsuccessfulSay: node.unsuccessfulSay,
52
+ };
53
+ }
54
+ function compileBlock(block, functions, issues, path) {
55
+ try {
56
+ if (block.kind === 'topic') {
57
+ const finishWhen = compileWhen(block.finishWhen, functions);
58
+ const fields = block.fields ?? whenFields(finishWhen);
59
+ return { ...block, fields, finishWhen };
60
+ }
61
+ if (block.kind === 'action') {
62
+ const finishWhen = compileWhen(block.finishWhen, functions);
63
+ const run = resolveFuncName(block.run, functions);
64
+ return { ...block, run, finishWhen };
65
+ }
66
+ const resultField = transferResultField(block.id);
67
+ const finishWhen = compileWhen(jsonRules({
68
+ fact: resultField,
69
+ path: '$.state',
70
+ operator: 'in',
71
+ value: ['filled', 'asked_unknown', 'declined'],
72
+ }), functions);
73
+ return { ...block, finishWhen, resultField };
74
+ }
75
+ catch (error) {
76
+ issues.push({
77
+ code: 'invalid-finish-when',
78
+ message: error instanceof Error ? error.message : String(error),
79
+ path,
80
+ });
81
+ return null;
82
+ }
83
+ }
84
+ function registerBlock(block, compiled, functions, issues, path) {
85
+ const existing = compiled[block.id];
86
+ if (existing && existing.kind !== block.kind) {
87
+ issues.push({
88
+ code: 'block-kind-mismatch',
89
+ message: `Block ${block.id} kind mismatch`,
90
+ path,
91
+ });
92
+ return existing;
93
+ }
94
+ const next = compileBlock(block, functions, issues, path);
95
+ if (!next)
96
+ return existing ?? null;
97
+ compiled[block.id] = next;
98
+ return next;
99
+ }
100
+ function expandSugar(nodes, issues, path) {
101
+ const out = [];
102
+ for (let i = 0; i < nodes.length; i++) {
103
+ const node = nodes[i];
104
+ const childPath = `${path}[${i}]`;
105
+ if (node.kind === 'branch') {
106
+ out.push(...expandBranch(node, issues, childPath));
107
+ }
108
+ else if (node.kind === 'switchOn') {
109
+ out.push(...expandSwitchOn(node, issues, childPath));
110
+ }
111
+ else if (node.kind === 'section') {
112
+ out.push({ ...node, children: expandSugar(node.children, issues, `${childPath}/${node.id}`) });
113
+ }
114
+ else {
115
+ out.push(node);
116
+ }
117
+ }
118
+ return out;
119
+ }
120
+ function expandBranch(node, issues, path) {
121
+ const children = expandSugar(node.children, issues, path);
122
+ const otherwiseIndexes = [];
123
+ for (let i = 0; i < children.length; i++) {
124
+ if (isOtherwise(applyWhenOf(children[i])))
125
+ otherwiseIndexes.push(i);
126
+ }
127
+ if (otherwiseIndexes.length > 1) {
128
+ issues.push({
129
+ code: 'otherwise-not-unique',
130
+ message: 'branch() otherwise must appear at most once',
131
+ path,
132
+ });
133
+ }
134
+ if (otherwiseIndexes.length === 1 && otherwiseIndexes[0] !== children.length - 1) {
135
+ issues.push({
136
+ code: 'otherwise-not-last',
137
+ message: 'branch() otherwise must be last',
138
+ path,
139
+ });
140
+ }
141
+ const explicit = children.filter((child) => !isOtherwise(applyWhenOf(child)));
142
+ if (explicit.length === 0) {
143
+ issues.push({
144
+ code: 'branch-no-explicit',
145
+ message: 'branch() needs at least one explicit condition branch',
146
+ path,
147
+ });
148
+ return children;
149
+ }
150
+ const explicitWhens = [];
151
+ for (const child of explicit) {
152
+ const when = applyWhenOf(child);
153
+ if (when == null) {
154
+ issues.push({
155
+ code: 'branch-null-sibling',
156
+ message: 'branch() explicit sibling cannot have applyWhen: null',
157
+ path,
158
+ });
159
+ continue;
160
+ }
161
+ if (isOtherwise(when))
162
+ continue;
163
+ explicitWhens.push(when);
164
+ }
165
+ if (otherwiseIndexes.length === 1 && explicitWhens.length > 0) {
166
+ const index = otherwiseIndexes[0];
167
+ const child = children[index];
168
+ children[index] = withApplyWhen(child, { op: 'otherwise', notAny: explicitWhens });
169
+ }
170
+ return children;
171
+ }
172
+ function expandSwitchOn(node, issues, path) {
173
+ const children = [];
174
+ for (const [key, tree] of Object.entries(node.cases)) {
175
+ children.push({
176
+ kind: 'section',
177
+ id: key,
178
+ label: key,
179
+ applyWhen: jsonRules({ fact: node.field, operator: 'equal', value: key }),
180
+ children: tree,
181
+ });
182
+ }
183
+ if (node.otherwise) {
184
+ children.push({
185
+ kind: 'section',
186
+ id: 'otherwise',
187
+ label: 'otherwise',
188
+ applyWhen: { op: 'otherwise' },
189
+ children: node.otherwise,
190
+ });
191
+ }
192
+ return expandBranch({ kind: 'branch', children }, issues, path);
193
+ }
194
+ function withApplyWhen(node, applyWhen) {
195
+ if (node.kind === 'branch' || node.kind === 'switchOn')
196
+ return node;
197
+ return { ...node, applyWhen };
198
+ }
199
+ function tryCompileAuthoringWhen(raw, functions, issues, path) {
200
+ try {
201
+ return compileAuthoringWhen(raw, functions);
202
+ }
203
+ catch (error) {
204
+ issues.push({
205
+ code: 'invalid-apply-when',
206
+ message: error instanceof Error ? error.message : String(error),
207
+ path,
208
+ });
209
+ return null;
210
+ }
211
+ }
212
+ function uniquePlacementId(base, used) {
213
+ if (!used.has(base)) {
214
+ used.add(base);
215
+ return base;
216
+ }
217
+ let i = 2;
218
+ while (used.has(`${base}-${i}`))
219
+ i += 1;
220
+ const id = `${base}-${i}`;
221
+ used.add(id);
222
+ return id;
223
+ }
224
+ function flattenAuthoring(nodes, inherited, groupPath, compiled, functions, issues, usedIds, path) {
225
+ const expanded = expandSugar(nodes, issues, path);
226
+ const items = [];
227
+ for (let i = 0; i < expanded.length; i++) {
228
+ const node = expanded[i];
229
+ const childPath = `${path}[${i}]`;
230
+ if (isSectionNode(node)) {
231
+ if (isOtherwise(node.applyWhen) && node.applyWhen.notAny == null) {
232
+ issues.push({
233
+ code: 'otherwise-unexpanded',
234
+ message: 'otherwise() is only valid inside branch() / switchOn()',
235
+ path: childPath,
236
+ });
237
+ continue;
238
+ }
239
+ const compiledWhen = tryCompileAuthoringWhen(node.applyWhen, functions, issues, childPath);
240
+ if (!compiledWhen)
241
+ continue;
242
+ const merged = andCompiledWhen(inherited, compiledWhen);
243
+ const sectionId = uniquePlacementId(node.id, usedIds);
244
+ items.push(...flattenAuthoring(node.children, merged, [...groupPath, sectionId], compiled, functions, issues, usedIds, `${childPath}/${node.id}`));
245
+ continue;
246
+ }
247
+ if (node.kind === 'branch') {
248
+ items.push(...flattenAuthoring(node.children, inherited, groupPath, compiled, functions, issues, usedIds, childPath));
249
+ continue;
250
+ }
251
+ if (node.kind === 'switchOn') {
252
+ items.push(...flattenAuthoring(expandSwitchOn(node, issues, childPath), inherited, groupPath, compiled, functions, issues, usedIds, childPath));
253
+ continue;
254
+ }
255
+ if (isOtherwise(node.applyWhen) && node.applyWhen.notAny == null) {
256
+ issues.push({
257
+ code: 'otherwise-unexpanded',
258
+ message: 'otherwise() is only valid inside branch() / switchOn()',
259
+ path: childPath,
260
+ });
261
+ continue;
262
+ }
263
+ const compiledBlock = registerBlock(toAgendaBlock(node), compiled, functions, issues, childPath);
264
+ if (!compiledBlock)
265
+ continue;
266
+ const compiledWhen = tryCompileAuthoringWhen(node.applyWhen, functions, issues, childPath);
267
+ if (!compiledWhen)
268
+ continue;
269
+ const placementId = uniquePlacementId(node.id, usedIds);
270
+ items.push({
271
+ ...compiledBlock,
272
+ placementId,
273
+ applyWhen: andCompiledWhen(inherited, compiledWhen),
274
+ groupPath,
275
+ });
276
+ }
277
+ return items;
278
+ }
279
+ function flattenPlacements(placements, blocks, inherited, groupPath, compiled, functions, issues, usedIds, path) {
280
+ const items = [];
281
+ for (let i = 0; i < placements.length; i++) {
282
+ const placement = placements[i];
283
+ const childPath = `${path}[${i}]`;
284
+ if (placement.kind === 'section') {
285
+ if (isOtherwise(placement.applyWhen) && placement.applyWhen.notAny == null) {
286
+ issues.push({
287
+ code: 'otherwise-unexpanded',
288
+ message: 'otherwise() is only valid inside branch() / switchOn()',
289
+ path: childPath,
290
+ });
291
+ continue;
292
+ }
293
+ const compiledWhen = tryCompileAuthoringWhen(placement.applyWhen, functions, issues, childPath);
294
+ if (!compiledWhen)
295
+ continue;
296
+ const merged = andCompiledWhen(inherited, compiledWhen);
297
+ const sectionId = uniquePlacementId(placement.placementId, usedIds);
298
+ items.push(...flattenPlacements(placement.children, blocks, merged, [...groupPath, sectionId], compiled, functions, issues, usedIds, `${childPath}/${placement.placementId}`));
299
+ continue;
300
+ }
301
+ const block = blocks[placement.blockId];
302
+ if (!block) {
303
+ issues.push({
304
+ code: 'unknown-block',
305
+ message: `Unknown block: ${placement.blockId}`,
306
+ path: childPath,
307
+ });
308
+ continue;
309
+ }
310
+ if (isOtherwise(placement.applyWhen) && placement.applyWhen.notAny == null) {
311
+ issues.push({
312
+ code: 'otherwise-unexpanded',
313
+ message: 'otherwise() is only valid inside branch() / switchOn()',
314
+ path: childPath,
315
+ });
316
+ continue;
317
+ }
318
+ const compiledBlock = registerBlock(block, compiled, functions, issues, childPath);
319
+ if (!compiledBlock)
320
+ continue;
321
+ const compiledWhen = tryCompileAuthoringWhen(placement.applyWhen, functions, issues, childPath);
322
+ if (!compiledWhen)
323
+ continue;
324
+ const placementId = uniquePlacementId(placement.placementId, usedIds);
325
+ items.push({
326
+ ...compiledBlock,
327
+ placementId,
328
+ applyWhen: andCompiledWhen(inherited, compiledWhen),
329
+ groupPath,
330
+ });
331
+ }
332
+ return items;
333
+ }
334
+ function asSectionPlacement(node, usedIds, path, issues) {
335
+ if (isSectionNode(node)) {
336
+ return {
337
+ kind: 'section',
338
+ placementId: uniquePlacementId(node.id, usedIds),
339
+ label: node.label,
340
+ applyWhen: node.applyWhen,
341
+ children: node.children.map((child, i) => asPlacement(child, usedIds, `${path}/${node.id}[${i}]`, issues)),
342
+ };
343
+ }
344
+ if (isTopicNode(node) || isActionNode(node) || isTransferNode(node)) {
345
+ return {
346
+ kind: 'node',
347
+ placementId: uniquePlacementId(node.id, usedIds),
348
+ blockId: node.id,
349
+ applyWhen: node.applyWhen,
350
+ };
351
+ }
352
+ issues.push({
353
+ code: 'invalid-placement',
354
+ message: `Cannot place ${node.kind} as a placement child before sugar expansion`,
355
+ path,
356
+ });
357
+ return {
358
+ kind: 'section',
359
+ placementId: uniquePlacementId('_invalid', usedIds),
360
+ label: '_invalid',
361
+ applyWhen: null,
362
+ children: [],
363
+ };
364
+ }
365
+ function asPlacement(node, usedIds, path, issues) {
366
+ return asSectionPlacement(node, usedIds, path, issues);
367
+ }
368
+ /** Extract reusable blocks from an inline agenda tree. */
369
+ export function extractBlocksFromTree(tree, blocks = {}) {
370
+ visit(tree);
371
+ return blocks;
372
+ function visit(nodes) {
373
+ for (const node of nodes) {
374
+ if (isTopicNode(node) || isActionNode(node) || isTransferNode(node)) {
375
+ blocks[node.id] = toAgendaBlock(node);
376
+ }
377
+ else if (isSectionNode(node) || node.kind === 'branch') {
378
+ visit(node.children);
379
+ }
380
+ else if (node.kind === 'switchOn') {
381
+ for (const children of Object.values(node.cases))
382
+ visit(children);
383
+ if (node.otherwise)
384
+ visit(node.otherwise);
385
+ }
386
+ }
387
+ }
388
+ }
389
+ export function flattenAgendaTree(tree, options = {}) {
390
+ const issues = [];
391
+ const compiled = {};
392
+ const items = flattenAuthoring(tree, { kind: 'always' }, [], compiled, options.functions, issues, new Set(), 'tree');
393
+ return { items, blocks: compiled, issues };
394
+ }
395
+ export function flattenAgendaPlacements(placements, blocks, options = {}) {
396
+ const issues = [];
397
+ const compiled = {};
398
+ const items = flattenPlacements(placements, blocks, { kind: 'always' }, [], compiled, options.functions, issues, new Set(), 'placements');
399
+ return { items, blocks: compiled, issues };
400
+ }
401
+ export function flattenAgendaConfig(config, blocks, options = {}) {
402
+ if (config.tree)
403
+ return flattenAgendaTree(config.tree, options);
404
+ return flattenAgendaPlacements(config.placements ?? [], blocks, options);
405
+ }
406
+ export function treeToPlacements(tree) {
407
+ const issues = [];
408
+ const expanded = expandSugar(tree, issues, 'tree');
409
+ const blocks = extractBlocksFromTree(expanded);
410
+ const usedIds = new Set();
411
+ const placements = expanded.map((node, i) => asPlacement(node, usedIds, `tree[${i}]`, issues));
412
+ return { blocks, placements };
413
+ }
414
+ export function isAgendaTree(value) {
415
+ return (value != null &&
416
+ typeof value === 'object' &&
417
+ value.kind === 'agenda' &&
418
+ typeof value.id === 'string' &&
419
+ Array.isArray(value.tree));
420
+ }
421
+ export function isSequenceAgendas(value) {
422
+ if (value == null || typeof value !== 'object' || Array.isArray(value))
423
+ return false;
424
+ const obj = value;
425
+ return isPlainBlocks(obj.blocks) && isPlainAgendas(obj.agendas);
426
+ }
427
+ function isPlainBlocks(value) {
428
+ return value != null && typeof value === 'object' && !Array.isArray(value);
429
+ }
430
+ function isPlainAgendas(value) {
431
+ return value != null && typeof value === 'object' && !Array.isArray(value);
432
+ }
433
+ export function normalizeSequenceAgendas(value) {
434
+ if (isSequenceAgendas(value))
435
+ return value;
436
+ if (isAgendaTree(value)) {
437
+ const { blocks, placements } = treeToPlacements(value.tree);
438
+ return {
439
+ blocks,
440
+ agendas: {
441
+ [value.id]: { id: value.id, header: value.header, placements },
442
+ },
443
+ };
444
+ }
445
+ if (Array.isArray(value) && value.every(isAgendaTree)) {
446
+ const blocks = {};
447
+ const agendas = {};
448
+ for (const tree of value) {
449
+ const extracted = treeToPlacements(tree.tree);
450
+ Object.assign(blocks, extracted.blocks);
451
+ agendas[tree.id] = { id: tree.id, header: tree.header, placements: extracted.placements };
452
+ }
453
+ return { blocks, agendas };
454
+ }
455
+ if (value != null && typeof value === 'object' && !Array.isArray(value)) {
456
+ const record = value;
457
+ const trees = Object.values(record);
458
+ if (trees.length > 0 && trees.every(isAgendaTree)) {
459
+ return normalizeSequenceAgendas(trees);
460
+ }
461
+ }
462
+ throw new Error('Could not find agendas export as SequenceAgendas or agenda() tree');
463
+ }
464
+ //# sourceMappingURL=flatten.js.map
@@ -0,0 +1,11 @@
1
+ export type { ActionBlock, ActionNode, AgendaBlock, AgendaConfig, AgendaHeader, AgendaTree, ApplyWhen, AuthoringNode, AuthoringWhen, BranchNode, Channel, CompiledActionBlock, CompiledAgenda, CompiledAgendaBlock, CompiledTopicBlock, CompiledTransferBlock, CompiledWhen, Condition, FirstMessage, FinishWhen, FlatActionItem, FlatAgendaItem, FlatNodePlacement, FlatTopicItem, FlatTransferItem, Interrupt, SequenceAgendas, JsonRulesCondition, NodePlacement, OnNoProgress, OtherwiseSentinel, Placement, SectionNode, SectionPlacement, SwitchOnNode, TopicBlock, TopicNode, TransferBlock, TransferNode, When, } from './types.js';
2
+ export { collectJsonRulesFacts, evaluateJsonRules, extractTemplateFields, isOtherwise, jsonRules, looksLikeJsonRules, normalizeJsonRules, otherwise, parseJsonRules, type ConditionEval, } from './condition.js';
3
+ export { andCompiledWhen, compileAuthoringWhen, compileFinishWhen, compileWhen, evaluateFinishWhen, evaluateWhen, finishWhenFields, notAnyWhen, whenFields, type FinishWhenEval, type WhenEval, } from './finish-when.js';
4
+ export { action, agenda, branch, section, switchOn, topic, transfer } from './builders.js';
5
+ export { extractBlocksFromTree, flattenAgendaConfig, flattenAgendaPlacements, flattenAgendaTree, isAgendaTree, isSequenceAgendas, normalizeSequenceAgendas, treeToPlacements, type FlattenOptions, type FlattenResult, } from './flatten.js';
6
+ export { LAST_TRANSFER_RESULT_FIELD, collectTransferBlocks, registerTransferSessionFields, transferFieldKey, transferResultField, } from './transfer-fields.js';
7
+ export { fatalAgendaIssues, validateCompiledAgenda, type ValidateAgendaOptions } from './validate.js';
8
+ export { unresolvedFinishWhenFields, walkAgenda, type WalkDecision, type WalkHitReason, type WalkOptions, type WalkResult, type WalkSkip, type WalkSkipReason, } from './walk.js';
9
+ export { simulateAgenda, type SimulateResult, type SimulatedItem, type SimulatedItemStatus } from './simulate.js';
10
+ export { DEFAULT_MAX_ACTIONS_PER_TURN, advanceAgenda, driveAgenda, transferWrites, watchdogHint, type AdvanceResult, type StageDriveHooks, type StageDriveResult, type StageDriveState, } from './drive.js';
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,10 @@
1
+ export { collectJsonRulesFacts, evaluateJsonRules, extractTemplateFields, isOtherwise, jsonRules, looksLikeJsonRules, normalizeJsonRules, otherwise, parseJsonRules, } from './condition.js';
2
+ export { andCompiledWhen, compileAuthoringWhen, compileFinishWhen, compileWhen, evaluateFinishWhen, evaluateWhen, finishWhenFields, notAnyWhen, whenFields, } from './finish-when.js';
3
+ export { action, agenda, branch, section, switchOn, topic, transfer } from './builders.js';
4
+ export { extractBlocksFromTree, flattenAgendaConfig, flattenAgendaPlacements, flattenAgendaTree, isAgendaTree, isSequenceAgendas, normalizeSequenceAgendas, treeToPlacements, } from './flatten.js';
5
+ export { LAST_TRANSFER_RESULT_FIELD, collectTransferBlocks, registerTransferSessionFields, transferFieldKey, transferResultField, } from './transfer-fields.js';
6
+ export { fatalAgendaIssues, validateCompiledAgenda } from './validate.js';
7
+ export { unresolvedFinishWhenFields, walkAgenda, } from './walk.js';
8
+ export { simulateAgenda } from './simulate.js';
9
+ export { DEFAULT_MAX_ACTIONS_PER_TURN, advanceAgenda, driveAgenda, transferWrites, watchdogHint, } from './drive.js';
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,24 @@
1
+ import type { ComputeHost } from '../../compute-host/compute-host.js';
2
+ import type { RuntimeSlotsSnapshot } from '../slot/types.js';
3
+ import { type WalkResult } from './walk.js';
4
+ import type { FlatAgendaItem } from './types.js';
5
+ export type SimulatedItemStatus = 'folded' | 'done' | 'selected' | 'open';
6
+ export type SimulatedItem = {
7
+ placementId: string;
8
+ blockId: string;
9
+ kind: FlatAgendaItem['kind'];
10
+ groupPath: string[];
11
+ status: SimulatedItemStatus;
12
+ applyWhen: boolean;
13
+ finishWhen: boolean | null;
14
+ };
15
+ export type SimulateResult = {
16
+ selected: FlatAgendaItem | null;
17
+ walk: WalkResult;
18
+ items: SimulatedItem[];
19
+ };
20
+ export declare function simulateAgenda(items: FlatAgendaItem[], snapshot: RuntimeSlotsSnapshot, options?: {
21
+ currentPlacementId?: string | null;
22
+ compute?: ComputeHost;
23
+ }): Promise<SimulateResult>;
24
+ //# sourceMappingURL=simulate.d.ts.map
@@ -0,0 +1,33 @@
1
+ import { evaluateWhen } from './finish-when.js';
2
+ import { walkAgenda } from './walk.js';
3
+ export async function simulateAgenda(items, snapshot, options = {}) {
4
+ const walk = await walkAgenda(items, snapshot, {
5
+ currentPlacementId: options.currentPlacementId,
6
+ compute: options.compute,
7
+ });
8
+ const simulated = [];
9
+ for (const item of items) {
10
+ const applies = (await evaluateWhen(item.applyWhen, snapshot, options.compute)).pass;
11
+ const finished = applies ? (await evaluateWhen(item.finishWhen, snapshot, options.compute)).done : null;
12
+ let status;
13
+ if (!applies)
14
+ status = 'folded';
15
+ else if (finished)
16
+ status = 'done';
17
+ else if (walk.hit?.placementId === item.placementId)
18
+ status = 'selected';
19
+ else
20
+ status = 'open';
21
+ simulated.push({
22
+ placementId: item.placementId,
23
+ blockId: item.id,
24
+ kind: item.kind,
25
+ groupPath: item.groupPath,
26
+ status,
27
+ applyWhen: applies,
28
+ finishWhen: finished,
29
+ });
30
+ }
31
+ return { selected: walk.hit, walk, items: simulated };
32
+ }
33
+ //# sourceMappingURL=simulate.js.map
@@ -0,0 +1,14 @@
1
+ import type { FieldPath } from '../slot/path.js';
2
+ import type { CompiledSlotSchema } from '../slot/types.js';
3
+ import type { AgendaBlock, AuthoringNode, TransferBlock } from './types.js';
4
+ /** Sanitize a transfer node id into a session field fragment. */
5
+ export declare function transferFieldKey(nodeId: string): string;
6
+ export declare function transferResultField(nodeId: string): FieldPath;
7
+ export declare const LAST_TRANSFER_RESULT_FIELD: `${string}.${string}`;
8
+ export declare function collectTransferBlocks(blocks: Record<string, AgendaBlock>, nodes?: AuthoringNode[]): TransferBlock[];
9
+ /**
10
+ * Register per-node `session.transfer_<id>_result` and `session.last_transfer_result`.
11
+ * Existing session fields with the same name are left untouched.
12
+ */
13
+ export declare function registerTransferSessionFields(schema: CompiledSlotSchema, transferIds: Iterable<string>): CompiledSlotSchema;
14
+ //# sourceMappingURL=transfer-fields.d.ts.map
@@ -0,0 +1,88 @@
1
+ import { idField } from '../slot/schema.js';
2
+ import { getCompiledTable } from '../slot/compile-schema.js';
3
+ import { formatFieldPath } from '../slot/path.js';
4
+ const RESULT_ENUM = ['connected', 'failed'];
5
+ function transferFieldCore(label) {
6
+ return {
7
+ derived: false,
8
+ label,
9
+ dataType: 'string',
10
+ source: 'system',
11
+ extraction: 'Platform transfer result',
12
+ enum: [...RESULT_ENUM],
13
+ nullable: true,
14
+ identifier: null,
15
+ ref: null,
16
+ };
17
+ }
18
+ /** Sanitize a transfer node id into a session field fragment. */
19
+ export function transferFieldKey(nodeId) {
20
+ const cleaned = nodeId.replace(/[^A-Za-z0-9_]/g, '_');
21
+ return cleaned.length > 0 ? cleaned : 'node';
22
+ }
23
+ export function transferResultField(nodeId) {
24
+ return formatFieldPath('session', `transfer_${transferFieldKey(nodeId)}_result`);
25
+ }
26
+ export const LAST_TRANSFER_RESULT_FIELD = formatFieldPath('session', 'last_transfer_result');
27
+ export function collectTransferBlocks(blocks, nodes = []) {
28
+ const out = [];
29
+ const seen = new Set();
30
+ for (const block of Object.values(blocks)) {
31
+ if (block.kind === 'transfer' && !seen.has(block.id)) {
32
+ seen.add(block.id);
33
+ out.push(block);
34
+ }
35
+ }
36
+ visit(nodes);
37
+ return out;
38
+ function visit(list) {
39
+ for (const node of list) {
40
+ if (node.kind === 'transfer' && !seen.has(node.id)) {
41
+ seen.add(node.id);
42
+ out.push(node);
43
+ }
44
+ else if (node.kind === 'section' || node.kind === 'branch') {
45
+ visit(node.children);
46
+ }
47
+ else if (node.kind === 'switchOn') {
48
+ for (const children of Object.values(node.cases))
49
+ visit(children);
50
+ if (node.otherwise)
51
+ visit(node.otherwise);
52
+ }
53
+ }
54
+ }
55
+ }
56
+ /**
57
+ * Register per-node `session.transfer_<id>_result` and `session.last_transfer_result`.
58
+ * Existing session fields with the same name are left untouched.
59
+ */
60
+ export function registerTransferSessionFields(schema, transferIds) {
61
+ const ids = [...new Set(transferIds)];
62
+ if (ids.length === 0)
63
+ return schema;
64
+ const session = getCompiledTable(schema, 'session');
65
+ if (!session)
66
+ return schema;
67
+ const fields = { ...session.fields };
68
+ if (!fields.id)
69
+ fields.id = { ...idField, ref: null };
70
+ for (const id of ids) {
71
+ const name = `transfer_${transferFieldKey(id)}_result`;
72
+ if (!fields[name]) {
73
+ fields[name] = transferFieldCore(`Transfer ${id} result`);
74
+ }
75
+ }
76
+ if (!fields.last_transfer_result) {
77
+ fields.last_transfer_result = transferFieldCore('Last transfer result');
78
+ }
79
+ const nextSession = {
80
+ ...session,
81
+ fields,
82
+ };
83
+ return {
84
+ ...schema,
85
+ session: nextSession,
86
+ };
87
+ }
88
+ //# sourceMappingURL=transfer-fields.js.map