asma-core-helpers 0.16.0 → 0.17.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,73 @@
1
+ import type { AltId, BindingId, BindingTarget, DocScalar, FilterId, NodeId, QuestionId } from './templateDocument.js'
2
+
3
+ /**
4
+ * The authoring op vocabulary. `entity.action`, stable-id targets only, append-only —
5
+ * a new op is a new member, never a changed one.
6
+ *
7
+ * `null` in an op payload is the **explicit unset** the way JSON-merge-patch uses it.
8
+ * DOC-LAW-2 bans `null` in the stored document, not in the op that produces it: the op
9
+ * layer is the only place the absent-vs-cleared tri-state survives.
10
+ *
11
+ * @see _docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:380
12
+ * @see _docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:195 (DOC-LAW-2 tri-state)
13
+ */
14
+
15
+ /** A value an op may write. `null` means unset. */
16
+ export type OpValue = DocScalar | null
17
+
18
+ export type TemplateOp =
19
+ | { type: 'template.updateMeta'; patch: Record<string, OpValue | Record<string, unknown>> }
20
+ | { type: 'question.create'; questionId: QuestionId; kind: string; atIndex?: number }
21
+ | { type: 'question.updateField'; questionId: QuestionId; field: string; value: OpValue }
22
+ | { type: 'question.move'; questionId: QuestionId; toIndex: number }
23
+ | { type: 'question.delete'; questionId: QuestionId }
24
+ | { type: 'alternative.create'; questionId: QuestionId; alternativeId: AltId; label?: string; atIndex?: number }
25
+ | { type: 'alternative.delete'; questionId: QuestionId; alternativeId: AltId }
26
+ | { type: 'mappingNode.create'; nodeId: NodeId; entityId: string; parentNodeId?: NodeId; relationshipId?: string }
27
+ | { type: 'mappingNode.delete'; nodeId: NodeId }
28
+ | {
29
+ type: 'mappingBinding.create'
30
+ bindingId: BindingId
31
+ nodeId: NodeId
32
+ fieldId: string
33
+ target: BindingTarget
34
+ }
35
+ | {
36
+ type: 'mappingBinding.update'
37
+ bindingId: BindingId
38
+ patch: { nodeId?: NodeId; fieldId?: string; target?: BindingTarget }
39
+ }
40
+ | { type: 'mappingBinding.delete'; bindingId: BindingId }
41
+ | {
42
+ type: 'mappingFilter.set'
43
+ filterId: FilterId
44
+ nodeId: NodeId
45
+ fieldId: string
46
+ operator: string
47
+ value?: DocScalar
48
+ }
49
+ | { type: 'mappingFilter.delete'; filterId: FilterId }
50
+
51
+ export type TemplateOpType = TemplateOp['type']
52
+
53
+ /**
54
+ * The op types this reducer implements. The vocabulary in the architecture doc is
55
+ * larger (grid rows, tabs, actions, layout, visibility/highlight rules); those slot
56
+ * into the same registry without changing the envelope.
57
+ */
58
+ export const IMPLEMENTED_OP_TYPES = [
59
+ 'template.updateMeta',
60
+ 'question.create',
61
+ 'question.updateField',
62
+ 'question.move',
63
+ 'question.delete',
64
+ 'alternative.create',
65
+ 'alternative.delete',
66
+ 'mappingNode.create',
67
+ 'mappingNode.delete',
68
+ 'mappingBinding.create',
69
+ 'mappingBinding.update',
70
+ 'mappingBinding.delete',
71
+ 'mappingFilter.set',
72
+ 'mappingFilter.delete',
73
+ ] as const satisfies readonly TemplateOpType[]
@@ -0,0 +1,103 @@
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
+ export type QuestionId = string
16
+ export type RowId = string
17
+ export type AltId = string
18
+ export type TabId = string
19
+ export type ActionId = string
20
+ export type NodeId = string
21
+ export type BindingId = string
22
+ export type FilterId = string
23
+ export type MappingId = string
24
+
25
+ /** A scalar a document may carry. Deliberately not `any`: DOC-LAW-2 bans `null`. */
26
+ export type DocScalar = string | number | boolean
27
+
28
+ export type QnrQuestion = {
29
+ kind: string
30
+ label?: string
31
+ required?: boolean
32
+ defaultValue?: DocScalar
33
+ [key: string]: unknown
34
+ }
35
+
36
+ export type QnrAlternative = { label?: string; value?: DocScalar; [key: string]: unknown }
37
+ export type QnrGridRow = { label?: string; [key: string]: unknown }
38
+ export type QnrTab = { label?: string; [key: string]: unknown }
39
+ export type QnrAction = { kind?: string; [key: string]: unknown }
40
+
41
+ /** One traversal step. Many questions off one entity share a single node (§2.2a). */
42
+ export type MappingNode = {
43
+ entityId: string
44
+ parentNodeId?: NodeId
45
+ relationshipId?: string
46
+ filterOrder?: FilterId[]
47
+ [key: string]: unknown
48
+ }
49
+
50
+ /**
51
+ * A binding's target. A `gridColumn` target is a pair, which is why the binding key is
52
+ * a synthetic id rather than the question id (§2.2a).
53
+ */
54
+ export type BindingTarget =
55
+ | { kind: 'question'; questionId: QuestionId }
56
+ | { kind: 'gridColumn'; gridQuestionId: QuestionId; columnQuestionId: QuestionId }
57
+
58
+ export type MappingBinding = {
59
+ nodeId: NodeId
60
+ fieldId: string
61
+ target: BindingTarget
62
+ cardinality?: string
63
+ onMissing?: string
64
+ onMany?: string
65
+ [key: string]: unknown
66
+ }
67
+
68
+ /** Carries no `nodeId` back-pointer: the node already lists it (§2.2a). */
69
+ export type MappingFilter = {
70
+ fieldId: string
71
+ operator: string
72
+ value?: DocScalar
73
+ }
74
+
75
+ export type QnrTemplateDocument = {
76
+ documentId: string
77
+ revision: number
78
+ meta?: Record<string, unknown>
79
+ questionsById?: Record<QuestionId, QnrQuestion>
80
+ questionOrder: QuestionId[]
81
+ gridRowsById?: Record<RowId, QnrGridRow>
82
+ gridRowOrderByQuestionId?: Record<QuestionId, RowId[]>
83
+ alternativesById?: Record<AltId, QnrAlternative>
84
+ alternativeOrderByQuestionId?: Record<QuestionId, AltId[]>
85
+ tabsById?: Record<TabId, QnrTab>
86
+ tabOrder?: TabId[]
87
+ actionsById?: Record<ActionId, QnrAction>
88
+ dataMappingsById?: Record<MappingId, { sourceId: string; rootNodeId: NodeId; bindingOrder?: BindingId[] }>
89
+ mappingNodesById?: Record<NodeId, MappingNode>
90
+ mappingBindingsById?: Record<BindingId, MappingBinding>
91
+ mappingFiltersById?: Record<FilterId, MappingFilter>
92
+ }
93
+
94
+ /**
95
+ * A new family's document. `questionOrder` is present-and-empty rather than absent
96
+ * because it is the document's only structural array and every reader indexes it; an
97
+ * absent order array would make "no questions yet" and "malformed" the same shape.
98
+ */
99
+ export const emptyTemplateDocument = (documentId: string): QnrTemplateDocument => ({
100
+ documentId,
101
+ revision: 0,
102
+ questionOrder: [],
103
+ })