@superpanel-sdk/runtime 0.2.2 → 0.5.0

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 (274) hide show
  1. package/README.md +28 -8
  2. package/dist/builder/codegen/emit.d.ts +29 -11
  3. package/dist/builder/codegen/emit.js +328 -61
  4. package/dist/builder/codegen/generate.d.ts +21 -0
  5. package/dist/builder/codegen/generate.js +152 -20
  6. package/dist/builder/company-import-graph.d.ts +2 -0
  7. package/dist/builder/company-import-graph.js +40 -8
  8. package/dist/builder/company-imports.d.ts +24 -0
  9. package/dist/builder/company-imports.js +114 -0
  10. package/dist/builder/compile/compile-sequence.d.ts +59 -8
  11. package/dist/builder/compile/compile-sequence.js +279 -77
  12. package/dist/builder/compile/function-code.d.ts +12 -2
  13. package/dist/builder/compile/function-code.js +87 -39
  14. package/dist/builder/compile/registry.d.ts +7 -3
  15. package/dist/builder/compile/registry.js +24 -23
  16. package/dist/builder/load.d.ts +70 -10
  17. package/dist/builder/load.js +250 -61
  18. package/dist/builder/paths.d.ts +3 -0
  19. package/dist/builder/paths.js +16 -0
  20. package/dist/builder/schedules.d.ts +23 -0
  21. package/dist/builder/schedules.js +102 -0
  22. package/dist/builder/slot-schema-validation.d.ts +4 -0
  23. package/dist/builder/slot-schema-validation.js +10 -0
  24. package/dist/builder/typecheck.d.ts +8 -0
  25. package/dist/builder/typecheck.js +105 -0
  26. package/dist/builder/types.d.ts +91 -19
  27. package/dist/builder/types.js +2 -1
  28. package/dist/cli/auth/auth.d.ts +37 -0
  29. package/dist/cli/auth/auth.js +211 -0
  30. package/dist/cli/auth/backend-url.d.ts +5 -0
  31. package/dist/cli/auth/backend-url.js +25 -0
  32. package/dist/cli/auth/clipboard.d.ts +9 -0
  33. package/dist/cli/auth/clipboard.js +38 -0
  34. package/dist/cli/auth/http.d.ts +20 -0
  35. package/dist/cli/auth/http.js +27 -0
  36. package/dist/cli/auth/index.d.ts +10 -0
  37. package/dist/cli/auth/index.js +27 -0
  38. package/dist/cli/auth/progress.d.ts +12 -0
  39. package/dist/cli/auth/progress.js +15 -0
  40. package/dist/cli/auth/serve-login.d.ts +8 -0
  41. package/dist/cli/auth/serve-login.js +24 -0
  42. package/dist/cli/compile/index.js +3 -0
  43. package/dist/cli/config.d.ts +10 -0
  44. package/dist/cli/config.js +52 -0
  45. package/dist/cli/index.js +28 -8
  46. package/dist/cli/serve/index.d.ts +73 -3
  47. package/dist/cli/serve/index.js +410 -113
  48. package/dist/cli/type-gen/index.js +9 -8
  49. package/dist/compiler.d.ts +7 -0
  50. package/dist/compiler.js +7 -0
  51. package/dist/compute-host/compute-host.d.ts +15 -23
  52. package/dist/compute-host/compute-host.js +25 -28
  53. package/dist/compute-host/index.d.ts +2 -2
  54. package/dist/compute-host/index.js +1 -1
  55. package/dist/compute-host/source-compute-host.d.ts +11 -9
  56. package/dist/compute-host/source-compute-host.js +50 -31
  57. package/dist/core/case-orchestrator/contact-hours.d.ts +50 -0
  58. package/dist/core/case-orchestrator/contact-hours.js +108 -0
  59. package/dist/core/case-orchestrator/follow-ups.d.ts +31 -0
  60. package/dist/core/case-orchestrator/follow-ups.js +103 -0
  61. package/dist/core/case-orchestrator/index.d.ts +6 -0
  62. package/dist/core/case-orchestrator/index.js +6 -0
  63. package/dist/core/case-orchestrator/render.d.ts +75 -0
  64. package/dist/core/case-orchestrator/render.js +239 -0
  65. package/dist/core/case-orchestrator/time.d.ts +11 -0
  66. package/dist/core/case-orchestrator/time.js +29 -0
  67. package/dist/core/case-orchestrator/tools.d.ts +29 -0
  68. package/dist/core/case-orchestrator/tools.js +31 -0
  69. package/dist/core/case-orchestrator/types.d.ts +753 -0
  70. package/dist/core/case-orchestrator/types.js +46 -0
  71. package/dist/core/case-orchestrator/validate.d.ts +11 -0
  72. package/dist/core/case-orchestrator/validate.js +63 -0
  73. package/dist/core/context/call.d.ts +291 -0
  74. package/dist/core/context/call.js +94 -0
  75. package/dist/core/context/channels.d.ts +60 -0
  76. package/dist/core/context/channels.js +35 -0
  77. package/dist/core/context/contact.d.ts +96 -6
  78. package/dist/core/context/contact.js +48 -2
  79. package/dist/core/context/email-message.d.ts +159 -0
  80. package/dist/core/context/email-message.js +59 -0
  81. package/dist/core/context/email.d.ts +97 -0
  82. package/dist/core/context/email.js +91 -0
  83. package/dist/core/context/exchanges.d.ts +142 -0
  84. package/dist/core/context/exchanges.js +63 -0
  85. package/dist/core/context/instances.d.ts +278 -0
  86. package/dist/core/context/instances.js +123 -0
  87. package/dist/core/context/kv.d.ts +113 -0
  88. package/dist/core/context/kv.js +60 -0
  89. package/dist/core/context/platform-registry.d.ts +509 -4
  90. package/dist/core/context/platform-registry.js +14 -2
  91. package/dist/core/context/platform.d.ts +2 -1
  92. package/dist/core/context/sms.d.ts +147 -0
  93. package/dist/core/context/sms.js +57 -0
  94. package/dist/core/context/types.d.ts +51 -30
  95. package/dist/core/context/types.js +1 -7
  96. package/dist/core/conversation/index.d.ts +31 -0
  97. package/dist/core/conversation/index.js +14 -0
  98. package/dist/core/functions/define/bind.d.ts +25 -0
  99. package/dist/core/functions/define/bind.js +14 -0
  100. package/dist/core/functions/define/case.d.ts +9 -0
  101. package/dist/core/functions/define/case.js +13 -0
  102. package/dist/core/functions/define/events.d.ts +20 -0
  103. package/dist/core/functions/define/events.js +21 -0
  104. package/dist/core/functions/define/no-case.d.ts +4 -0
  105. package/dist/core/functions/define/no-case.js +9 -0
  106. package/dist/core/functions/events.d.ts +247 -0
  107. package/dist/core/functions/events.js +32 -0
  108. package/dist/core/functions/func-ref.d.ts +11 -2
  109. package/dist/core/functions/schedules.d.ts +51 -0
  110. package/dist/core/functions/schedules.js +20 -0
  111. package/dist/core/functions/types.d.ts +62 -51
  112. package/dist/core/functions/types.js +6 -1
  113. package/dist/core/office-hours/index.d.ts +39 -0
  114. package/dist/core/office-hours/index.js +187 -0
  115. package/dist/core/page/define.d.ts +834 -0
  116. package/dist/core/page/define.js +477 -0
  117. package/dist/core/page/ops.d.ts +58 -0
  118. package/dist/core/page/ops.js +220 -0
  119. package/dist/core/page/render.d.ts +253 -0
  120. package/dist/core/page/render.js +840 -0
  121. package/dist/core/page/time-format.d.ts +19 -0
  122. package/dist/core/page/time-format.js +41 -0
  123. package/dist/core/page/timeline.d.ts +40 -0
  124. package/dist/core/page/timeline.js +2 -0
  125. package/dist/core/page/typed.d.ts +642 -0
  126. package/dist/core/page/typed.js +2 -0
  127. package/dist/core/page/workspace.d.ts +33 -0
  128. package/dist/core/page/workspace.js +231 -0
  129. package/dist/core/party/index.d.ts +3 -0
  130. package/dist/core/party/index.js +3 -0
  131. package/dist/core/party/schema.d.ts +137 -0
  132. package/dist/core/party/schema.js +80 -0
  133. package/dist/core/party/types.d.ts +40 -0
  134. package/dist/core/party/types.js +15 -0
  135. package/dist/core/sequence/define.d.ts +57 -0
  136. package/dist/core/sequence/define.js +10 -0
  137. package/dist/core/sequence/index.d.ts +63 -0
  138. package/dist/core/sequence/index.js +231 -0
  139. package/dist/core/slot/binding.d.ts +40 -0
  140. package/dist/core/slot/binding.js +2 -0
  141. package/dist/core/slot/company-metadata.d.ts +113 -0
  142. package/dist/core/slot/company-metadata.js +563 -0
  143. package/dist/core/slot/compile-schema.d.ts +19 -13
  144. package/dist/core/slot/compile-schema.js +51 -248
  145. package/dist/core/slot/contact-timezone.d.ts +110 -0
  146. package/dist/core/slot/contact-timezone.js +186 -0
  147. package/dist/core/slot/field.d.ts +2 -24
  148. package/dist/core/slot/field.js +0 -69
  149. package/dist/core/slot/id-ref.d.ts +17 -2
  150. package/dist/core/slot/id-ref.js +16 -8
  151. package/dist/core/slot/index.d.ts +12 -13
  152. package/dist/core/slot/index.js +11 -12
  153. package/dist/core/slot/naming.d.ts +15 -0
  154. package/dist/core/slot/naming.js +96 -0
  155. package/dist/core/slot/platform-fields.d.ts +119 -0
  156. package/dist/core/slot/platform-fields.js +121 -0
  157. package/dist/core/slot/ref.d.ts +16 -0
  158. package/dist/core/slot/ref.js +14 -0
  159. package/dist/core/slot/row.d.ts +2 -9
  160. package/dist/core/slot/row.js +13 -23
  161. package/dist/core/slot/schema-compat.d.ts +16 -0
  162. package/dist/core/slot/schema-compat.js +112 -0
  163. package/dist/core/slot/schema.d.ts +409 -76
  164. package/dist/core/slot/schema.js +178 -10
  165. package/dist/core/slot/types.d.ts +27 -92
  166. package/dist/core/slot/value.d.ts +26 -3
  167. package/dist/core/slot/value.js +89 -57
  168. package/dist/dev-overlay/protocol.d.ts +41 -8
  169. package/dist/fn-context/build-context.d.ts +3 -1
  170. package/dist/fn-context/build-context.js +20 -10
  171. package/dist/fn-context/fake/empty-api.js +9 -28
  172. package/dist/fn-context/fake/index.d.ts +11 -4
  173. package/dist/fn-context/fake/index.js +27 -2
  174. package/dist/fn-context/fn-params.d.ts +6 -0
  175. package/dist/fn-context/fn-params.js +13 -0
  176. package/dist/fn-context/index.d.ts +5 -5
  177. package/dist/fn-context/index.js +4 -4
  178. package/dist/fn-context/log.js +2 -2
  179. package/dist/fn-context/make-ctx.d.ts +11 -0
  180. package/dist/fn-context/make-ctx.js +18 -8
  181. package/dist/fn-context/rpc/case-orchestrator-tools.d.ts +24 -0
  182. package/dist/fn-context/rpc/case-orchestrator-tools.js +13 -0
  183. package/dist/fn-context/rpc/index.d.ts +3 -1
  184. package/dist/fn-context/rpc/index.js +3 -1
  185. package/dist/fn-context/rpc/integration/index.d.ts +4 -4
  186. package/dist/fn-context/rpc/integration/index.js +1 -1
  187. package/dist/fn-context/rpc/integration/list-surfaces.d.ts +21 -0
  188. package/dist/fn-context/rpc/integration/{list-bindings.js → list-surfaces.js} +4 -4
  189. package/dist/fn-context/rpc/platform.d.ts +2 -2
  190. package/dist/fn-context/rpc/platform.js +1 -1
  191. package/dist/fn-context/rpc/records.d.ts +78 -0
  192. package/dist/fn-context/rpc/records.js +26 -0
  193. package/dist/fn-context/rpc/trpc-client.d.ts +2 -2
  194. package/dist/fn-context/rpc/trpc-client.js +89 -5
  195. package/dist/fn-context/rpc/trpc-init.d.ts +2 -2
  196. package/dist/fn-context/rpc/trpc-router-factory.d.ts +49 -3
  197. package/dist/fn-context/rpc/trpc-router-factory.js +2 -0
  198. package/dist/fn-context/run-scope.d.ts +4 -0
  199. package/dist/fn-context/run-scope.js +9 -0
  200. package/dist/fn-context/types.d.ts +36 -3
  201. package/dist/index.d.ts +37 -46
  202. package/dist/index.js +33 -33
  203. package/dist/logger.d.ts +7 -0
  204. package/dist/logger.js +41 -0
  205. package/dist/platform.d.ts +76 -0
  206. package/dist/platform.js +56 -0
  207. package/dist/voice-service/actions.d.ts +71 -4
  208. package/dist/voice-service/types.d.ts +42 -2
  209. package/dist/worker.d.ts +10 -0
  210. package/dist/worker.js +10 -0
  211. package/package.json +38 -19
  212. package/dist/builder/compile/compile-agenda.d.ts +0 -15
  213. package/dist/builder/compile/compile-agenda.js +0 -59
  214. package/dist/builder/merge.d.ts +0 -4
  215. package/dist/builder/merge.js +0 -19
  216. package/dist/core/context/db.d.ts +0 -69
  217. package/dist/core/context/db.js +0 -44
  218. package/dist/core/context/docusign.d.ts +0 -134
  219. package/dist/core/context/docusign.js +0 -108
  220. package/dist/core/context/filevine.d.ts +0 -323
  221. package/dist/core/context/filevine.js +0 -254
  222. package/dist/core/context/http.d.ts +0 -9
  223. package/dist/core/context/http.js +0 -4
  224. package/dist/core/context/hubspot.d.ts +0 -250
  225. package/dist/core/context/hubspot.js +0 -191
  226. package/dist/core/context/twilio.d.ts +0 -50
  227. package/dist/core/context/twilio.js +0 -42
  228. package/dist/core/functions/define/case-context.d.ts +0 -14
  229. package/dist/core/functions/define/case-context.js +0 -15
  230. package/dist/core/functions/define/compute.d.ts +0 -7
  231. package/dist/core/functions/define/compute.js +0 -17
  232. package/dist/core/functions/define/custom.d.ts +0 -4
  233. package/dist/core/functions/define/custom.js +0 -8
  234. package/dist/core/functions/define/system.d.ts +0 -7
  235. package/dist/core/functions/define/system.js +0 -12
  236. package/dist/core/slot/deps.d.ts +0 -15
  237. package/dist/core/slot/deps.js +0 -88
  238. package/dist/core/slot/derived.d.ts +0 -32
  239. package/dist/core/slot/derived.js +0 -115
  240. package/dist/core/slot/platform-session-fields.d.ts +0 -38
  241. package/dist/core/slot/platform-session-fields.js +0 -48
  242. package/dist/core/slot/sf-schema.d.ts +0 -23
  243. package/dist/core/slot/sf-schema.js +0 -125
  244. package/dist/core/slot/sf-validate.d.ts +0 -14
  245. package/dist/core/slot/sf-validate.js +0 -132
  246. package/dist/core/slot/snapshot.d.ts +0 -18
  247. package/dist/core/slot/snapshot.js +0 -138
  248. package/dist/core/slot/write.d.ts +0 -41
  249. package/dist/core/slot/write.js +0 -319
  250. package/dist/core/stage/builders.d.ts +0 -36
  251. package/dist/core/stage/builders.js +0 -28
  252. package/dist/core/stage/condition.d.ts +0 -17
  253. package/dist/core/stage/condition.js +0 -198
  254. package/dist/core/stage/drive.d.ts +0 -43
  255. package/dist/core/stage/drive.js +0 -77
  256. package/dist/core/stage/finish-when.d.ts +0 -20
  257. package/dist/core/stage/finish-when.js +0 -82
  258. package/dist/core/stage/flatten.d.ts +0 -27
  259. package/dist/core/stage/flatten.js +0 -464
  260. package/dist/core/stage/index.d.ts +0 -11
  261. package/dist/core/stage/index.js +0 -10
  262. package/dist/core/stage/simulate.d.ts +0 -24
  263. package/dist/core/stage/simulate.js +0 -33
  264. package/dist/core/stage/transfer-fields.d.ts +0 -14
  265. package/dist/core/stage/transfer-fields.js +0 -88
  266. package/dist/core/stage/types.d.ts +0 -177
  267. package/dist/core/stage/types.js +0 -2
  268. package/dist/core/stage/validate.d.ts +0 -11
  269. package/dist/core/stage/validate.js +0 -226
  270. package/dist/core/stage/walk.d.ts +0 -34
  271. package/dist/core/stage/walk.js +0 -96
  272. package/dist/fn-context/rpc/integration/list-bindings.d.ts +0 -21
  273. package/dist/fn-context/system-args.d.ts +0 -6
  274. package/dist/fn-context/system-args.js +0 -14
