@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.
- package/README.md +35 -0
- package/bin/superpanel-sdk-runtime.mjs +22 -0
- package/dist/builder/codegen/emit.d.ts +17 -0
- package/dist/builder/codegen/emit.js +143 -0
- package/dist/builder/codegen/generate.d.ts +21 -0
- package/dist/builder/codegen/generate.js +90 -0
- package/dist/builder/compile/compile-agenda.d.ts +15 -0
- package/dist/builder/compile/compile-agenda.js +59 -0
- package/dist/builder/compile/compile-sequence.d.ts +24 -0
- package/dist/builder/compile/compile-sequence.js +121 -0
- package/dist/builder/compile/function-code.d.ts +6 -0
- package/dist/builder/compile/function-code.js +30 -0
- package/dist/builder/compile/registry.d.ts +13 -0
- package/dist/builder/compile/registry.js +42 -0
- package/dist/builder/load.d.ts +22 -0
- package/dist/builder/load.js +169 -0
- package/dist/builder/merge.d.ts +4 -0
- package/dist/builder/merge.js +19 -0
- package/dist/builder/paths.d.ts +10 -0
- package/dist/builder/paths.js +22 -0
- package/dist/builder/types.d.ts +31 -0
- package/dist/builder/types.js +2 -0
- package/dist/cli/compile/index.d.ts +4 -0
- package/dist/cli/compile/index.js +11 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.js +64 -0
- package/dist/cli/serve/index.d.ts +9 -0
- package/dist/cli/serve/index.js +340 -0
- package/dist/cli/type-gen/index.d.ts +5 -0
- package/dist/cli/type-gen/index.js +90 -0
- package/dist/compute-host/compute-host.d.ts +38 -0
- package/dist/compute-host/compute-host.js +45 -0
- package/dist/compute-host/index.d.ts +5 -0
- package/dist/compute-host/index.js +3 -0
- package/dist/compute-host/source-compute-host.d.ts +29 -0
- package/dist/compute-host/source-compute-host.js +72 -0
- package/dist/core/context/account-fields.d.ts +12 -0
- package/dist/core/context/account-fields.js +2 -0
- package/dist/core/context/contact.d.ts +60 -0
- package/dist/core/context/contact.js +34 -0
- package/dist/core/context/db.d.ts +69 -0
- package/dist/core/context/db.js +44 -0
- package/dist/core/context/docusign.d.ts +134 -0
- package/dist/core/context/docusign.js +108 -0
- package/dist/core/context/env.d.ts +9 -0
- package/dist/core/context/env.js +19 -0
- package/dist/core/context/filevine.d.ts +323 -0
- package/dist/core/context/filevine.js +254 -0
- package/dist/core/context/http.d.ts +9 -0
- package/dist/core/context/http.js +4 -0
- package/dist/core/context/hubspot.d.ts +250 -0
- package/dist/core/context/hubspot.js +191 -0
- package/dist/core/context/platform-registry.d.ts +67 -0
- package/dist/core/context/platform-registry.js +11 -0
- package/dist/core/context/platform.d.ts +33 -0
- package/dist/core/context/platform.js +2 -0
- package/dist/core/context/twilio.d.ts +50 -0
- package/dist/core/context/twilio.js +42 -0
- package/dist/core/context/types.d.ts +87 -0
- package/dist/core/context/types.js +9 -0
- package/dist/core/functions/define/case-context.d.ts +14 -0
- package/dist/core/functions/define/case-context.js +15 -0
- package/dist/core/functions/define/compute.d.ts +7 -0
- package/dist/core/functions/define/compute.js +17 -0
- package/dist/core/functions/define/custom.d.ts +4 -0
- package/dist/core/functions/define/custom.js +8 -0
- package/dist/core/functions/define/system.d.ts +7 -0
- package/dist/core/functions/define/system.js +12 -0
- package/dist/core/functions/func-ref.d.ts +16 -0
- package/dist/core/functions/func-ref.js +20 -0
- package/dist/core/functions/types.d.ts +78 -0
- package/dist/core/functions/types.js +5 -0
- package/dist/core/slot/compile-schema.d.ts +24 -0
- package/dist/core/slot/compile-schema.js +336 -0
- package/dist/core/slot/deps.d.ts +15 -0
- package/dist/core/slot/deps.js +88 -0
- package/dist/core/slot/derived.d.ts +32 -0
- package/dist/core/slot/derived.js +115 -0
- package/dist/core/slot/field.d.ts +36 -0
- package/dist/core/slot/field.js +97 -0
- package/dist/core/slot/id-ref.d.ts +4 -0
- package/dist/core/slot/id-ref.js +15 -0
- package/dist/core/slot/index.d.ts +15 -0
- package/dist/core/slot/index.js +14 -0
- package/dist/core/slot/path.d.ts +18 -0
- package/dist/core/slot/path.js +22 -0
- package/dist/core/slot/row.d.ts +22 -0
- package/dist/core/slot/row.js +54 -0
- package/dist/core/slot/schema.d.ts +102 -0
- package/dist/core/slot/schema.js +17 -0
- package/dist/core/slot/sf-schema.d.ts +23 -0
- package/dist/core/slot/sf-schema.js +125 -0
- package/dist/core/slot/sf-validate.d.ts +14 -0
- package/dist/core/slot/sf-validate.js +132 -0
- package/dist/core/slot/snapshot.d.ts +18 -0
- package/dist/core/slot/snapshot.js +138 -0
- package/dist/core/slot/types.d.ts +144 -0
- package/dist/core/slot/types.js +2 -0
- package/dist/core/slot/value.d.ts +9 -0
- package/dist/core/slot/value.js +97 -0
- package/dist/core/slot/write.d.ts +41 -0
- package/dist/core/slot/write.js +319 -0
- package/dist/core/stage/builders.d.ts +36 -0
- package/dist/core/stage/builders.js +28 -0
- package/dist/core/stage/condition.d.ts +17 -0
- package/dist/core/stage/condition.js +198 -0
- package/dist/core/stage/drive.d.ts +43 -0
- package/dist/core/stage/drive.js +77 -0
- package/dist/core/stage/finish-when.d.ts +20 -0
- package/dist/core/stage/finish-when.js +82 -0
- package/dist/core/stage/flatten.d.ts +27 -0
- package/dist/core/stage/flatten.js +464 -0
- package/dist/core/stage/index.d.ts +11 -0
- package/dist/core/stage/index.js +10 -0
- package/dist/core/stage/simulate.d.ts +24 -0
- package/dist/core/stage/simulate.js +33 -0
- package/dist/core/stage/transfer-fields.d.ts +14 -0
- package/dist/core/stage/transfer-fields.js +88 -0
- package/dist/core/stage/types.d.ts +177 -0
- package/dist/core/stage/types.js +2 -0
- package/dist/core/stage/validate.d.ts +11 -0
- package/dist/core/stage/validate.js +226 -0
- package/dist/core/stage/walk.d.ts +34 -0
- package/dist/core/stage/walk.js +96 -0
- package/dist/dev-overlay/protocol.d.ts +53 -0
- package/dist/dev-overlay/protocol.js +21 -0
- package/dist/fn-context/build-context.d.ts +11 -0
- package/dist/fn-context/build-context.js +21 -0
- package/dist/fn-context/fake/empty-api.d.ts +9 -0
- package/dist/fn-context/fake/empty-api.js +50 -0
- package/dist/fn-context/fake/index.d.ts +12 -0
- package/dist/fn-context/fake/index.js +22 -0
- package/dist/fn-context/index.d.ts +9 -0
- package/dist/fn-context/index.js +8 -0
- package/dist/fn-context/log.d.ts +8 -0
- package/dist/fn-context/log.js +29 -0
- package/dist/fn-context/make-ctx.d.ts +9 -0
- package/dist/fn-context/make-ctx.js +51 -0
- package/dist/fn-context/rpc/errors.d.ts +25 -0
- package/dist/fn-context/rpc/errors.js +23 -0
- package/dist/fn-context/rpc/index.d.ts +10 -0
- package/dist/fn-context/rpc/index.js +9 -0
- package/dist/fn-context/rpc/integration/index.d.ts +51 -0
- package/dist/fn-context/rpc/integration/index.js +19 -0
- package/dist/fn-context/rpc/integration/list-bindings.d.ts +21 -0
- package/dist/fn-context/rpc/integration/list-bindings.js +18 -0
- package/dist/fn-context/rpc/log.d.ts +36 -0
- package/dist/fn-context/rpc/log.js +16 -0
- package/dist/fn-context/rpc/platform.d.ts +28 -0
- package/dist/fn-context/rpc/platform.js +15 -0
- package/dist/fn-context/rpc/request-id.d.ts +5 -0
- package/dist/fn-context/rpc/request-id.js +13 -0
- package/dist/fn-context/rpc/trpc-client.d.ts +7 -0
- package/dist/fn-context/rpc/trpc-client.js +109 -0
- package/dist/fn-context/rpc/trpc-init.d.ts +17 -0
- package/dist/fn-context/rpc/trpc-init.js +17 -0
- package/dist/fn-context/rpc/trpc-router-factory.d.ts +84 -0
- package/dist/fn-context/rpc/trpc-router-factory.js +9 -0
- package/dist/fn-context/types.d.ts +17 -0
- package/dist/fn-context/types.js +2 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +34 -0
- package/dist/voice-service/actions.d.ts +20 -0
- package/dist/voice-service/actions.js +2 -0
- package/dist/voice-service/index.d.ts +3 -0
- package/dist/voice-service/index.js +3 -0
- package/dist/voice-service/types.d.ts +1010 -0
- package/dist/voice-service/types.js +16 -0
- package/package.json +55 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Reserved row-identity field name. Authors should not invent another. */
|
|
2
|
+
export const ROW_ID_FIELD = 'id';
|
|
3
|
+
export const idFieldCore = {
|
|
4
|
+
derived: false,
|
|
5
|
+
label: 'Id',
|
|
6
|
+
dataType: 'string',
|
|
7
|
+
source: 'system',
|
|
8
|
+
extraction: 'Row identity',
|
|
9
|
+
enum: null,
|
|
10
|
+
nullable: false,
|
|
11
|
+
};
|
|
12
|
+
/** Unified row id. Write `id: idField` on every table. */
|
|
13
|
+
export const idField = {
|
|
14
|
+
...idFieldCore,
|
|
15
|
+
identifier: null,
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { FieldPath } from './path.js';
|
|
2
|
+
import type { CompiledSlotSchema } from './types.js';
|
|
3
|
+
type JsonSchema = Record<string, unknown>;
|
|
4
|
+
export type SfSchemaOptions = {
|
|
5
|
+
/**
|
|
6
|
+
* Field paths included with full definitions.
|
|
7
|
+
* When omitted, all conversation fields are included (full-schema / hangup pass).
|
|
8
|
+
*/
|
|
9
|
+
focusFields?: Iterable<FieldPath>;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Build JSON Schema for Slot Filling structured output.
|
|
13
|
+
* Derived and `source: system` fields are never included.
|
|
14
|
+
*/
|
|
15
|
+
export declare function buildSlotFillingJsonSchema(schema: CompiledSlotSchema, options?: SfSchemaOptions): JsonSchema;
|
|
16
|
+
/** Compact catalog for out-of-focus conversation fields (C1). */
|
|
17
|
+
export declare function buildFieldCatalog(schema: CompiledSlotSchema, exclude?: Iterable<FieldPath>): Array<{
|
|
18
|
+
path: FieldPath;
|
|
19
|
+
label: string;
|
|
20
|
+
extraction: string;
|
|
21
|
+
}>;
|
|
22
|
+
export {};
|
|
23
|
+
//# sourceMappingURL=sf-schema.d.ts.map
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { compiledTableEntries, isSnapshotSingleRow } from './compile-schema.js';
|
|
2
|
+
import { formatFieldPath, parseFieldPath } from './path.js';
|
|
3
|
+
function dataTypeToJsonSchema(dataType, enumValues, nullable) {
|
|
4
|
+
let base;
|
|
5
|
+
if (enumValues && enumValues.length > 0) {
|
|
6
|
+
base = { enum: [...enumValues] };
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
switch (dataType) {
|
|
10
|
+
case 'string':
|
|
11
|
+
case 'date':
|
|
12
|
+
case 'datetime':
|
|
13
|
+
base = { type: 'string' };
|
|
14
|
+
break;
|
|
15
|
+
case 'number':
|
|
16
|
+
base = { type: 'number' };
|
|
17
|
+
break;
|
|
18
|
+
case 'boolean':
|
|
19
|
+
base = { type: 'boolean' };
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (nullable) {
|
|
24
|
+
if (base.enum) {
|
|
25
|
+
return { enum: [...base.enum, null] };
|
|
26
|
+
}
|
|
27
|
+
return { type: [base.type, 'null'] };
|
|
28
|
+
}
|
|
29
|
+
return base;
|
|
30
|
+
}
|
|
31
|
+
function extractedFieldSchema(field) {
|
|
32
|
+
return {
|
|
33
|
+
type: 'object',
|
|
34
|
+
additionalProperties: false,
|
|
35
|
+
required: ['reason', 'value', 'state'],
|
|
36
|
+
properties: {
|
|
37
|
+
reason: { type: 'string', minLength: 1 },
|
|
38
|
+
value: dataTypeToJsonSchema(field.dataType, field.enum, true),
|
|
39
|
+
state: { type: 'string', enum: ['filled', 'asked_unknown', 'declined'] },
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Build JSON Schema for Slot Filling structured output.
|
|
45
|
+
* Derived and `source: system` fields are never included.
|
|
46
|
+
*/
|
|
47
|
+
export function buildSlotFillingJsonSchema(schema, options = {}) {
|
|
48
|
+
const focus = options.focusFields
|
|
49
|
+
? new Set([...options.focusFields].map((p) => {
|
|
50
|
+
const parsed = parseFieldPath(p);
|
|
51
|
+
if (!parsed)
|
|
52
|
+
throw new Error(`Invalid focus field: ${p}`);
|
|
53
|
+
return parsed.path;
|
|
54
|
+
}))
|
|
55
|
+
: null;
|
|
56
|
+
const tableSchemas = {};
|
|
57
|
+
for (const [tableName, table] of compiledTableEntries(schema)) {
|
|
58
|
+
const fieldProps = {};
|
|
59
|
+
for (const [fieldName, field] of Object.entries(table.fields)) {
|
|
60
|
+
if (field.derived)
|
|
61
|
+
continue;
|
|
62
|
+
if (field.source !== 'conversation')
|
|
63
|
+
continue;
|
|
64
|
+
const path = formatFieldPath(tableName, fieldName);
|
|
65
|
+
if (focus && !focus.has(path))
|
|
66
|
+
continue;
|
|
67
|
+
fieldProps[fieldName] = extractedFieldSchema(field);
|
|
68
|
+
}
|
|
69
|
+
if (Object.keys(fieldProps).length === 0)
|
|
70
|
+
continue;
|
|
71
|
+
if (isSnapshotSingleRow(table)) {
|
|
72
|
+
tableSchemas[tableName] = {
|
|
73
|
+
type: 'object',
|
|
74
|
+
additionalProperties: false,
|
|
75
|
+
properties: fieldProps,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
tableSchemas[tableName] = {
|
|
80
|
+
type: 'array',
|
|
81
|
+
items: {
|
|
82
|
+
type: 'object',
|
|
83
|
+
additionalProperties: false,
|
|
84
|
+
required: ['rowKey', 'fields'],
|
|
85
|
+
properties: {
|
|
86
|
+
rowKey: { type: 'string', minLength: 1 },
|
|
87
|
+
fields: {
|
|
88
|
+
type: 'object',
|
|
89
|
+
additionalProperties: false,
|
|
90
|
+
properties: fieldProps,
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
type: 'object',
|
|
99
|
+
additionalProperties: false,
|
|
100
|
+
properties: tableSchemas,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/** Compact catalog for out-of-focus conversation fields (C1). */
|
|
104
|
+
export function buildFieldCatalog(schema, exclude = []) {
|
|
105
|
+
const excluded = new Set([...exclude].map((p) => parseFieldPath(p)?.path).filter(Boolean));
|
|
106
|
+
const catalog = [];
|
|
107
|
+
for (const [tableName, table] of compiledTableEntries(schema)) {
|
|
108
|
+
for (const [fieldName, field] of Object.entries(table.fields)) {
|
|
109
|
+
if (field.derived)
|
|
110
|
+
continue;
|
|
111
|
+
if (field.source !== 'conversation')
|
|
112
|
+
continue;
|
|
113
|
+
const path = formatFieldPath(tableName, fieldName);
|
|
114
|
+
if (excluded.has(path))
|
|
115
|
+
continue;
|
|
116
|
+
catalog.push({
|
|
117
|
+
path,
|
|
118
|
+
label: field.label,
|
|
119
|
+
extraction: field.extraction,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return catalog;
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=sf-schema.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { CompiledSlotSchema, SlotFillingValidateResult } from './types.js';
|
|
2
|
+
export type ValidateSlotFillingOptions = {
|
|
3
|
+
/** Conversation version carried by the SF task. */
|
|
4
|
+
taskVersion: number;
|
|
5
|
+
/** Current conversation version; mismatch → whole pack discarded. */
|
|
6
|
+
currentVersion: number;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* C3 gates for Slot Filling output: version gate, type/enum, write protection.
|
|
10
|
+
* Derived / system fields in the payload are dropped (write protection).
|
|
11
|
+
* Type/enum failures are rejected per-field; valid fields stay accepted.
|
|
12
|
+
*/
|
|
13
|
+
export declare function validateSlotFillingOutput(schema: CompiledSlotSchema, raw: unknown, options: ValidateSlotFillingOptions): SlotFillingValidateResult;
|
|
14
|
+
//# sourceMappingURL=sf-validate.d.ts.map
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { getCompiledTable, isSnapshotSingleRow } from './compile-schema.js';
|
|
2
|
+
import { formatFieldPath } from './path.js';
|
|
3
|
+
import { isReservedTableName } from './row.js';
|
|
4
|
+
import { validateExtractedField } from './value.js';
|
|
5
|
+
function isExtractedField(value) {
|
|
6
|
+
if (!value || typeof value !== 'object')
|
|
7
|
+
return false;
|
|
8
|
+
const v = value;
|
|
9
|
+
return (typeof v.reason === 'string' &&
|
|
10
|
+
'value' in v &&
|
|
11
|
+
(v.state === 'filled' || v.state === 'asked_unknown' || v.state === 'declined'));
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* C3 gates for Slot Filling output: version gate, type/enum, write protection.
|
|
15
|
+
* Derived / system fields in the payload are dropped (write protection).
|
|
16
|
+
* Type/enum failures are rejected per-field; valid fields stay accepted.
|
|
17
|
+
*/
|
|
18
|
+
export function validateSlotFillingOutput(schema, raw, options) {
|
|
19
|
+
if (options.taskVersion !== options.currentVersion) {
|
|
20
|
+
return {
|
|
21
|
+
ok: false,
|
|
22
|
+
reason: 'stale-version',
|
|
23
|
+
issues: [
|
|
24
|
+
{
|
|
25
|
+
code: 'stale-version',
|
|
26
|
+
message: `SF version ${options.taskVersion} != current ${options.currentVersion}`,
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
32
|
+
return {
|
|
33
|
+
ok: false,
|
|
34
|
+
reason: 'invalid-shape',
|
|
35
|
+
issues: [
|
|
36
|
+
{
|
|
37
|
+
code: 'invalid-shape',
|
|
38
|
+
message: 'SlotFillingOutput must be an object of table projections',
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
const accepted = {};
|
|
44
|
+
const rejected = [];
|
|
45
|
+
const shapeIssues = [];
|
|
46
|
+
for (const [tableName, projection] of Object.entries(raw)) {
|
|
47
|
+
if (isReservedTableName(tableName))
|
|
48
|
+
continue;
|
|
49
|
+
const table = getCompiledTable(schema, tableName);
|
|
50
|
+
if (!table) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (isSnapshotSingleRow(table)) {
|
|
54
|
+
if (!projection || typeof projection !== 'object' || Array.isArray(projection)) {
|
|
55
|
+
shapeIssues.push({
|
|
56
|
+
code: 'invalid-projection',
|
|
57
|
+
message: `single-row table ${tableName} expects an object projection`,
|
|
58
|
+
path: tableName,
|
|
59
|
+
});
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const outFields = {};
|
|
63
|
+
for (const [fieldName, extracted] of Object.entries(projection)) {
|
|
64
|
+
const path = formatFieldPath(tableName, fieldName);
|
|
65
|
+
const field = table.fields[fieldName];
|
|
66
|
+
if (!field || field.derived || field.source !== 'conversation') {
|
|
67
|
+
continue; // write protection: drop
|
|
68
|
+
}
|
|
69
|
+
if (!isExtractedField(extracted)) {
|
|
70
|
+
rejected.push(path);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const issues = validateExtractedField(field, extracted, path);
|
|
74
|
+
if (issues.length > 0) {
|
|
75
|
+
rejected.push(path);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
outFields[fieldName] = extracted;
|
|
79
|
+
}
|
|
80
|
+
if (Object.keys(outFields).length > 0) {
|
|
81
|
+
accepted[tableName] = outFields;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
if (!Array.isArray(projection)) {
|
|
86
|
+
shapeIssues.push({
|
|
87
|
+
code: 'invalid-projection',
|
|
88
|
+
message: `multi-row table ${tableName} expects an array projection`,
|
|
89
|
+
path: tableName,
|
|
90
|
+
});
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const outRows = [];
|
|
94
|
+
for (const row of projection) {
|
|
95
|
+
if (!row || typeof row !== 'object' || typeof row.rowKey !== 'string') {
|
|
96
|
+
shapeIssues.push({
|
|
97
|
+
code: 'invalid-row',
|
|
98
|
+
message: `multi-row table ${tableName} row missing rowKey`,
|
|
99
|
+
path: tableName,
|
|
100
|
+
});
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
const fieldsIn = row.fields ?? {};
|
|
104
|
+
const outFields = {};
|
|
105
|
+
for (const [fieldName, extracted] of Object.entries(fieldsIn)) {
|
|
106
|
+
const path = formatFieldPath(tableName, fieldName);
|
|
107
|
+
const field = table.fields[fieldName];
|
|
108
|
+
if (!field || field.derived || field.source !== 'conversation') {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (!isExtractedField(extracted)) {
|
|
112
|
+
rejected.push(path);
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
const issues = validateExtractedField(field, extracted, path);
|
|
116
|
+
if (issues.length > 0) {
|
|
117
|
+
rejected.push(path);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
outFields[fieldName] = extracted;
|
|
121
|
+
}
|
|
122
|
+
outRows.push({ rowKey: row.rowKey, fields: outFields });
|
|
123
|
+
}
|
|
124
|
+
accepted[tableName] = outRows;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (shapeIssues.length > 0) {
|
|
128
|
+
return { ok: false, reason: 'invalid-shape', issues: shapeIssues };
|
|
129
|
+
}
|
|
130
|
+
return { ok: true, accepted, rejected };
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=sf-validate.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { FieldPath } from './path.js';
|
|
2
|
+
import type { CompiledSlotSchema, FieldValue, RuntimeSlotsSnapshot, TableData } from './types.js';
|
|
3
|
+
export type SnapshotSeed = Record<string, unknown>;
|
|
4
|
+
/**
|
|
5
|
+
* Build a runtime snapshot for local tests.
|
|
6
|
+
*
|
|
7
|
+
* - `table.field` keys seed single-row (or current-party) fields
|
|
8
|
+
* - bare multi-table name keys seed row arrays:
|
|
9
|
+
* `children: [{ id?: string, name: 'Lily', age: 8 }]`
|
|
10
|
+
*/
|
|
11
|
+
export declare function snapshotFrom(schema: CompiledSlotSchema, seed?: SnapshotSeed, options?: {
|
|
12
|
+
now?: number;
|
|
13
|
+
}): RuntimeSlotsSnapshot;
|
|
14
|
+
/** Deep-clone a snapshot (structuredClone when available). */
|
|
15
|
+
export declare function cloneSnapshot(snapshot: RuntimeSlotsSnapshot): RuntimeSlotsSnapshot;
|
|
16
|
+
export declare function getSnapshotTable(snapshot: RuntimeSlotsSnapshot, tableName: string): TableData | undefined;
|
|
17
|
+
export declare function getSingleField(snapshot: RuntimeSlotsSnapshot, path: FieldPath): FieldValue | undefined;
|
|
18
|
+
//# sourceMappingURL=snapshot.d.ts.map
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { compiledTableEntries, getCompiledTable, isSnapshotSingleRow } from './compile-schema.js';
|
|
2
|
+
import { emptyFieldValue, makeFieldValue } from './field.js';
|
|
3
|
+
import { parseFieldPath } from './path.js';
|
|
4
|
+
import { emptySlotRow, getRowField, isReservedTableName, isRowIdField, slotFieldNames } from './row.js';
|
|
5
|
+
import { validateBareValue } from './value.js';
|
|
6
|
+
function seedToFieldValue(seed, now) {
|
|
7
|
+
if (seed && typeof seed === 'object' && !Array.isArray(seed) && 'state' in seed) {
|
|
8
|
+
const s = seed;
|
|
9
|
+
return makeFieldValue({
|
|
10
|
+
value: (s.value ?? null),
|
|
11
|
+
state: s.state,
|
|
12
|
+
reason: s.reason ?? null,
|
|
13
|
+
updatedBy: s.updatedBy ?? 'import',
|
|
14
|
+
updatedAt: s.updatedAt ?? now,
|
|
15
|
+
history: s.history ?? [],
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
if (seed === undefined) {
|
|
19
|
+
return emptyFieldValue(now);
|
|
20
|
+
}
|
|
21
|
+
return makeFieldValue({
|
|
22
|
+
value: seed,
|
|
23
|
+
state: 'filled',
|
|
24
|
+
reason: null,
|
|
25
|
+
updatedBy: 'import',
|
|
26
|
+
updatedAt: now,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Build a runtime snapshot for local tests.
|
|
31
|
+
*
|
|
32
|
+
* - `table.field` keys seed single-row (or current-party) fields
|
|
33
|
+
* - bare multi-table name keys seed row arrays:
|
|
34
|
+
* `children: [{ id?: string, name: 'Lily', age: 8 }]`
|
|
35
|
+
*/
|
|
36
|
+
export function snapshotFrom(schema, seed = {}, options = {}) {
|
|
37
|
+
const now = options.now ?? 0;
|
|
38
|
+
const snapshot = {};
|
|
39
|
+
for (const [tableName, table] of compiledTableEntries(schema)) {
|
|
40
|
+
if (isSnapshotSingleRow(table)) {
|
|
41
|
+
snapshot[tableName] = emptySlotRow(table.fields, { now });
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
snapshot[tableName] = [];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
for (const [key, value] of Object.entries(seed)) {
|
|
48
|
+
const parsed = parseFieldPath(key);
|
|
49
|
+
if (parsed) {
|
|
50
|
+
const table = getCompiledTable(schema, parsed.table);
|
|
51
|
+
if (!table) {
|
|
52
|
+
throw new Error(`Unknown table in seed: ${parsed.table}`);
|
|
53
|
+
}
|
|
54
|
+
if (!isSnapshotSingleRow(table)) {
|
|
55
|
+
throw new Error(`Field path seed ${key} is only for single-row tables; seed multi rows by table name`);
|
|
56
|
+
}
|
|
57
|
+
if (isRowIdField(parsed.field)) {
|
|
58
|
+
snapshot[parsed.table].id = value == null ? '' : String(value);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const field = table.fields[parsed.field];
|
|
62
|
+
if (!field) {
|
|
63
|
+
throw new Error(`Unknown field in seed: ${key}`);
|
|
64
|
+
}
|
|
65
|
+
const fv = seedToFieldValue(value, now);
|
|
66
|
+
if (fv.state === 'filled' || fv.state === 'asked_unknown') {
|
|
67
|
+
const issues = validateBareValue(field, fv.value, key, {
|
|
68
|
+
allowNull: field.derived || (field.derived === false && field.nullable),
|
|
69
|
+
});
|
|
70
|
+
if (issues.length > 0) {
|
|
71
|
+
throw new Error(issues.map((i) => i.message).join('; '));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
snapshot[parsed.table][parsed.field] = fv;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const table = getCompiledTable(schema, key);
|
|
78
|
+
if (!table) {
|
|
79
|
+
throw new Error(`Unknown seed key: ${key}`);
|
|
80
|
+
}
|
|
81
|
+
if (isSnapshotSingleRow(table)) {
|
|
82
|
+
throw new Error(`Seed key ${key} is not a multi-row table`);
|
|
83
|
+
}
|
|
84
|
+
if (!Array.isArray(value)) {
|
|
85
|
+
throw new Error(`Multi-row seed ${key} must be an array`);
|
|
86
|
+
}
|
|
87
|
+
const rows = [];
|
|
88
|
+
let i = 0;
|
|
89
|
+
for (const rowSeed of value) {
|
|
90
|
+
if (!rowSeed || typeof rowSeed !== 'object') {
|
|
91
|
+
throw new Error(`Invalid row seed in ${key}`);
|
|
92
|
+
}
|
|
93
|
+
const rowObj = rowSeed;
|
|
94
|
+
const seededId = rowObj.id ?? rowObj.rowId;
|
|
95
|
+
const id = typeof seededId === 'string' && seededId.length > 0 ? seededId : `r${++i}`;
|
|
96
|
+
const row = { id };
|
|
97
|
+
for (const fieldName of slotFieldNames(table.fields)) {
|
|
98
|
+
if (fieldName in rowObj) {
|
|
99
|
+
row[fieldName] = seedToFieldValue(rowObj[fieldName], now);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
row[fieldName] = emptyFieldValue(now);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
rows.push(row);
|
|
106
|
+
}
|
|
107
|
+
snapshot[key] = rows;
|
|
108
|
+
}
|
|
109
|
+
return snapshot;
|
|
110
|
+
}
|
|
111
|
+
/** Deep-clone a snapshot (structuredClone when available). */
|
|
112
|
+
export function cloneSnapshot(snapshot) {
|
|
113
|
+
return typeof structuredClone === 'function'
|
|
114
|
+
? structuredClone(snapshot)
|
|
115
|
+
: JSON.parse(JSON.stringify(snapshot));
|
|
116
|
+
}
|
|
117
|
+
export function getSnapshotTable(snapshot, tableName) {
|
|
118
|
+
if (isReservedTableName(tableName))
|
|
119
|
+
return undefined;
|
|
120
|
+
const value = snapshot[tableName];
|
|
121
|
+
if (value == null)
|
|
122
|
+
return undefined;
|
|
123
|
+
if (Array.isArray(value))
|
|
124
|
+
return value;
|
|
125
|
+
if (typeof value === 'object' && 'id' in value)
|
|
126
|
+
return value;
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
export function getSingleField(snapshot, path) {
|
|
130
|
+
const parsed = parseFieldPath(path);
|
|
131
|
+
if (!parsed)
|
|
132
|
+
return undefined;
|
|
133
|
+
const table = getSnapshotTable(snapshot, parsed.table);
|
|
134
|
+
if (!table || Array.isArray(table))
|
|
135
|
+
return undefined;
|
|
136
|
+
return getRowField(table, parsed.field);
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=snapshot.js.map
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import type { DataType } from './schema.js';
|
|
2
|
+
import type { FieldPath } from './path.js';
|
|
3
|
+
import type { FieldState } from '../context/types.js';
|
|
4
|
+
export type UpdatedBy = 'slot-filling' | 'derived' | 'function' | 'human' | 'import' | 'platform';
|
|
5
|
+
/** Prior generation archived into FieldValue.history (no reason — 01-slot-db §4.1). */
|
|
6
|
+
export type FieldHistoryEntry = {
|
|
7
|
+
value: string | number | boolean | null;
|
|
8
|
+
state: FieldState;
|
|
9
|
+
at: number;
|
|
10
|
+
by: UpdatedBy;
|
|
11
|
+
};
|
|
12
|
+
/** In-memory field envelope (01-slot-design §2 + 01-slot-db §4 history). */
|
|
13
|
+
export type FieldValue = {
|
|
14
|
+
reason: string | null;
|
|
15
|
+
value: string | number | boolean | null;
|
|
16
|
+
state: FieldState;
|
|
17
|
+
updatedAt: number;
|
|
18
|
+
updatedBy: UpdatedBy;
|
|
19
|
+
/** Past generations ascending; excludes current. Appended only on C5 change. */
|
|
20
|
+
history: FieldHistoryEntry[];
|
|
21
|
+
};
|
|
22
|
+
/** One history append produced by a write batch (for platform write_log). */
|
|
23
|
+
export type HistoryAppend = {
|
|
24
|
+
path: FieldPath;
|
|
25
|
+
entry: FieldHistoryEntry;
|
|
26
|
+
};
|
|
27
|
+
export type ExtractedField = {
|
|
28
|
+
reason: string;
|
|
29
|
+
value: string | number | boolean | null;
|
|
30
|
+
state: 'filled' | 'asked_unknown' | 'declined';
|
|
31
|
+
};
|
|
32
|
+
export type SingleRowProjection = Record<string, ExtractedField>;
|
|
33
|
+
export type MultiRowProjection = Array<{
|
|
34
|
+
rowKey: string;
|
|
35
|
+
fields: Record<string, ExtractedField>;
|
|
36
|
+
}>;
|
|
37
|
+
export type SlotFillingOutput = Record<string, SingleRowProjection | MultiRowProjection>;
|
|
38
|
+
export type RuntimeNormalFieldDef = {
|
|
39
|
+
derived: false;
|
|
40
|
+
label: string;
|
|
41
|
+
dataType: DataType;
|
|
42
|
+
source: 'conversation' | 'system';
|
|
43
|
+
extraction: string;
|
|
44
|
+
enum: Array<string | number> | null;
|
|
45
|
+
nullable: boolean;
|
|
46
|
+
identifier: null | {
|
|
47
|
+
kind: 'phone' | 'email';
|
|
48
|
+
};
|
|
49
|
+
ref: null | {
|
|
50
|
+
table: string;
|
|
51
|
+
};
|
|
52
|
+
deprecated?: boolean;
|
|
53
|
+
};
|
|
54
|
+
export type RuntimeDerivedFieldDef = {
|
|
55
|
+
derived: true;
|
|
56
|
+
label: string;
|
|
57
|
+
dataType: DataType;
|
|
58
|
+
dependsOn: FieldPath[];
|
|
59
|
+
computeFn: string;
|
|
60
|
+
deprecated?: boolean;
|
|
61
|
+
};
|
|
62
|
+
export type RuntimeFieldDef = RuntimeNormalFieldDef | RuntimeDerivedFieldDef;
|
|
63
|
+
export type RuntimeTableDef = {
|
|
64
|
+
label: string;
|
|
65
|
+
rows: 'single' | 'multi';
|
|
66
|
+
description: string;
|
|
67
|
+
fields: Record<string, RuntimeFieldDef>;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Compiled schema: one key per table, plus derived graph.
|
|
71
|
+
* `derivedOrder` / `reverseDeps` are reserved and cannot be table names.
|
|
72
|
+
*/
|
|
73
|
+
export type CompiledSlotSchema = {
|
|
74
|
+
/** Derived fields in topological order (dependencies first). */
|
|
75
|
+
derivedOrder: FieldPath[];
|
|
76
|
+
/** `table.field` → derived fields that list it in dependsOn. */
|
|
77
|
+
reverseDeps: Record<FieldPath, FieldPath[]>;
|
|
78
|
+
[table: string]: RuntimeTableDef | FieldPath[] | Record<FieldPath, FieldPath[]>;
|
|
79
|
+
};
|
|
80
|
+
export type SlotCurrent = {
|
|
81
|
+
contactId: string;
|
|
82
|
+
caseContactId: string;
|
|
83
|
+
caseFileId: string;
|
|
84
|
+
sessionId: string | null;
|
|
85
|
+
};
|
|
86
|
+
export type SingleRowTableData = {
|
|
87
|
+
id: string;
|
|
88
|
+
[field: string]: FieldValue | string;
|
|
89
|
+
};
|
|
90
|
+
/** Multi-row table row: `id` plus field envelopes (codegen mirrors this). */
|
|
91
|
+
export type MultiRowRecord = {
|
|
92
|
+
id: string;
|
|
93
|
+
[field: string]: FieldValue | string;
|
|
94
|
+
};
|
|
95
|
+
export type MultiRowTableData = MultiRowRecord[];
|
|
96
|
+
export type TableData = SingleRowTableData | MultiRowTableData;
|
|
97
|
+
/**
|
|
98
|
+
* Runtime snapshot: `current` plus one key per table.
|
|
99
|
+
* `current` is reserved and cannot be a table name.
|
|
100
|
+
*/
|
|
101
|
+
export type RuntimeSlotsSnapshot = {
|
|
102
|
+
current?: SlotCurrent;
|
|
103
|
+
[table: string]: TableData | SlotCurrent | undefined;
|
|
104
|
+
};
|
|
105
|
+
export type ValidationIssue = {
|
|
106
|
+
code: string;
|
|
107
|
+
message: string;
|
|
108
|
+
path?: string;
|
|
109
|
+
};
|
|
110
|
+
export type ValidationResult = {
|
|
111
|
+
ok: true;
|
|
112
|
+
} | {
|
|
113
|
+
ok: false;
|
|
114
|
+
issues: ValidationIssue[];
|
|
115
|
+
};
|
|
116
|
+
export type WriteSource = Exclude<UpdatedBy, 'derived'>;
|
|
117
|
+
export type ApplyWritesOptions = {
|
|
118
|
+
source: WriteSource;
|
|
119
|
+
now?: number;
|
|
120
|
+
/** When true (default for function/platform), only `source: system` normals. */
|
|
121
|
+
systemOnly?: boolean;
|
|
122
|
+
};
|
|
123
|
+
export type ApplyWritesResult = {
|
|
124
|
+
snapshot: RuntimeSlotsSnapshot;
|
|
125
|
+
changed: FieldPath[];
|
|
126
|
+
/** Previous generations archived into field envelopes this batch. */
|
|
127
|
+
history: HistoryAppend[];
|
|
128
|
+
};
|
|
129
|
+
export type RecomputeResult = {
|
|
130
|
+
snapshot: RuntimeSlotsSnapshot;
|
|
131
|
+
changed: FieldPath[];
|
|
132
|
+
history: HistoryAppend[];
|
|
133
|
+
};
|
|
134
|
+
export type SlotFillingValidateResult = {
|
|
135
|
+
ok: true;
|
|
136
|
+
accepted: SlotFillingOutput;
|
|
137
|
+
/** Fields that failed type/enum checks — caller may re-extract. */
|
|
138
|
+
rejected: FieldPath[];
|
|
139
|
+
} | {
|
|
140
|
+
ok: false;
|
|
141
|
+
reason: 'stale-version' | 'invalid-shape';
|
|
142
|
+
issues: ValidationIssue[];
|
|
143
|
+
};
|
|
144
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { DataType } from './schema.js';
|
|
2
|
+
import type { ExtractedField, RuntimeFieldDef, RuntimeNormalFieldDef, ValidationIssue } from './types.js';
|
|
3
|
+
export declare function isValidDataTypeValue(dataType: DataType, value: unknown): boolean;
|
|
4
|
+
export declare function validateExtractedField(field: RuntimeNormalFieldDef, extracted: ExtractedField, path: string): ValidationIssue[];
|
|
5
|
+
/** Validate a bare write value for a normal or derived field. */
|
|
6
|
+
export declare function validateBareValue(field: RuntimeFieldDef, value: unknown, path: string, options?: {
|
|
7
|
+
allowNull?: boolean;
|
|
8
|
+
}): ValidationIssue[];
|
|
9
|
+
//# sourceMappingURL=value.d.ts.map
|