asma-core-helpers 0.16.0 → 0.18.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.
@@ -0,0 +1,112 @@
1
+ /**
2
+ * The template authoring document — a normalized entity graph with stable ids and
3
+ * explicit order arrays, in DOC-LAW-1 / DOC-LAW-2 minimal form.
4
+ *
5
+ * Every collection is optional because DOC-LAW-2 forbids storing an empty one: a
6
+ * document with no alternatives has no `alternativesById` key at all. Readers hydrate
7
+ * to a total in-memory model once at the document boundary rather than writing `?? {}`
8
+ * at each use site.
9
+ *
10
+ * @see _docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:152 (document shape)
11
+ * @see _docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:193 (DOC-LAW-1)
12
+ * @see _docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:214 (§2.2a mapping graph)
13
+ */
14
+
15
+ import type { QuestionSubtype, QuestionType } from './questionTypes.js'
16
+
17
+ export type QuestionId = string
18
+ export type RowId = string
19
+ export type AltId = string
20
+ export type TabId = string
21
+ export type ActionId = string
22
+ export type NodeId = string
23
+ export type BindingId = string
24
+ export type FilterId = string
25
+ export type MappingId = string
26
+
27
+ /** A scalar a document may carry. Deliberately not `any`: DOC-LAW-2 bans `null`. */
28
+ export type DocScalar = string | number | boolean
29
+
30
+ /**
31
+ * `type` is the question's own discriminator (M-062), a closed union rather than a string — see
32
+ * `questionTypes.ts` for why the value cannot be loose. Not to be confused with the other `kind`
33
+ * slots in this file: `QnrAction.kind` and `BindingTarget.kind` discriminate *structure*, not the
34
+ * question a user authored.
35
+ */
36
+ export type QnrQuestion = {
37
+ type: QuestionType
38
+ subtype?: QuestionSubtype
39
+ label?: string
40
+ required?: boolean
41
+ defaultValue?: DocScalar
42
+ [key: string]: unknown
43
+ }
44
+
45
+ export type QnrAlternative = { label?: string; value?: DocScalar; [key: string]: unknown }
46
+ export type QnrGridRow = { label?: string; [key: string]: unknown }
47
+ export type QnrTab = { label?: string; [key: string]: unknown }
48
+ export type QnrAction = { kind?: string; [key: string]: unknown }
49
+
50
+ /** One traversal step. Many questions off one entity share a single node (§2.2a). */
51
+ export type MappingNode = {
52
+ entityId: string
53
+ parentNodeId?: NodeId
54
+ relationshipId?: string
55
+ filterOrder?: FilterId[]
56
+ [key: string]: unknown
57
+ }
58
+
59
+ /**
60
+ * A binding's target. A `gridColumn` target is a pair, which is why the binding key is
61
+ * a synthetic id rather than the question id (§2.2a).
62
+ */
63
+ export type BindingTarget =
64
+ | { kind: 'question'; questionId: QuestionId }
65
+ | { kind: 'gridColumn'; gridQuestionId: QuestionId; columnQuestionId: QuestionId }
66
+
67
+ export type MappingBinding = {
68
+ nodeId: NodeId
69
+ fieldId: string
70
+ target: BindingTarget
71
+ cardinality?: string
72
+ onMissing?: string
73
+ onMany?: string
74
+ [key: string]: unknown
75
+ }
76
+
77
+ /** Carries no `nodeId` back-pointer: the node already lists it (§2.2a). */
78
+ export type MappingFilter = {
79
+ fieldId: string
80
+ operator: string
81
+ value?: DocScalar
82
+ }
83
+
84
+ export type QnrTemplateDocument = {
85
+ documentId: string
86
+ revision: number
87
+ meta?: Record<string, unknown>
88
+ questionsById?: Record<QuestionId, QnrQuestion>
89
+ questionOrder: QuestionId[]
90
+ gridRowsById?: Record<RowId, QnrGridRow>
91
+ gridRowOrderByQuestionId?: Record<QuestionId, RowId[]>
92
+ alternativesById?: Record<AltId, QnrAlternative>
93
+ alternativeOrderByQuestionId?: Record<QuestionId, AltId[]>
94
+ tabsById?: Record<TabId, QnrTab>
95
+ tabOrder?: TabId[]
96
+ actionsById?: Record<ActionId, QnrAction>
97
+ dataMappingsById?: Record<MappingId, { sourceId: string; rootNodeId: NodeId; bindingOrder?: BindingId[] }>
98
+ mappingNodesById?: Record<NodeId, MappingNode>
99
+ mappingBindingsById?: Record<BindingId, MappingBinding>
100
+ mappingFiltersById?: Record<FilterId, MappingFilter>
101
+ }
102
+
103
+ /**
104
+ * A new family's document. `questionOrder` is present-and-empty rather than absent
105
+ * because it is the document's only structural array and every reader indexes it; an
106
+ * absent order array would make "no questions yet" and "malformed" the same shape.
107
+ */
108
+ export const emptyTemplateDocument = (documentId: string): QnrTemplateDocument => ({
109
+ documentId,
110
+ revision: 0,
111
+ questionOrder: [],
112
+ })