@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
@@ -1,319 +0,0 @@
1
- import { getCompiledTable, isSnapshotSingleRow } from './compile-schema.js';
2
- import { commitFieldWrite, makeFieldValue } from './field.js';
3
- import { formatFieldPath, parseFieldPath, requireFieldPath } from './path.js';
4
- import { getRowField, isReservedTableName, isRowIdField, mintRowId, slotFieldNames } from './row.js';
5
- import { cloneSnapshot, getSnapshotTable } from './snapshot.js';
6
- import { validateBareValue, validateExtractedField } from './value.js';
7
- function extractedToFieldValue(extracted, updatedBy, now) {
8
- return makeFieldValue({
9
- reason: extracted.reason,
10
- value: extracted.value,
11
- state: extracted.state,
12
- updatedBy,
13
- updatedAt: now,
14
- });
15
- }
16
- function assertWritableNormal(schema, path, options) {
17
- const { table, field: fieldName } = requireFieldPath(path);
18
- const tableDef = getCompiledTable(schema, table);
19
- const field = tableDef?.fields[fieldName];
20
- if (!tableDef || !field) {
21
- return [{ code: 'unknown-field', message: `Unknown field: ${path}`, path }];
22
- }
23
- if (isRowIdField(fieldName)) {
24
- return [
25
- {
26
- code: 'id-readonly',
27
- message: `Cannot write row id: ${path}`,
28
- path,
29
- },
30
- ];
31
- }
32
- if (field.derived) {
33
- return [
34
- {
35
- code: 'derived-readonly',
36
- message: `Cannot write derived field: ${path}`,
37
- path,
38
- },
39
- ];
40
- }
41
- const systemOnly = options.systemOnly ??
42
- (options.source === 'function' || options.source === 'platform' || options.source === 'import');
43
- if (systemOnly && field.source !== 'system') {
44
- return [
45
- {
46
- code: 'write-forbidden',
47
- message: `Source ${options.source} cannot write conversation field ${path}`,
48
- path,
49
- },
50
- ];
51
- }
52
- if (!isSnapshotSingleRow(tableDef)) {
53
- return [
54
- {
55
- code: 'multi-path-write',
56
- message: `Use Slot Filling / row APIs to write multi-row field ${path}`,
57
- path,
58
- },
59
- ];
60
- }
61
- return [];
62
- }
63
- /**
64
- * Apply bare Writes (function / human / import / platform) onto a snapshot.
65
- * Validates type/enum and write permissions. Detects C5 changes and appends history.
66
- */
67
- export function applyWrites(schema, snapshot, writes, options) {
68
- if (!writes || Object.keys(writes).length === 0) {
69
- return { snapshot, changed: [], history: [] };
70
- }
71
- const now = options.now ?? Date.now();
72
- const next = cloneSnapshot(snapshot);
73
- const changed = [];
74
- const history = [];
75
- const errors = [];
76
- for (const [path, value] of Object.entries(writes)) {
77
- const writeIssues = assertWritableNormal(schema, path, options);
78
- if (writeIssues.length > 0) {
79
- errors.push(...writeIssues);
80
- continue;
81
- }
82
- const parsed = parseFieldPath(path);
83
- const field = getCompiledTable(schema, parsed.table).fields[parsed.field];
84
- const valueIssues = validateBareValue(field, value, path, {
85
- allowNull: !field.derived && field.nullable,
86
- });
87
- if (valueIssues.length > 0) {
88
- errors.push(...valueIssues);
89
- continue;
90
- }
91
- const tableData = getSnapshotTable(next, parsed.table);
92
- const prev = getRowField(tableData, parsed.field);
93
- if (!prev)
94
- continue;
95
- const committed = commitFieldWrite(prev, makeFieldValue({
96
- value: (value ?? null),
97
- state: 'filled',
98
- reason: null,
99
- updatedBy: options.source,
100
- updatedAt: now,
101
- }));
102
- tableData[parsed.field] = committed.field;
103
- if (committed.changed) {
104
- changed.push(parsed.path);
105
- if (committed.historyEntry) {
106
- history.push({ path: parsed.path, entry: committed.historyEntry });
107
- }
108
- }
109
- }
110
- if (errors.length > 0) {
111
- throw new Error(`applyWrites failed:\n${errors.map((e) => `- ${e.path ?? ''}: ${e.message}`).join('\n')}`);
112
- }
113
- return { snapshot: next, changed, history };
114
- }
115
- /**
116
- * Apply one human/admin field edit, including explicit slot state.
117
- * Unlike bare writes, this supports a selected row in a multi-row entity table.
118
- */
119
- export function applyFieldPatch(schema, snapshot, patch, options) {
120
- const parsed = requireFieldPath(patch.path);
121
- const table = getCompiledTable(schema, parsed.table);
122
- const field = table?.fields[parsed.field];
123
- if (!table || !field)
124
- throw new Error(`Unknown field: ${patch.path}`);
125
- if (isRowIdField(parsed.field))
126
- throw new Error(`Cannot write row id: ${patch.path}`);
127
- if (field.derived)
128
- throw new Error(`Cannot write derived field: ${patch.path}`);
129
- const value = patch.state === 'filled' ? patch.value : null;
130
- if (patch.state !== 'empty') {
131
- const issues = validateExtractedField(field, {
132
- value,
133
- state: patch.state,
134
- reason: patch.reason ?? 'Human edit',
135
- }, patch.path);
136
- if (issues.length > 0) {
137
- throw new Error(`applyFieldPatch failed:\n${issues.map((issue) => `- ${issue.path ?? ''}: ${issue.message}`).join('\n')}`);
138
- }
139
- }
140
- const now = options.now ?? Date.now();
141
- const next = cloneSnapshot(snapshot);
142
- const data = getSnapshotTable(next, parsed.table);
143
- const row = isSnapshotSingleRow(table)
144
- ? data
145
- : data?.find((candidate) => candidate.id === patch.rowId);
146
- if (!row)
147
- throw new Error(`Slot row not found: ${parsed.table}:${patch.rowId ?? 'single'}`);
148
- const previous = getRowField(row, parsed.field);
149
- if (!previous)
150
- throw new Error(`Slot field not initialized: ${patch.path}`);
151
- const committed = commitFieldWrite(previous, makeFieldValue({
152
- value,
153
- state: patch.state,
154
- reason: patch.reason ?? 'Human edit',
155
- updatedBy: options.source,
156
- updatedAt: now,
157
- }));
158
- row[parsed.field] = committed.field;
159
- if (!committed.changed)
160
- return { snapshot: next, changed: [], history: [] };
161
- return {
162
- snapshot: next,
163
- changed: [parsed.path],
164
- history: committed.historyEntry ? [{ path: parsed.path, entry: committed.historyEntry }] : [],
165
- };
166
- }
167
- /**
168
- * C4: apply accepted Slot Filling projection (full replace for present fields/rows).
169
- * Multi-row: projection is the full set of rows; omitted existing rows are deleted.
170
- */
171
- export function applySlotFilling(schema, snapshot, output, options = {}) {
172
- const now = options.now ?? Date.now();
173
- const next = cloneSnapshot(snapshot);
174
- const changed = new Set();
175
- const history = [];
176
- const rowKeyMap = options.rowKeyMap ?? new Map();
177
- for (const [tableName, projection] of Object.entries(output)) {
178
- if (isReservedTableName(tableName))
179
- continue;
180
- const table = getCompiledTable(schema, tableName);
181
- if (!table)
182
- continue;
183
- if (isSnapshotSingleRow(table)) {
184
- const tableData = getSnapshotTable(next, tableName);
185
- for (const [fieldName, extracted] of Object.entries(projection)) {
186
- const field = table.fields[fieldName];
187
- if (!field || field.derived || field.source !== 'conversation')
188
- continue;
189
- const path = formatFieldPath(tableName, fieldName);
190
- const issues = validateExtractedField(field, extracted, path);
191
- if (issues.length > 0) {
192
- throw new Error(issues.map((i) => i.message).join('; '));
193
- }
194
- if (isRowIdField(fieldName))
195
- continue;
196
- const prev = getRowField(tableData, fieldName);
197
- if (!prev)
198
- continue;
199
- const committed = commitFieldWrite(prev, extractedToFieldValue(extracted, 'slot-filling', now));
200
- tableData[fieldName] = committed.field;
201
- if (committed.changed) {
202
- changed.add(path);
203
- if (committed.historyEntry) {
204
- history.push({ path, entry: committed.historyEntry });
205
- }
206
- }
207
- }
208
- }
209
- else {
210
- const prevRows = getSnapshotTable(snapshot, tableName) ?? [];
211
- const prevById = new Map(prevRows.map((r) => [r.id, r]));
212
- const projectionRows = projection;
213
- const nextRows = [];
214
- let structuralChange = false;
215
- for (const row of projectionRows) {
216
- let id = row.rowKey;
217
- if (row.rowKey.startsWith('new-')) {
218
- id = rowKeyMap.get(row.rowKey) ?? mintRowId();
219
- rowKeyMap.set(row.rowKey, id);
220
- structuralChange = true;
221
- }
222
- else if (!prevById.has(row.rowKey)) {
223
- structuralChange = true;
224
- }
225
- const prevRow = prevById.get(id);
226
- const nextRow = { id };
227
- for (const fieldName of slotFieldNames(table.fields)) {
228
- const field = table.fields[fieldName];
229
- if (field.derived)
230
- continue;
231
- const extracted = row.fields[fieldName];
232
- const prevFv = prevRow ? getRowField(prevRow, fieldName) : undefined;
233
- if (extracted) {
234
- const path = formatFieldPath(tableName, fieldName);
235
- const issues = validateExtractedField(field, extracted, path);
236
- if (issues.length > 0) {
237
- throw new Error(issues.map((i) => i.message).join('; '));
238
- }
239
- const committed = commitFieldWrite(prevFv, extractedToFieldValue(extracted, 'slot-filling', now));
240
- nextRow[fieldName] = committed.field;
241
- if (committed.changed) {
242
- changed.add(path);
243
- if (committed.historyEntry) {
244
- history.push({ path, entry: committed.historyEntry });
245
- }
246
- }
247
- }
248
- else if (prevFv) {
249
- nextRow[fieldName] = prevFv;
250
- }
251
- else {
252
- nextRow[fieldName] = makeFieldValue({
253
- value: null,
254
- state: 'empty',
255
- updatedBy: 'slot-filling',
256
- updatedAt: now,
257
- });
258
- }
259
- }
260
- nextRows.push(nextRow);
261
- }
262
- const prevIds = new Set(prevRows.map((r) => r.id));
263
- const nextIds = new Set(nextRows.map((r) => r.id));
264
- for (const id of prevIds) {
265
- if (!nextIds.has(id))
266
- structuralChange = true;
267
- }
268
- if (prevRows.length !== nextRows.length)
269
- structuralChange = true;
270
- next[tableName] = nextRows;
271
- if (structuralChange) {
272
- for (const fieldName of slotFieldNames(table.fields)) {
273
- changed.add(formatFieldPath(tableName, fieldName));
274
- }
275
- }
276
- }
277
- }
278
- return { snapshot: next, changed: [...changed], history };
279
- }
280
- /**
281
- * Mark remaining conversation fields as asked_unknown (Topic move-on).
282
- */
283
- export function markAskedUnknown(schema, snapshot, fields, options = {}) {
284
- const now = options.now ?? Date.now();
285
- const next = cloneSnapshot(snapshot);
286
- const changed = [];
287
- const history = [];
288
- for (const path of fields) {
289
- const parsed = requireFieldPath(path);
290
- const table = getCompiledTable(schema, parsed.table);
291
- const field = table?.fields[parsed.field];
292
- if (!table || !field || field.derived)
293
- continue;
294
- if (field.source !== 'conversation')
295
- continue;
296
- if (!isSnapshotSingleRow(table))
297
- continue;
298
- const tableData = getSnapshotTable(next, parsed.table);
299
- const prev = getRowField(tableData, parsed.field);
300
- if (!prev || prev.state !== 'empty')
301
- continue;
302
- const committed = commitFieldWrite(prev, makeFieldValue({
303
- value: null,
304
- state: 'asked_unknown',
305
- reason: options.reason ?? 'move-on',
306
- updatedBy: 'platform',
307
- updatedAt: now,
308
- }));
309
- tableData[parsed.field] = committed.field;
310
- if (committed.changed) {
311
- changed.push(parsed.path);
312
- if (committed.historyEntry) {
313
- history.push({ path: parsed.path, entry: committed.historyEntry });
314
- }
315
- }
316
- }
317
- return { snapshot: next, changed, history };
318
- }
319
- //# sourceMappingURL=write.js.map
@@ -1,36 +0,0 @@
1
- import type { FuncRef } from '../functions/func-ref.js';
2
- import type { FieldPath } from '../slot/path.js';
3
- import type { ActionNode, AgendaHeader, AgendaTree, AuthoringNode, AuthoringWhen, BranchNode, FinishWhen, OnNoProgress, SectionNode, SwitchOnNode, TopicNode, TransferNode } from './types.js';
4
- export declare function topic(id: string, config: {
5
- label: string;
6
- applyWhen: AuthoringWhen;
7
- instructions: string;
8
- fields: FieldPath[] | null;
9
- finishWhen: FinishWhen;
10
- onNoProgress: OnNoProgress | null;
11
- }): TopicNode;
12
- export declare function action(id: string, config: {
13
- label: string;
14
- applyWhen: AuthoringWhen;
15
- finishWhen: FinishWhen;
16
- run: FuncRef;
17
- say: string | null;
18
- }): ActionNode;
19
- export declare function transfer(id: string, config: {
20
- label: string;
21
- applyWhen: AuthoringWhen;
22
- numbers: string[];
23
- mode: 'cold' | 'warm';
24
- say: string;
25
- unsuccessfulSay: string | null;
26
- }): TransferNode;
27
- export declare function section(id: string, config: {
28
- label: string;
29
- applyWhen: AuthoringWhen;
30
- }, children: AuthoringNode[]): SectionNode;
31
- export declare function branch(children: AuthoringNode[]): BranchNode;
32
- export declare function switchOn(field: FieldPath, cases: Record<string, AuthoringNode[]> & {
33
- otherwise?: AuthoringNode[];
34
- }): SwitchOnNode;
35
- export declare function agenda(id: string, header: AgendaHeader | null, tree: AuthoringNode[]): AgendaTree;
36
- //# sourceMappingURL=builders.d.ts.map
@@ -1,28 +0,0 @@
1
- export function topic(id, config) {
2
- return { kind: 'topic', id, ...config };
3
- }
4
- export function action(id, config) {
5
- return { kind: 'action', id, ...config };
6
- }
7
- export function transfer(id, config) {
8
- return { kind: 'transfer', id, ...config };
9
- }
10
- export function section(id, config, children) {
11
- return { kind: 'section', id, label: config.label, applyWhen: config.applyWhen, children };
12
- }
13
- export function branch(children) {
14
- return { kind: 'branch', children };
15
- }
16
- export function switchOn(field, cases) {
17
- const { otherwise: otherwiseChildren, ...rest } = cases;
18
- return {
19
- kind: 'switchOn',
20
- field,
21
- cases: rest,
22
- ...(otherwiseChildren ? { otherwise: otherwiseChildren } : {}),
23
- };
24
- }
25
- export function agenda(id, header, tree) {
26
- return { kind: 'agenda', id, header, tree };
27
- }
28
- //# sourceMappingURL=builders.js.map
@@ -1,17 +0,0 @@
1
- import type { FieldPath } from '../slot/path.js';
2
- import type { Condition, JsonRulesCondition, OtherwiseSentinel } from './types.js';
3
- export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
4
- export declare function isOtherwise(value: unknown): value is OtherwiseSentinel;
5
- export declare function looksLikeJsonRules(value: string): boolean;
6
- export declare function jsonRules(rules: Condition | Condition[] | string): Condition;
7
- export declare function otherwise(): OtherwiseSentinel;
8
- export declare function parseJsonRules(input: string): JsonRulesCondition;
9
- export declare function normalizeJsonRules(raw: unknown): JsonRulesCondition;
10
- export declare function collectJsonRulesFacts(condition: JsonRulesCondition | null | undefined): FieldPath[];
11
- export type ConditionEval = {
12
- pass: boolean;
13
- failedFields: FieldPath[];
14
- };
15
- export declare function evaluateJsonRules(condition: JsonRulesCondition, caseContext: unknown): Promise<ConditionEval>;
16
- export declare function extractTemplateFields(text: string): FieldPath[];
17
- //# sourceMappingURL=condition.d.ts.map
@@ -1,198 +0,0 @@
1
- import { Engine } from 'json-rules-engine';
2
- import { emptyFieldValue } from '../slot/field.js';
3
- import { parseFieldPath } from '../slot/path.js';
4
- import { getRowField } from '../slot/row.js';
5
- const OPERATORS = new Set([
6
- 'equal',
7
- 'notEqual',
8
- 'in',
9
- 'notIn',
10
- 'contains',
11
- 'doesNotContain',
12
- 'lessThan',
13
- 'lessThanInclusive',
14
- 'greaterThan',
15
- 'greaterThanInclusive',
16
- ]);
17
- const PASS_EVENT = 'pass';
18
- export function isPlainObject(value) {
19
- return value != null && typeof value === 'object' && !Array.isArray(value);
20
- }
21
- export function isOtherwise(value) {
22
- return isPlainObject(value) && value.op === 'otherwise' && !('fact' in value) && !('operator' in value);
23
- }
24
- export function looksLikeJsonRules(value) {
25
- const trimmed = value.trim();
26
- return trimmed.startsWith('{') || trimmed.startsWith('[');
27
- }
28
- export function jsonRules(rules) {
29
- if (typeof rules === 'string')
30
- return parseJsonRules(rules);
31
- if (Array.isArray(rules))
32
- return { all: rules };
33
- return rules;
34
- }
35
- export function otherwise() {
36
- return { op: 'otherwise' };
37
- }
38
- export function parseJsonRules(input) {
39
- let parsed;
40
- try {
41
- parsed = JSON.parse(input);
42
- }
43
- catch {
44
- throw new Error('Invalid json-rules JSON');
45
- }
46
- return normalizeJsonRules(parsed);
47
- }
48
- export function normalizeJsonRules(raw) {
49
- if (Array.isArray(raw)) {
50
- if (raw.length === 0)
51
- throw new Error('json-rules array must not be empty');
52
- return { all: raw.map(normalizeJsonRules) };
53
- }
54
- if (!isPlainObject(raw)) {
55
- throw new Error('Invalid json-rules condition');
56
- }
57
- if ('conditions' in raw) {
58
- return normalizeJsonRules(raw.conditions);
59
- }
60
- if ('all' in raw) {
61
- if (!Array.isArray(raw.all))
62
- throw new Error('json-rules `all` must be an array');
63
- return { all: raw.all.map(normalizeJsonRules) };
64
- }
65
- if ('any' in raw) {
66
- if (!Array.isArray(raw.any))
67
- throw new Error('json-rules `any` must be an array');
68
- return { any: raw.any.map(normalizeJsonRules) };
69
- }
70
- if ('not' in raw) {
71
- return { not: normalizeJsonRules(raw.not) };
72
- }
73
- if (typeof raw.fact !== 'string' || typeof raw.operator !== 'string') {
74
- throw new Error('json-rules leaf requires `fact` and `operator`');
75
- }
76
- if (!OPERATORS.has(raw.operator)) {
77
- throw new Error(`Unknown json-rules-engine operator: ${raw.operator}`);
78
- }
79
- const leaf = {
80
- fact: raw.fact,
81
- operator: raw.operator,
82
- value: 'value' in raw ? raw.value : undefined,
83
- };
84
- if (typeof raw.path === 'string')
85
- leaf.path = raw.path;
86
- return leaf;
87
- }
88
- export function collectJsonRulesFacts(condition) {
89
- if (!condition)
90
- return [];
91
- const out = [];
92
- const seen = new Set();
93
- visit(condition);
94
- return out;
95
- function visit(node) {
96
- if ('all' in node) {
97
- for (const child of node.all)
98
- visit(child);
99
- return;
100
- }
101
- if ('any' in node) {
102
- for (const child of node.any)
103
- visit(child);
104
- return;
105
- }
106
- if ('not' in node) {
107
- visit(node.not);
108
- return;
109
- }
110
- if ('fact' in node && node.fact && !seen.has(node.fact)) {
111
- seen.add(node.fact);
112
- out.push(node.fact);
113
- }
114
- }
115
- }
116
- export async function evaluateJsonRules(condition, caseContext) {
117
- const engine = new Engine([], { allowUndefinedFacts: true });
118
- engine.addRule({
119
- conditions: toEngineConditions(condition),
120
- event: { type: PASS_EVENT },
121
- });
122
- const { events, failureResults } = await engine.run(factsFromCaseContext(condition, caseContext));
123
- const pass = events.some((event) => event.type === PASS_EVENT);
124
- return {
125
- pass,
126
- failedFields: pass ? [] : collectFailedFacts(failureResults[0]?.conditions),
127
- };
128
- }
129
- function toEngineConditions(condition) {
130
- const node = applyDefaultPath(condition);
131
- if ('all' in node || 'any' in node || 'not' in node)
132
- return node;
133
- return { all: [node] };
134
- }
135
- function applyDefaultPath(condition) {
136
- if ('all' in condition)
137
- return { ...condition, all: condition.all.map(applyDefaultPath) };
138
- if ('any' in condition)
139
- return { ...condition, any: condition.any.map(applyDefaultPath) };
140
- if ('not' in condition)
141
- return { ...condition, not: applyDefaultPath(condition.not) };
142
- if ('path' in condition && condition.path)
143
- return condition;
144
- return { ...condition, path: '$.value' };
145
- }
146
- function factsFromCaseContext(condition, caseContext) {
147
- const facts = {};
148
- for (const fact of collectJsonRulesFacts(condition)) {
149
- facts[fact] = getContextField(caseContext, fact) ?? emptyFieldValue();
150
- }
151
- return facts;
152
- }
153
- function getContextField(caseContext, path) {
154
- const parsed = parseFieldPath(path);
155
- if (!parsed || !isPlainObject(caseContext))
156
- return undefined;
157
- const row = caseContext[parsed.table];
158
- if (!row || typeof row !== 'object' || Array.isArray(row))
159
- return undefined;
160
- return getRowField(row, parsed.field);
161
- }
162
- function collectFailedFacts(node) {
163
- const out = [];
164
- const seen = new Set();
165
- visit(node);
166
- return out;
167
- function visit(value) {
168
- if (value == null || typeof value !== 'object')
169
- return;
170
- const rec = value;
171
- if (typeof rec.fact === 'string' && rec.result === false && !seen.has(rec.fact)) {
172
- seen.add(rec.fact);
173
- out.push(rec.fact);
174
- }
175
- if (Array.isArray(rec.all))
176
- for (const child of rec.all)
177
- visit(child);
178
- if (Array.isArray(rec.any))
179
- for (const child of rec.any)
180
- visit(child);
181
- if (rec.not)
182
- visit(rec.not);
183
- }
184
- }
185
- const TEMPLATE_RE = /\{\{\s*([A-Za-z_][A-Za-z0-9_]*\.[A-Za-z_][A-Za-z0-9_]*)\s*\}\}/g;
186
- export function extractTemplateFields(text) {
187
- const out = [];
188
- const seen = new Set();
189
- for (const match of text.matchAll(TEMPLATE_RE)) {
190
- const path = match[1];
191
- if (!seen.has(path)) {
192
- seen.add(path);
193
- out.push(path);
194
- }
195
- }
196
- return out;
197
- }
198
- //# sourceMappingURL=condition.js.map
@@ -1,43 +0,0 @@
1
- import type { Writes } from '../context/types.js';
2
- import type { ComputeHost } from '../../compute-host/compute-host.js';
3
- import type { FieldPath } from '../slot/path.js';
4
- import type { RuntimeSlotsSnapshot } from '../slot/types.js';
5
- import { type WalkResult } from './walk.js';
6
- import type { FlatActionItem, FlatAgendaItem, FlatTransferItem } from './types.js';
7
- export declare const DEFAULT_MAX_ACTIONS_PER_TURN = 10;
8
- export type StageDriveHooks = {
9
- runAction: (item: FlatActionItem, snapshot: RuntimeSlotsSnapshot) => Promise<Writes | null>;
10
- applyWrites: (writes: Writes, snapshot: RuntimeSlotsSnapshot) => Promise<RuntimeSlotsSnapshot>;
11
- };
12
- export type StageDriveState = {
13
- snapshot: RuntimeSlotsSnapshot;
14
- currentPlacementId: string | null;
15
- };
16
- export type StageDriveResult = {
17
- snapshot: RuntimeSlotsSnapshot;
18
- currentPlacementId: string | null;
19
- walk: WalkResult;
20
- actionsRun: number;
21
- fused: boolean;
22
- };
23
- /**
24
- * Walk until a topic/transfer/complete, executing actions inline.
25
- * Action returning null is skipped for this tick; writes trigger a full re-walk.
26
- */
27
- export declare function driveAgenda(items: FlatAgendaItem[], state: StageDriveState, compute: ComputeHost, hooks: StageDriveHooks, options?: {
28
- maxActions?: number;
29
- }): Promise<StageDriveResult>;
30
- export type AdvanceResult = {
31
- ok: true;
32
- drive: StageDriveResult;
33
- } | {
34
- ok: false;
35
- missing: FieldPath[];
36
- item: FlatAgendaItem;
37
- };
38
- export declare function advanceAgenda(items: FlatAgendaItem[], state: StageDriveState, compute: ComputeHost, hooks: StageDriveHooks, options?: {
39
- maxActions?: number;
40
- }): Promise<AdvanceResult>;
41
- export declare function watchdogHint(item: FlatAgendaItem | null, snapshot: RuntimeSlotsSnapshot, compute?: ComputeHost): Promise<string | null>;
42
- export declare function transferWrites(item: FlatTransferItem, result: 'connected' | 'failed'): Writes;
43
- //# sourceMappingURL=drive.d.ts.map