@@ -0,0 +1,231 @@
1
+ /**
2
+ * Sequence-level declarations (14-sequence-instance §4.4–§4.5): the business root a sequence runs
3
+ * about (`index.ts`: `defineSequence({ subject })`) and its run variables and experiments
4
+ * (`variables.ts`). Compiled into `SequenceBuild.subject` / `.variables` / `.experiments`; the
5
+ * platform validates writes and assigns experiments with the helpers here.
6
+ */
7
+ import { POLICY_IDS } from '../slot/company-metadata.js';
8
+ export { defineSequence, defineVariables, json, } from './define.js';
9
+ /**
10
+ * Run variable names the platform reserves (14 §4.4). It reads `voice` when it opens a session;
11
+ * `responsePolicy` and `slotFillingPolicy` are reserved and not read yet. A sequence may leave them
12
+ * out (the platform default) or declare them as enums whose values the platform knows.
13
+ */
14
+ export const SYSTEM_VARIABLES = ['responsePolicy', 'slotFillingPolicy', 'voice'];
15
+ export function isSystemVariable(name) {
16
+ return SYSTEM_VARIABLES.includes(name);
17
+ }
18
+ const VARIABLE_NAME = /^[a-zA-Z][a-zA-Z0-9_]{0,63}$/;
19
+ /** The values a system variable may take: the policy catalog. */
20
+ function systemVariableValues(name) {
21
+ switch (name) {
22
+ case 'responsePolicy':
23
+ return POLICY_IDS.response;
24
+ case 'slotFillingPolicy':
25
+ return POLICY_IDS.slotFilling;
26
+ case 'voice':
27
+ return POLICY_IDS.voice;
28
+ }
29
+ }
30
+ /** Normalize and check a sequence's `variables.ts` (14 §4.4). */
31
+ export function compileVariables(declared, experiments) {
32
+ const issues = [];
33
+ const variables = {};
34
+ const decls = declared && typeof declared === 'object' && !Array.isArray(declared) ? declared : {};
35
+ if (declared !== undefined && decls !== declared)
36
+ issues.push('variables must be an object of declarations');
37
+ for (const [name, raw] of Object.entries(decls)) {
38
+ if (!VARIABLE_NAME.test(name)) {
39
+ issues.push(`variable '${name}': a name is a letter then letters, digits or _ (at most 64)`);
40
+ continue;
41
+ }
42
+ const compiled = compileVariable(name, raw, issues);
43
+ if (!compiled)
44
+ continue;
45
+ if (isSystemVariable(name)) {
46
+ const allowed = systemVariableValues(name);
47
+ if (compiled.type !== 'enum') {
48
+ issues.push(`variable '${name}' is the platform's: declare it with options (one of ${allowed.join(', ') || 'none'})`);
49
+ continue;
50
+ }
51
+ const unknown = compiled.options.filter((value) => typeof value !== 'string' || !allowed.includes(value));
52
+ if (unknown.length > 0) {
53
+ issues.push(`variable '${name}': ${unknown.join(', ')} not one of ${allowed.join(', ') || '(none)'}`);
54
+ continue;
55
+ }
56
+ }
57
+ variables[name] = compiled;
58
+ }
59
+ const compiledExperiments = {};
60
+ const exps = experiments && typeof experiments === 'object' && !Array.isArray(experiments)
61
+ ? experiments
62
+ : {};
63
+ for (const [name, raw] of Object.entries(exps)) {
64
+ const exp = raw;
65
+ const variable = exp && typeof exp.variable === 'string' ? variables[exp.variable] : undefined;
66
+ if (!exp || typeof exp.variable !== 'string' || !variable) {
67
+ issues.push(`experiment '${name}': variable must name a declared variable`);
68
+ continue;
69
+ }
70
+ if (variable.type !== 'enum') {
71
+ issues.push(`experiment '${name}': only an enum variable (one declared with options) can be split`);
72
+ continue;
73
+ }
74
+ const split = exp.split && typeof exp.split === 'object' ? exp.split : {};
75
+ const entries = Object.entries(split);
76
+ const options = variable.options.map(String);
77
+ const bad = entries.filter(([value, share]) => !options.includes(value) || typeof share !== 'number' || share < 0);
78
+ if (entries.length === 0 || bad.length > 0 || entries.every(([, share]) => share === 0)) {
79
+ issues.push(`experiment '${name}': split gives non-negative shares to values of '${exp.variable}' (${options.join(', ')})`);
80
+ continue;
81
+ }
82
+ if (Object.values(compiledExperiments).some((other) => other.variable === exp.variable)) {
83
+ issues.push(`experiment '${name}': '${exp.variable}' is already split by another experiment`);
84
+ continue;
85
+ }
86
+ compiledExperiments[name] = { variable: exp.variable, split: Object.fromEntries(entries) };
87
+ }
88
+ return { variables, experiments: compiledExperiments, issues };
89
+ }
90
+ function compileVariable(name, raw, issues) {
91
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
92
+ issues.push(`variable '${name}': a declaration is an object ({ options } or { type })`);
93
+ return null;
94
+ }
95
+ const decl = raw;
96
+ const hasDefault = Object.hasOwn(decl, 'default') && decl.default !== undefined;
97
+ if (Array.isArray(decl.options)) {
98
+ const options = decl.options;
99
+ if (options.length === 0 || options.some((value) => !['string', 'number', 'boolean'].includes(typeof value))) {
100
+ issues.push(`variable '${name}': options are a non-empty list of strings, numbers or booleans`);
101
+ return null;
102
+ }
103
+ if (hasDefault && decl.default !== null && !options.includes(decl.default)) {
104
+ issues.push(`variable '${name}': default ${String(decl.default)} is not one of its options`);
105
+ return null;
106
+ }
107
+ return {
108
+ type: 'enum',
109
+ options: options,
110
+ default: hasDefault ? decl.default : null,
111
+ };
112
+ }
113
+ const type = decl.type;
114
+ if (type === 'string' || type === 'number' || type === 'boolean') {
115
+ if (hasDefault && decl.default !== null && typeof decl.default !== type) {
116
+ issues.push(`variable '${name}': default must be a ${type}`);
117
+ return null;
118
+ }
119
+ return { type, default: hasDefault ? decl.default : null };
120
+ }
121
+ if (type && typeof type === 'object' && type.kind === 'json') {
122
+ if (hasDefault && !serializes(decl.default)) {
123
+ issues.push(`variable '${name}': default must serialize as JSON`);
124
+ return null;
125
+ }
126
+ return { type: 'json', default: hasDefault ? decl.default : null };
127
+ }
128
+ issues.push(`variable '${name}': declare options, or type 'string' | 'number' | 'boolean' | json()`);
129
+ return null;
130
+ }
131
+ function serializes(value) {
132
+ try {
133
+ return JSON.stringify(value) !== undefined;
134
+ }
135
+ catch {
136
+ return false;
137
+ }
138
+ }
139
+ /** Why `value` cannot be stored in the variable; null when it can. `null` always can (unset). */
140
+ export function variableValueIssue(name, variable, value) {
141
+ if (!variable)
142
+ return `unknown variable '${name}' (declare it in variables.ts)`;
143
+ if (value === null)
144
+ return null;
145
+ switch (variable.type) {
146
+ case 'enum':
147
+ return variable.options.includes(value) ? null : `'${name}' must be one of ${variable.options.join(', ')}`;
148
+ case 'string':
149
+ case 'number':
150
+ case 'boolean':
151
+ return typeof value === variable.type ? null : `'${name}' must be a ${variable.type}`;
152
+ case 'json':
153
+ return value !== undefined && serializes(value) ? null : `'${name}' must serialize as JSON`;
154
+ }
155
+ }
156
+ /** Issues of a whole write of variables (unknown names, wrong types); empty when it may be stored. */
157
+ export function variablesWriteIssues(variables, values) {
158
+ return Object.entries(values)
159
+ .map(([name, value]) => variableValueIssue(name, variables[name], value))
160
+ .filter((issue) => issue !== null);
161
+ }
162
+ /**
163
+ * The variables a new run starts with: every declared default, then the values given, then each
164
+ * experiment whose variable was not given assigns a value by `hash(instanceId + experiment)`, so the
165
+ * same instance always lands in the same group (14 §4.4). Returns the assigned groups too.
166
+ */
167
+ export function startVariables(instanceId, declared, experiments, given) {
168
+ const variables = {};
169
+ for (const [name, variable] of Object.entries(declared)) {
170
+ if (variable.default !== null && variable.default !== undefined)
171
+ variables[name] = variable.default;
172
+ }
173
+ Object.assign(variables, given);
174
+ const assigned = {};
175
+ for (const [name, experiment] of Object.entries(experiments)) {
176
+ if (Object.hasOwn(given, experiment.variable))
177
+ continue;
178
+ const value = pickByShare(`${instanceId}:${name}`, experiment.split);
179
+ const variable = declared[experiment.variable];
180
+ // A split value is a string key; the variable's option may be a number or boolean.
181
+ const option = variable?.type === 'enum' ? (variable.options.find((candidate) => String(candidate) === value) ?? value) : value;
182
+ variables[experiment.variable] = option;
183
+ assigned[name] = value;
184
+ }
185
+ return { variables, experiments: assigned };
186
+ }
187
+ /**
188
+ * The current value of each variable as a run reads it (14 §4.4 "sequence 版本变化"): a stored value
189
+ * the declaration no longer allows (a removed variable or option) falls back to the default.
190
+ * `onStale` hears each fallback. Stored values are not changed.
191
+ */
192
+ export function readVariables(declared, stored, onStale) {
193
+ const out = {};
194
+ for (const [name, variable] of Object.entries(declared)) {
195
+ const has = Object.hasOwn(stored, name);
196
+ const value = has ? stored[name] : variable.default;
197
+ if (has && variableValueIssue(name, variable, value) !== null) {
198
+ onStale?.(name, value);
199
+ out[name] = variable.default ?? null;
200
+ continue;
201
+ }
202
+ out[name] = value ?? null;
203
+ }
204
+ for (const [name, value] of Object.entries(stored)) {
205
+ if (!Object.hasOwn(declared, name))
206
+ onStale?.(name, value);
207
+ }
208
+ return out;
209
+ }
210
+ /** Deterministic weighted pick: FNV-1a of `key` into the cumulative shares. */
211
+ function pickByShare(key, split) {
212
+ const entries = Object.entries(split).filter(([, share]) => share > 0);
213
+ const total = entries.reduce((sum, [, share]) => sum + share, 0);
214
+ let hash = 0x811c9dc5;
215
+ for (let i = 0; i < key.length; i += 1) {
216
+ hash ^= key.charCodeAt(i);
217
+ hash = Math.imul(hash, 0x01000193) >>> 0;
218
+ }
219
+ let point = (hash / 0x100000000) * total;
220
+ for (const [value, share] of entries) {
221
+ if (point < share)
222
+ return value;
223
+ point -= share;
224
+ }
225
+ return entries[entries.length - 1][0];
226
+ }
227
+ /** Bytes of `variables` serialized: what {@link LIMITS.variablesBytes} bounds. */
228
+ export function variablesSize(variables) {
229
+ return new TextEncoder().encode(JSON.stringify(variables)).length;
230
+ }
231
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,40 @@
1
+ import type { FieldState } from '../context/types.js';
2
+ /**
3
+ * Where a slot cell is stored, as the generated `Rows` type marks it: a cell's
4
+ * `(table, row id, field)` and a row's `(table, id)`.
5
+ */
6
+ /** Where a cell is stored: `(table, row id, field)`. */
7
+ export type CellRef<T extends string = string, F extends string = string> = {
8
+ table: T;
9
+ rowId: string;
10
+ field: F;
11
+ };
12
+ /** A row's binding: `(table, row id)`. */
13
+ export type RowRef<T extends string = string> = {
14
+ table: T;
15
+ id: string;
16
+ };
17
+ declare const cellMode: unique symbol;
18
+ declare const cellKind: unique symbol;
19
+ /**
20
+ * The binding part of a cell type: `ref`, and phantom marks codegen adds to every field of
21
+ * `Rows`. Mode `'rw'`: a page may open it with `field(...)`; `'ro'`: the row id
22
+ * or the parent, which no page opens (19-page-writable-cells). Kind `'ref'`: a ref field (another row's id), which a region never lists.
23
+ */
24
+ export type CellBinding<T extends string = string, F extends string = string, M extends 'rw' | 'ro' = 'rw' | 'ro', K extends 'value' | 'ref' = 'value' | 'ref'> = {
25
+ ref: CellRef<T, F>;
26
+ readonly [cellMode]?: M;
27
+ readonly [cellKind]?: K;
28
+ };
29
+ /** A slot cell as a view receives it: the value and state, and where it is stored. */
30
+ export type Cell<T extends string = string, F extends string = string, V = unknown, M extends 'rw' | 'ro' = 'rw' | 'ro'> = {
31
+ value: V;
32
+ state: FieldState;
33
+ } & CellBinding<T, F, M>;
34
+ /** A bound row as a view receives it. */
35
+ export type BoundRow = {
36
+ id: string;
37
+ $ref: RowRef<string>;
38
+ };
39
+ export {};
40
+ //# sourceMappingURL=binding.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=binding.js.map
@@ -0,0 +1,113 @@
1
+ import type { CompanyFieldDef, CompanySchema, CompiledCompanyMetadata, SessionSchema, TableRef } from './schema.js';
2
+ import type { ValidationIssue, ValidationResult } from './types.js';
3
+ export { PLATFORM_CONTACT_FIELDS, PLATFORM_SESSION_FIELDS } from './platform-fields.js';
4
+ /** The one table every company has: the only thing the platform contacts (14-sequence-instance §5). */
5
+ export declare const CONTACT_TABLE = "contact";
6
+ /** The conversation's table: its fields are the company's `schema/session.ts`; no company table has this name. */
7
+ export declare const SESSION_TABLE = "session";
8
+ export declare const CONTACT_OPT_OUT_FIELDS: readonly ['opted_out', 'call_opted_out', 'sms_opted_out', 'email_opted_out'];
9
+ /** Platform limits (14-sequence-instance §16.1). */
10
+ export declare const LIMITS: {
11
+ /** Longest parent chain: a table's parent's parent … at most this many hops to a table without one. */
12
+ readonly parentDepth: 5;
13
+ /** Rows one load step (one table) may read. */
14
+ readonly loadRowsPerStep: 500;
15
+ /** `follow` hops chained in one load expression. */
16
+ readonly followDepth: 3;
17
+ /** A load's time budget, ms. */
18
+ readonly loadTimeoutMs: 2000;
19
+ /** An instance's `variables`, serialized, bytes. */
20
+ readonly variablesBytes: number;
21
+ /** Company tables per company and environment (18-record-search-index §4.7). */
22
+ readonly tables: 100;
23
+ /** Fields per table, platform fields included. */
24
+ readonly fieldsPerTable: 300;
25
+ /** `options` of one `select` / `multi_select` field (a longer list is a table and a lookupRef). */
26
+ readonly options: 200;
27
+ /** Characters of one option: a short code, not a sentence (its meaning goes in `extraction`). */
28
+ readonly optionChars: 60;
29
+ /** A `string` value, characters. */
30
+ readonly stringChars: 255;
31
+ /** A `text` value, UTF-8 bytes. */
32
+ readonly textBytes: number;
33
+ /** One row's stored data, serialized, UTF-8 bytes. */
34
+ readonly rowDataBytes: number;
35
+ };
36
+ /**
37
+ * The policy ids a run can name, per layer: the values of the platform's run variables
38
+ * `responsePolicy`, `slotFillingPolicy` and `voice` (14 §4.4). Customers write them, so an id is only
39
+ * ever added; removing or renaming one breaks the companies that use it.
40
+ */
41
+ export declare const POLICY_IDS: {
42
+ readonly response: readonly ['default'];
43
+ readonly slotFilling: readonly ['default'];
44
+ readonly voice: readonly ['default'];
45
+ };
46
+ export type ResponsePolicyId = (typeof POLICY_IDS.response)[number];
47
+ export type SlotFillingPolicyId = (typeof POLICY_IDS.slotFilling)[number];
48
+ export type VoiceProfileId = (typeof POLICY_IDS.voice)[number];
49
+ /** Session fields with the platform's only: what a company without fields of its own declares. */
50
+ export declare const PLATFORM_SESSION_SCHEMA: SessionSchema;
51
+ /** Each table's platform fields, with the name its issue codes use (`missing-platform-<name>-field`). */
52
+ export declare const PLATFORM_TABLE_FIELDS: Readonly<Record<string, {
53
+ name: string;
54
+ fields: Record<string, CompanyFieldDef>;
55
+ }>>;
56
+ /**
57
+ * A company table other than `contact`: one the company defines whole. Every company table (contact
58
+ * included) is stored as `Record` rows; `session` is the sequence's, stored on the session.
59
+ */
60
+ export declare function isCustomTable(tableName: string): boolean;
61
+ /** A table whose rows are `Record` rows: every company table, contact included. */
62
+ export declare function isRecordTable(tableName: string): boolean;
63
+ type FieldsOf = {
64
+ fields: Record<string, {
65
+ ref?: TableRef | null;
66
+ } | CompanyFieldDef>;
67
+ };
68
+ /** A table's parent: the field name and the table it points at; null when it has none. */
69
+ export declare function parentOf(table: FieldsOf | undefined): {
70
+ field: string;
71
+ table: string;
72
+ } | null;
73
+ /**
74
+ * Whether `fieldName` is the table's parent. Its value is the row's owner: filled by the platform or
75
+ * given once on insert, never written after, and stored as the row's parent edge in `RecordRef`, not
76
+ * in its data.
77
+ */
78
+ export declare function isParentField(table: FieldsOf | undefined, fieldName: string): boolean;
79
+ export type CompileCompanyMetadataResult = {
80
+ metadata: CompiledCompanyMetadata | null;
81
+ validation: ValidationResult;
82
+ };
83
+ /**
84
+ * The company schema as authored, without the reserved names (as `compileSlotSchema` leaves them
85
+ * out). It is plain data.
86
+ */
87
+ export declare function compileCompanySchema(company: CompanySchema): CompanySchema;
88
+ /**
89
+ * Why `schema/session.ts` is not a session schema (11-remove-sequence-schema §3.1): its fields are
90
+ * simple values, with no `ref` (the session is related to no other table).
91
+ */
92
+ export declare function sessionSchemaIssues(session: SessionSchema): ValidationIssue[];
93
+ /**
94
+ * The company's metadata: its tables (`schema/index.ts`) and its session fields (`schema/session.ts`,
95
+ * none when the file is missing). `session` is reserved: no company table has the name.
96
+ */
97
+ export declare function compileCompanyMetadata(company: CompanySchema, session?: SessionSchema): CompileCompanyMetadataResult;
98
+ /** The issue for a field that still names its `source` (19-page-writable-cells). */
99
+ export declare function sourceRemovedIssue(path: string, prefix?: string): ValidationIssue;
100
+ /** The issue for a field that still names `nullable`, which no write checked. */
101
+ export declare function nullableRemovedIssue(path: string, prefix?: string): ValidationIssue;
102
+ /** The issue for a field that still names `identifier`, which nothing used. */
103
+ export declare function identifierRemovedIssue(path: string, prefix?: string): ValidationIssue;
104
+ /**
105
+ * A field's choices (27 §2–§3): no `enum` (now a `select`); `options` on a `select` / `multi_select`
106
+ * only, and there required: 1 to `LIMITS.options` distinct short codes.
107
+ */
108
+ export declare function optionIssues(path: string, field: Record<string, unknown>, prefix?: string): ValidationIssue[];
109
+ /** The issue for a table that writes the row id, which is the platform's. */
110
+ export declare function idIsPlatformIssue(path: string): ValidationIssue;
111
+ /** The company table a sequence names as its business root, checked (14 §4.5). */
112
+ export declare function subjectTableIssues(company: CompanySchema, subject: unknown): ValidationIssue[];
113
+ //# sourceMappingURL=company-metadata.d.ts.map