@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,97 @@
1
+ const DATE_RE = /^\d{4}(-\d{2}(-\d{2})?)?$/;
2
+ // ISO-8601 datetime with timezone offset or Z
3
+ const DATETIME_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/;
4
+ export function isValidDataTypeValue(dataType, value) {
5
+ if (value === null)
6
+ return true;
7
+ switch (dataType) {
8
+ case 'string':
9
+ return typeof value === 'string';
10
+ case 'number':
11
+ return typeof value === 'number' && Number.isFinite(value);
12
+ case 'boolean':
13
+ return typeof value === 'boolean';
14
+ case 'date':
15
+ return typeof value === 'string' && DATE_RE.test(value);
16
+ case 'datetime':
17
+ return typeof value === 'string' && DATETIME_RE.test(value);
18
+ }
19
+ }
20
+ export function validateExtractedField(field, extracted, path) {
21
+ const issues = [];
22
+ if (typeof extracted.reason !== 'string' || extracted.reason.length === 0) {
23
+ issues.push({
24
+ code: 'missing-reason',
25
+ message: 'Extracted field requires a non-empty reason',
26
+ path,
27
+ });
28
+ }
29
+ if (extracted.state !== 'filled' && extracted.state !== 'asked_unknown' && extracted.state !== 'declined') {
30
+ issues.push({
31
+ code: 'invalid-state',
32
+ message: `Invalid extracted state: ${String(extracted.state)}`,
33
+ path,
34
+ });
35
+ return issues;
36
+ }
37
+ if (extracted.state === 'declined' && extracted.value !== null) {
38
+ issues.push({
39
+ code: 'declined-non-null',
40
+ message: 'declined fields must have value null',
41
+ path,
42
+ });
43
+ }
44
+ if (extracted.state === 'filled' && extracted.value === null && !field.nullable) {
45
+ issues.push({
46
+ code: 'filled-null',
47
+ message: 'filled requires non-null value unless nullable: true',
48
+ path,
49
+ });
50
+ }
51
+ if (extracted.value !== null && !isValidDataTypeValue(field.dataType, extracted.value)) {
52
+ issues.push({
53
+ code: 'type-mismatch',
54
+ message: `value does not match dataType ${field.dataType}`,
55
+ path,
56
+ });
57
+ }
58
+ if (extracted.value !== null && field.enum && !field.enum.includes(extracted.value)) {
59
+ issues.push({
60
+ code: 'enum-mismatch',
61
+ message: `value ${String(extracted.value)} not in enum`,
62
+ path,
63
+ });
64
+ }
65
+ return issues;
66
+ }
67
+ /** Validate a bare write value for a normal or derived field. */
68
+ export function validateBareValue(field, value, path, options = {}) {
69
+ const issues = [];
70
+ const allowNull = options.allowNull ?? true;
71
+ if (value === null || value === undefined) {
72
+ if (!allowNull && !(field.derived === false && field.nullable)) {
73
+ issues.push({
74
+ code: 'null-not-allowed',
75
+ message: 'null is not allowed for this field',
76
+ path,
77
+ });
78
+ }
79
+ return issues;
80
+ }
81
+ if (!isValidDataTypeValue(field.dataType, value)) {
82
+ issues.push({
83
+ code: 'type-mismatch',
84
+ message: `value does not match dataType ${field.dataType}`,
85
+ path,
86
+ });
87
+ }
88
+ if (!field.derived && field.enum && !field.enum.includes(value)) {
89
+ issues.push({
90
+ code: 'enum-mismatch',
91
+ message: `value ${String(value)} not in enum`,
92
+ path,
93
+ });
94
+ }
95
+ return issues;
96
+ }
97
+ //# sourceMappingURL=value.js.map
@@ -0,0 +1,41 @@
1
+ import type { FieldState, Writes } from '../context/types.js';
2
+ import type { FieldPath } from './path.js';
3
+ import type { ApplyWritesOptions, ApplyWritesResult, CompiledSlotSchema, RuntimeSlotsSnapshot, SlotFillingOutput, UpdatedBy } from './types.js';
4
+ /**
5
+ * Apply bare Writes (function / human / import / platform) onto a snapshot.
6
+ * Validates type/enum and write permissions. Detects C5 changes and appends history.
7
+ */
8
+ export declare function applyWrites(schema: CompiledSlotSchema, snapshot: RuntimeSlotsSnapshot, writes: Writes | null | undefined, options: ApplyWritesOptions): ApplyWritesResult;
9
+ export type FieldPatchInput = {
10
+ path: FieldPath;
11
+ rowId?: string;
12
+ value: string | number | boolean | null;
13
+ state: FieldState;
14
+ reason?: string | null;
15
+ };
16
+ /**
17
+ * Apply one human/admin field edit, including explicit slot state.
18
+ * Unlike bare writes, this supports a selected row in a multi-row entity table.
19
+ */
20
+ export declare function applyFieldPatch(schema: CompiledSlotSchema, snapshot: RuntimeSlotsSnapshot, patch: FieldPatchInput, options: {
21
+ source: UpdatedBy;
22
+ now?: number;
23
+ }): ApplyWritesResult;
24
+ export type ApplySlotFillingOptions = {
25
+ now?: number;
26
+ /** Map SF placeholder row keys (`new-1`) → allocated row ids (mutated in place). */
27
+ rowKeyMap?: Map<string, string>;
28
+ };
29
+ /**
30
+ * C4: apply accepted Slot Filling projection (full replace for present fields/rows).
31
+ * Multi-row: projection is the full set of rows; omitted existing rows are deleted.
32
+ */
33
+ export declare function applySlotFilling(schema: CompiledSlotSchema, snapshot: RuntimeSlotsSnapshot, output: SlotFillingOutput, options?: ApplySlotFillingOptions): ApplyWritesResult;
34
+ /**
35
+ * Mark remaining conversation fields as asked_unknown (Topic move-on).
36
+ */
37
+ export declare function markAskedUnknown(schema: CompiledSlotSchema, snapshot: RuntimeSlotsSnapshot, fields: Iterable<FieldPath>, options?: {
38
+ now?: number;
39
+ reason?: string;
40
+ }): ApplyWritesResult;
41
+ //# sourceMappingURL=write.d.ts.map
@@ -0,0 +1,319 @@
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
@@ -0,0 +1,36 @@
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
@@ -0,0 +1,28 @@
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
@@ -0,0 +1,17 @@
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