@jsb188/mday 1.1.2

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 (202) hide show
  1. package/package.json +142 -0
  2. package/src/constants/agentMemory.ts +166 -0
  3. package/src/constants/agentNote.ts +212 -0
  4. package/src/constants/aiChat.ts +10 -0
  5. package/src/constants/billing.ts +126 -0
  6. package/src/constants/campaign.ts +236 -0
  7. package/src/constants/contact.ts +72 -0
  8. package/src/constants/csvExport.ts +133 -0
  9. package/src/constants/dataTable.ts +22 -0
  10. package/src/constants/dataTableFieldTypes.generated.ts +59 -0
  11. package/src/constants/docAccess.ts +33 -0
  12. package/src/constants/dt.ts +234 -0
  13. package/src/constants/email.ts +146 -0
  14. package/src/constants/fieldDoc.ts +104 -0
  15. package/src/constants/graphqlReactivity.ts +196 -0
  16. package/src/constants/inboundContact.ts +8 -0
  17. package/src/constants/integration.ts +13 -0
  18. package/src/constants/log.ts +21 -0
  19. package/src/constants/logDisplay/README.md +88 -0
  20. package/src/constants/logDisplay/defaults.ts +39 -0
  21. package/src/constants/logDisplay/index.ts +27 -0
  22. package/src/constants/logDisplay/operations/arable.ts +16 -0
  23. package/src/constants/logDisplay/operations/farmersMarket.ts +10 -0
  24. package/src/constants/logDisplay/operations/foodManufacturing.ts +13 -0
  25. package/src/constants/logDisplay/shared/accounting.ts +13 -0
  26. package/src/constants/logDisplay/shared/interactions.ts +13 -0
  27. package/src/constants/logDisplay/shared/rackTemperatures.ts +42 -0
  28. package/src/constants/logTaxonomy.generated.ts +2199 -0
  29. package/src/constants/noteDoc.ts +77 -0
  30. package/src/constants/organization.ts +60 -0
  31. package/src/constants/pageRoute.ts +23 -0
  32. package/src/constants/qrCode.ts +30 -0
  33. package/src/constants/sheet.ts +132 -0
  34. package/src/constants/sms.ts +18 -0
  35. package/src/constants/square.ts +16 -0
  36. package/src/constants/storage.ts +85 -0
  37. package/src/constants/usage.ts +82 -0
  38. package/src/constants/workflowSteps.ts +60 -0
  39. package/src/schemas/editLogEntry.ts +879 -0
  40. package/src/schemas/inboundContact.ts +77 -0
  41. package/src/schemas/logsQuery.ts +93 -0
  42. package/src/types/account.d.ts +81 -0
  43. package/src/types/agentMemory.d.ts +100 -0
  44. package/src/types/agentNote.d.ts +441 -0
  45. package/src/types/billing.d.ts +537 -0
  46. package/src/types/campaign.d.ts +1533 -0
  47. package/src/types/contact.d.ts +15 -0
  48. package/src/types/csvExport.d.ts +213 -0
  49. package/src/types/dataAnalysis.d.ts +44 -0
  50. package/src/types/dataTable.d.ts +159 -0
  51. package/src/types/definitionElement.d.ts +35 -0
  52. package/src/types/docAccess.d.ts +96 -0
  53. package/src/types/dt.d.ts +257 -0
  54. package/src/types/email.d.ts +231 -0
  55. package/src/types/emailReplyReport.d.ts +76 -0
  56. package/src/types/fieldDoc.d.ts +702 -0
  57. package/src/types/fieldDocContract.d.ts +90 -0
  58. package/src/types/inboundContact.d.ts +65 -0
  59. package/src/types/integration.d.ts +42 -0
  60. package/src/types/log.d.ts +338 -0
  61. package/src/types/logDisplay.types.ts +85 -0
  62. package/src/types/organization.d.ts +350 -0
  63. package/src/types/pageRoute.d.ts +48 -0
  64. package/src/types/qrCode.d.ts +96 -0
  65. package/src/types/richText.d.ts +81 -0
  66. package/src/types/sheet.d.ts +847 -0
  67. package/src/types/sms.d.ts +38 -0
  68. package/src/types/storage.d.ts +481 -0
  69. package/src/types/usage.d.ts +106 -0
  70. package/src/types/workflow.d.ts +435 -0
  71. package/src/utils/agentMemory.ts +15 -0
  72. package/src/utils/agentNote.ts +850 -0
  73. package/src/utils/aiChat.ts +43 -0
  74. package/src/utils/aiChatWork.ts +64 -0
  75. package/src/utils/billing.ts +37 -0
  76. package/src/utils/campaign.ts +1173 -0
  77. package/src/utils/campaignAI.ts +72 -0
  78. package/src/utils/campaignAccess.ts +239 -0
  79. package/src/utils/campaignEmail.ts +601 -0
  80. package/src/utils/campaignEmailCompiler.ts +372 -0
  81. package/src/utils/campaignEmailImages.ts +60 -0
  82. package/src/utils/campaignEmailStyles.ts +544 -0
  83. package/src/utils/campaignLifecycle.ts +104 -0
  84. package/src/utils/campaignPreview.ts +285 -0
  85. package/src/utils/contact.ts +50 -0
  86. package/src/utils/csvExport.ts +19 -0
  87. package/src/utils/dataTable.ts +311 -0
  88. package/src/utils/dataTableFieldTypeRegistry.ts +106 -0
  89. package/src/utils/dataTableRelativeDate.ts +447 -0
  90. package/src/utils/definitionElement.ts +84 -0
  91. package/src/utils/docAccess.ts +383 -0
  92. package/src/utils/documentName.ts +53 -0
  93. package/src/utils/email.ts +379 -0
  94. package/src/utils/emailReplyReport.test.ts +187 -0
  95. package/src/utils/emailReplyReport.ts +143 -0
  96. package/src/utils/fieldDoc.ts +157 -0
  97. package/src/utils/fieldDocAnswers.ts +128 -0
  98. package/src/utils/fieldDocDefaults.ts +36 -0
  99. package/src/utils/fieldDocProgram.ts +36 -0
  100. package/src/utils/fieldDocSummary.ts +30 -0
  101. package/src/utils/log.ts +357 -0
  102. package/src/utils/logDisplay.helpers.ts +177 -0
  103. package/src/utils/logDisplay.ts +87 -0
  104. package/src/utils/logRegistryValue.ts +90 -0
  105. package/src/utils/logStoreRead.ts +262 -0
  106. package/src/utils/logTaxonomy.helpers.ts +17 -0
  107. package/src/utils/logTaxonomy.ts +76 -0
  108. package/src/utils/logTaxonomyDisplay.ts +395 -0
  109. package/src/utils/noteDoc.ts +609 -0
  110. package/src/utils/organization.ts +669 -0
  111. package/src/utils/pageRoute.ts +71 -0
  112. package/src/utils/qrCode.helpers.ts +189 -0
  113. package/src/utils/qrCode.test.ts +519 -0
  114. package/src/utils/qrCode.ts +273 -0
  115. package/src/utils/richTextHTML.ts +385 -0
  116. package/src/utils/sheet.ts +262 -0
  117. package/src/utils/sheetAxisMove.ts +616 -0
  118. package/src/utils/sheetBootstrap.ts +1402 -0
  119. package/src/utils/sheetCellLiteral.ts +101 -0
  120. package/src/utils/sheetCommand.ts +358 -0
  121. package/src/utils/sheetDesign.ts +705 -0
  122. package/src/utils/sheetExport.ts +186 -0
  123. package/src/utils/sheetFeatureReference.ts +183 -0
  124. package/src/utils/sheetFormulaArgs.ts +168 -0
  125. package/src/utils/sheetFormulaCompile.ts +475 -0
  126. package/src/utils/sheetFormulaConditionalAggregate.ts +254 -0
  127. package/src/utils/sheetFormulaCriteria.ts +656 -0
  128. package/src/utils/sheetFormulaDate.ts +444 -0
  129. package/src/utils/sheetFormulaEvaluation.ts +899 -0
  130. package/src/utils/sheetFormulaEvaluator.ts +997 -0
  131. package/src/utils/sheetFormulaFunctionHandlers.ts +403 -0
  132. package/src/utils/sheetFormulaLogical.ts +116 -0
  133. package/src/utils/sheetFormulaLookup.ts +488 -0
  134. package/src/utils/sheetFormulaMath.ts +48 -0
  135. package/src/utils/sheetFormulaParser.ts +729 -0
  136. package/src/utils/sheetFormulaRange.ts +330 -0
  137. package/src/utils/sheetFormulaReference.ts +703 -0
  138. package/src/utils/sheetFormulaReferenceAggregates.ts +313 -0
  139. package/src/utils/sheetFormulaReferenceDates.ts +237 -0
  140. package/src/utils/sheetFormulaReferenceLogical.ts +330 -0
  141. package/src/utils/sheetFormulaReferenceLookup.ts +341 -0
  142. package/src/utils/sheetFormulaReferenceMath.ts +95 -0
  143. package/src/utils/sheetFormulaReferenceRewrite.ts +220 -0
  144. package/src/utils/sheetFormulaReferenceText.ts +462 -0
  145. package/src/utils/sheetFormulaSyntax.ts +1027 -0
  146. package/src/utils/sheetFormulaText.ts +430 -0
  147. package/src/utils/sheetJournal.ts +200 -0
  148. package/src/utils/sheetNumberFormat.ts +1012 -0
  149. package/src/utils/sheetPreview.ts +68 -0
  150. package/src/utils/sheetSequence.ts +118 -0
  151. package/src/utils/sheetSnapshot.ts +739 -0
  152. package/src/utils/sheetStructureEdit.ts +914 -0
  153. package/src/utils/sheetStructureReferences.ts +389 -0
  154. package/src/utils/square.ts +32 -0
  155. package/src/utils/storage.ts +198 -0
  156. package/src/utils/table-route.ts +262 -0
  157. package/src/utils/time.ts +12 -0
  158. package/src/utils/workflow.ts +506 -0
  159. package/src/utils/workflowLogFingerprint.ts +302 -0
  160. package/src/utils/workflowLogRevisionSnapshot.ts +424 -0
  161. package/src/utils/workflowProgress.ts +280 -0
  162. package/src/utils/workflowTriggers.ts +61 -0
  163. package/tests/agentNote.test.ts +395 -0
  164. package/tests/campaignAI.test.ts +119 -0
  165. package/tests/campaignAccess.test.ts +239 -0
  166. package/tests/campaignEmail.test.ts +441 -0
  167. package/tests/campaignLifecycle.test.ts +134 -0
  168. package/tests/campaignPublication.test.ts +309 -0
  169. package/tests/dataTableFieldTypeRegistry.test.ts +111 -0
  170. package/tests/dataTableRelativeDate.test.ts +412 -0
  171. package/tests/docAccess.test.ts +525 -0
  172. package/tests/editLogEntry.test.ts +112 -0
  173. package/tests/email.test.ts +39 -0
  174. package/tests/fieldDocProgram.test.ts +114 -0
  175. package/tests/logDisplay.fixtures.ts +67 -0
  176. package/tests/logDisplay.test.ts +227 -0
  177. package/tests/noteDoc.test.ts +514 -0
  178. package/tests/richTextHTML.test.ts +185 -0
  179. package/tests/sheetAxisMove.test.ts +352 -0
  180. package/tests/sheetBootstrap.test.ts +1967 -0
  181. package/tests/sheetFeatureReference.test.ts +22 -0
  182. package/tests/sheetFormulaCriteria.test.ts +269 -0
  183. package/tests/sheetFormulaEvaluator.test.ts +468 -0
  184. package/tests/sheetFormulaFunctions.test.ts +538 -0
  185. package/tests/sheetFormulaLanguage.test.ts +361 -0
  186. package/tests/sheetFormulaParity.fixtures.ts +872 -0
  187. package/tests/sheetFormulaParity.test.ts +188 -0
  188. package/tests/sheetFormulaRange.test.ts +167 -0
  189. package/tests/sheetFormulaReference.test.ts +78 -0
  190. package/tests/sheetFormulaReferenceRewrite.test.ts +117 -0
  191. package/tests/sheetFormulaTextDate.test.ts +512 -0
  192. package/tests/sheetSequence.test.ts +22 -0
  193. package/tests/sheetSnapshot.test.ts +134 -0
  194. package/tests/sheetStructureEdit.test.ts +537 -0
  195. package/tests/sheetStructureReferences.test.ts +589 -0
  196. package/tests/workflowLifecycleStatus.test.ts +60 -0
  197. package/tests/workflowLogFingerprint.test.ts +203 -0
  198. package/tests/workflowLogRevisionSnapshot.test.ts +96 -0
  199. package/tests/workflowProgress.test.ts +180 -0
  200. package/tests/workflowTriggers.test.ts +29 -0
  201. package/tsconfig.json +12 -0
  202. package/vitest.config.ts +8 -0
@@ -0,0 +1,101 @@
1
+ import { SHEET_CELL_TEXT_MAX_CHARS } from '../constants/sheet.ts';
2
+ import type { SheetCellLiteralType } from '../types/sheet.d.ts';
3
+
4
+ export const SHEET_CELL_LITERAL_TYPES: readonly SheetCellLiteralType[] = ['TEXT', 'NUMBER', 'BOOLEAN', 'DATE', 'DATETIME'];
5
+
6
+ /*
7
+ * Recognize the explicit scalar types without guessing from the authored value.
8
+ */
9
+ export function isSheetCellLiteralType(value: unknown): value is SheetCellLiteralType {
10
+ return SHEET_CELL_LITERAL_TYPES.includes(value as SheetCellLiteralType);
11
+ }
12
+
13
+ /*
14
+ * Build exact scalar storage fields shared by the local ledger and the server.
15
+ * A null result represents invalid input, while an empty input is a valid cleared value.
16
+ */
17
+ export function getSheetCellLiteralFields(rawInput: string | null | undefined, literalType: SheetCellLiteralType) {
18
+ if (!isSheetCellLiteralType(literalType) || (rawInput != null && typeof rawInput !== 'string')) {
19
+ return null;
20
+ }
21
+ const empty = {
22
+ rawInput: null as string | null,
23
+ value: null as string | number | boolean | null,
24
+ textValue: null as string | null,
25
+ numberValue: null as number | null,
26
+ booleanValue: null as boolean | null,
27
+ dateValue: null as string | null,
28
+ datetimeValue: null as string | null,
29
+ formulaText: null,
30
+ };
31
+ if (rawInput == null || rawInput === '') {
32
+ return empty;
33
+ }
34
+ if (rawInput.length > SHEET_CELL_TEXT_MAX_CHARS) {
35
+ return null;
36
+ }
37
+ if (literalType === 'TEXT') {
38
+ return { ...empty, rawInput, value: rawInput, textValue: rawInput };
39
+ }
40
+ const trimmed = rawInput.trim();
41
+ if (literalType === 'NUMBER') {
42
+ if (!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i.test(trimmed)) {
43
+ return null;
44
+ }
45
+ const numberValue = Number(trimmed);
46
+ return Number.isFinite(numberValue)
47
+ ? {
48
+ ...empty,
49
+ rawInput: String(numberValue),
50
+ value: numberValue,
51
+ numberValue,
52
+ }
53
+ : null;
54
+ }
55
+ if (literalType === 'BOOLEAN') {
56
+ if (!/^(true|false)$/i.test(trimmed)) {
57
+ return null;
58
+ }
59
+ const booleanValue = trimmed.toLowerCase() === 'true';
60
+ return {
61
+ ...empty,
62
+ rawInput: booleanValue ? 'TRUE' : 'FALSE',
63
+ value: booleanValue,
64
+ booleanValue,
65
+ };
66
+ }
67
+ if (literalType === 'DATE') {
68
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(trimmed)) {
69
+ return null;
70
+ }
71
+ const date = new Date(`${trimmed}T00:00:00.000Z`);
72
+ return Number.isFinite(date.getTime()) && date.toISOString().slice(0, 10) === trimmed
73
+ ? { ...empty, rawInput: trimmed, value: trimmed, dateValue: trimmed }
74
+ : null;
75
+ }
76
+ if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})$/i.test(trimmed)) {
77
+ return null;
78
+ }
79
+ const calendarDate = trimmed.slice(0, 10);
80
+ const dateParts = new Date(`${calendarDate}T00:00:00.000Z`);
81
+ const timeParts = trimmed.slice(11).match(/^(\d{2}):(\d{2})(?::(\d{2}))?/);
82
+ const date = new Date(trimmed);
83
+ if (
84
+ !Number.isFinite(date.getTime()) ||
85
+ !Number.isFinite(dateParts.getTime()) ||
86
+ dateParts.toISOString().slice(0, 10) !== calendarDate ||
87
+ !timeParts ||
88
+ Number(timeParts[1]) > 23 ||
89
+ Number(timeParts[2]) > 59 ||
90
+ Number(timeParts[3] || 0) > 59
91
+ ) {
92
+ return null;
93
+ }
94
+ const datetimeValue = date.toISOString();
95
+ return {
96
+ ...empty,
97
+ rawInput: datetimeValue,
98
+ value: datetimeValue,
99
+ datetimeValue,
100
+ };
101
+ }
@@ -0,0 +1,358 @@
1
+ import type {
2
+ SheetCommandResultGQL,
3
+ SheetCommandTabRevisionGQL,
4
+ SheetDesignCommandIntent,
5
+ SheetDesignObj,
6
+ SheetDesignOperationTargetType,
7
+ SheetDesignPatchInput,
8
+ SheetNamedRangeObj,
9
+ } from '../types/sheet.d.ts';
10
+
11
+ type SheetDesignRecord = Record<string, unknown>;
12
+
13
+ /*
14
+ * Return whether a value is a plain design record rather than an array.
15
+ */
16
+ function isSheetDesignRecord(value: unknown): value is SheetDesignRecord {
17
+ return Boolean(value && typeof value === 'object' && !Array.isArray(value));
18
+ }
19
+
20
+ /*
21
+ * Parse one whole-map UI design field into a plain record.
22
+ */
23
+ function parseSheetDesignPatchRecord(value: string | null | undefined) {
24
+ if (typeof value !== 'string' || !value.trim()) {
25
+ return null;
26
+ }
27
+
28
+ try {
29
+ const parsed = JSON.parse(value);
30
+ return isSheetDesignRecord(parsed) ? parsed : null;
31
+ } catch {
32
+ return null;
33
+ }
34
+ }
35
+
36
+ /*
37
+ * Return one deterministic JSON signature for semantic design comparisons.
38
+ */
39
+ function getSheetDesignCommandValueSignature(value: unknown): string {
40
+ if (Array.isArray(value)) {
41
+ return `[${value.map(getSheetDesignCommandValueSignature).join(',')}]`;
42
+ }
43
+ if (isSheetDesignRecord(value)) {
44
+ return `{${Object.keys(value)
45
+ .sort()
46
+ .filter((key) => value[key] !== undefined)
47
+ .map((key) => `${JSON.stringify(key)}:${getSheetDesignCommandValueSignature(value[key])}`)
48
+ .join(',')}}`;
49
+ }
50
+
51
+ return JSON.stringify(value ?? null);
52
+ }
53
+
54
+ /*
55
+ * Return whether two design values have the same JSON semantics.
56
+ */
57
+ function sheetDesignCommandValuesAreEqual(a: unknown, b: unknown) {
58
+ return getSheetDesignCommandValueSignature(a) === getSheetDesignCommandValueSignature(b);
59
+ }
60
+
61
+ /*
62
+ * Append property-level intents that transform one keyed design record into another.
63
+ */
64
+ function appendSheetDesignRecordPropertyIntents(
65
+ operations: SheetDesignCommandIntent[],
66
+ params: {
67
+ before?: SheetDesignRecord | null;
68
+ after?: SheetDesignRecord | null;
69
+ targetKey: string;
70
+ targetType: SheetDesignOperationTargetType;
71
+ },
72
+ ) {
73
+ const before = params.before || {};
74
+ const after = params.after || {};
75
+ const propertyKeys = new Set([...Object.keys(before), ...Object.keys(after)]);
76
+
77
+ [...propertyKeys].sort().forEach((property) => {
78
+ const beforeValue = before[property];
79
+ const afterValue = after[property];
80
+ if (sheetDesignCommandValuesAreEqual(beforeValue, afterValue)) {
81
+ return;
82
+ }
83
+
84
+ operations.push({
85
+ operation: afterValue === undefined || afterValue === null ? 'DELETE_PROPERTY' : 'SET_PROPERTY',
86
+ property,
87
+ targetKey: params.targetKey,
88
+ targetType: params.targetType,
89
+ ...(afterValue === undefined || afterValue === null ? {} : { value: afterValue }),
90
+ });
91
+ });
92
+ }
93
+
94
+ /*
95
+ * Append keyed row or column operations for one whole-map UI patch.
96
+ */
97
+ function appendSheetDesignAxisIntents(
98
+ operations: SheetDesignCommandIntent[],
99
+ params: {
100
+ before?: Record<string, SheetDesignRecord> | null;
101
+ after: SheetDesignRecord;
102
+ targetType: 'COLUMN' | 'ROW';
103
+ },
104
+ ) {
105
+ const before = params.before || {};
106
+ const axisKeys = new Set([...Object.keys(before), ...Object.keys(params.after)]);
107
+
108
+ [...axisKeys]
109
+ .sort((a, b) => Number(a) - Number(b))
110
+ .forEach((targetKey) => {
111
+ const beforeEntity = before[targetKey];
112
+ const afterValue = params.after[targetKey];
113
+ const afterEntity = isSheetDesignRecord(afterValue) ? afterValue : null;
114
+ if (!/^[1-9]\d*$/.test(targetKey) || (afterValue !== undefined && !afterEntity)) {
115
+ throw new Error('INVALID_SHEET_DESIGN_AXIS_PATCH');
116
+ }
117
+ if (afterValue === undefined) {
118
+ if (beforeEntity) {
119
+ operations.push({
120
+ operation: 'DELETE_ENTITY',
121
+ targetKey,
122
+ targetType: params.targetType,
123
+ });
124
+ }
125
+ return;
126
+ }
127
+
128
+ appendSheetDesignRecordPropertyIntents(operations, {
129
+ after: afterEntity,
130
+ before: beforeEntity,
131
+ targetKey,
132
+ targetType: params.targetType,
133
+ });
134
+ });
135
+ }
136
+
137
+ /*
138
+ * Return named ranges indexed by their stable name key.
139
+ */
140
+ function getSheetNamedRangesByName(ranges: SheetNamedRangeObj[] | null | undefined) {
141
+ const byName = new Map<string, SheetNamedRangeObj>();
142
+ for (const range of ranges || []) {
143
+ const name = String(range?.name || '').trim();
144
+ if (!name || byName.has(name)) {
145
+ throw new Error('INVALID_SHEET_NAMED_RANGE_PATCH');
146
+ }
147
+ byName.set(name, range);
148
+ }
149
+ return byName;
150
+ }
151
+
152
+ /*
153
+ * Append whole-entity named-range operations keyed by range name.
154
+ */
155
+ function appendSheetNamedRangeIntents(
156
+ operations: SheetDesignCommandIntent[],
157
+ beforeRanges: SheetNamedRangeObj[] | null | undefined,
158
+ afterRanges: SheetNamedRangeObj[],
159
+ ) {
160
+ const before = getSheetNamedRangesByName(beforeRanges);
161
+ const after = getSheetNamedRangesByName(afterRanges);
162
+ const names = new Set([...before.keys(), ...after.keys()]);
163
+
164
+ [...names].sort().forEach((targetKey) => {
165
+ const beforeRange = before.get(targetKey);
166
+ const afterRange = after.get(targetKey);
167
+ if (sheetDesignCommandValuesAreEqual(beforeRange, afterRange)) {
168
+ return;
169
+ }
170
+ operations.push({
171
+ operation: afterRange ? 'UPSERT_ENTITY' : 'DELETE_ENTITY',
172
+ targetKey,
173
+ targetType: 'NAMED_RANGE',
174
+ ...(afterRange ? { value: afterRange } : {}),
175
+ });
176
+ });
177
+ }
178
+
179
+ /*
180
+ * Create one opaque id at the user-gesture boundary and keep it through retries.
181
+ */
182
+ export function createSheetCommandId() {
183
+ return globalThis.crypto?.randomUUID?.() || `sc-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
184
+ }
185
+
186
+ /*
187
+ * Return the stable scope used by tab-local sessions, queues, and history.
188
+ */
189
+ export function getSheetCommandScopeKey(sheetId: string, sheetTabId: string) {
190
+ return `sheet:${String(sheetId)}:tab:${String(sheetTabId)}`;
191
+ }
192
+
193
+ /*
194
+ * Return the canonical conflict key for one property-level design target.
195
+ */
196
+ export function getSheetDesignOperationTargetKey(
197
+ targetType: SheetDesignOperationTargetType,
198
+ targetKey: string | number,
199
+ property?: string | null,
200
+ ) {
201
+ return [targetType, String(targetKey).trim(), property?.trim() || '*'].join(':');
202
+ }
203
+
204
+ /*
205
+ * Return one intent with normalized string keys and no client-authored before state.
206
+ */
207
+ export function normalizeSheetDesignCommandIntent(intent: SheetDesignCommandIntent): SheetDesignCommandIntent {
208
+ return {
209
+ operation: intent.operation,
210
+ property: intent.property?.trim() || null,
211
+ targetKey: String(intent.targetKey).trim(),
212
+ targetType: intent.targetType,
213
+ ...(intent.operation === 'DELETE_PROPERTY' || intent.operation === 'DELETE_ENTITY' ? {} : { value: intent.value ?? null }),
214
+ };
215
+ }
216
+
217
+ /*
218
+ * Return whether a whole-map UI design patch can be represented by keyed
219
+ * design commands. Grid geometry stays on the structure-command path.
220
+ */
221
+ export function sheetDesignPatchSupportsDurableCommand(patch: SheetDesignPatchInput) {
222
+ if (patch.grid && (Object.hasOwn(patch.grid, 'rowCount') || Object.hasOwn(patch.grid, 'columnCount'))) {
223
+ return false;
224
+ }
225
+
226
+ for (const value of [patch.columns, patch.defaultCellFormat, patch.metadata, patch.rows]) {
227
+ if (value !== undefined && value !== null && !parseSheetDesignPatchRecord(value)) {
228
+ return false;
229
+ }
230
+ }
231
+
232
+ try {
233
+ if (patch.namedRanges) {
234
+ getSheetNamedRangesByName(patch.namedRanges);
235
+ }
236
+ } catch {
237
+ return false;
238
+ }
239
+
240
+ return true;
241
+ }
242
+
243
+ /*
244
+ * Translate one whole-map UI design patch into property-keyed client
245
+ * intent. The supplied current design is used only to identify touched keys;
246
+ * the server still captures every authoritative before-state itself.
247
+ */
248
+ export function getSheetDesignCommandIntentsFromPatch(
249
+ currentDesign: SheetDesignObj,
250
+ patch: SheetDesignPatchInput,
251
+ ): SheetDesignCommandIntent[] | null {
252
+ if (!sheetDesignPatchSupportsDurableCommand(patch)) {
253
+ return null;
254
+ }
255
+
256
+ try {
257
+ const operations: SheetDesignCommandIntent[] = [];
258
+ if (typeof patch.columns === 'string') {
259
+ appendSheetDesignAxisIntents(operations, {
260
+ after: parseSheetDesignPatchRecord(patch.columns) || {},
261
+ before: currentDesign.columns as Record<string, SheetDesignRecord>,
262
+ targetType: 'COLUMN',
263
+ });
264
+ }
265
+ if (typeof patch.rows === 'string') {
266
+ appendSheetDesignAxisIntents(operations, {
267
+ after: parseSheetDesignPatchRecord(patch.rows) || {},
268
+ before: currentDesign.rows as Record<string, SheetDesignRecord>,
269
+ targetType: 'ROW',
270
+ });
271
+ }
272
+ if (patch.grid) {
273
+ for (const property of ['frozenColumns', 'frozenRows'] as const) {
274
+ if (
275
+ Object.hasOwn(patch.grid, property) &&
276
+ !sheetDesignCommandValuesAreEqual(currentDesign.grid[property], patch.grid[property])
277
+ ) {
278
+ operations.push({
279
+ operation: 'SET_PROPERTY',
280
+ property,
281
+ targetKey: 'grid',
282
+ targetType: 'GRID',
283
+ value: patch.grid[property] ?? 0,
284
+ });
285
+ }
286
+ }
287
+ }
288
+ if (patch.defaultCellStyle !== undefined) {
289
+ appendSheetDesignRecordPropertyIntents(operations, {
290
+ after: (patch.defaultCellStyle || {}) as SheetDesignRecord,
291
+ before: (currentDesign.defaultCellStyle || {}) as SheetDesignRecord,
292
+ targetKey: 'default',
293
+ targetType: 'DEFAULT_CELL_STYLE',
294
+ });
295
+ }
296
+ if (typeof patch.defaultCellFormat === 'string') {
297
+ appendSheetDesignRecordPropertyIntents(operations, {
298
+ after: parseSheetDesignPatchRecord(patch.defaultCellFormat) || {},
299
+ before: (currentDesign.defaultCellFormat || {}) as SheetDesignRecord,
300
+ targetKey: 'default',
301
+ targetType: 'DEFAULT_CELL_FORMAT',
302
+ });
303
+ }
304
+ if (typeof patch.metadata === 'string') {
305
+ appendSheetDesignRecordPropertyIntents(operations, {
306
+ after: parseSheetDesignPatchRecord(patch.metadata) || {},
307
+ before: currentDesign.metadata || {},
308
+ targetKey: 'metadata',
309
+ targetType: 'METADATA',
310
+ });
311
+ }
312
+ if (patch.namedRanges) {
313
+ appendSheetNamedRangeIntents(operations, currentDesign.namedRanges, patch.namedRanges);
314
+ }
315
+
316
+ return operations.map(normalizeSheetDesignCommandIntent);
317
+ } catch {
318
+ return null;
319
+ }
320
+ }
321
+
322
+ /*
323
+ * Serialize intent values for the GraphQL String boundary without adding any
324
+ * client-authored before-state.
325
+ */
326
+ export function serializeSheetDesignCommandIntents(intents: SheetDesignCommandIntent[]) {
327
+ return intents.map((intent) => {
328
+ const normalized = normalizeSheetDesignCommandIntent(intent);
329
+ const { value, ...withoutValue } = normalized;
330
+ return intent.operation === 'DELETE_ENTITY' || intent.operation === 'DELETE_PROPERTY'
331
+ ? withoutValue
332
+ : { ...withoutValue, value: JSON.stringify(value ?? null) };
333
+ });
334
+ }
335
+
336
+ /*
337
+ * Return the revision allocation for one tab in a command result.
338
+ */
339
+ export function getSheetCommandTabRevision(
340
+ result: SheetCommandResultGQL | null | undefined,
341
+ sheetTabId: string,
342
+ ): SheetCommandTabRevisionGQL | null {
343
+ return result?.tabRevisions?.find((revision) => String(revision.sheetTabId) === String(sheetTabId)) || null;
344
+ }
345
+
346
+ /*
347
+ * Return whether authority accepted or idempotently replayed the command.
348
+ */
349
+ export function sheetCommandResultCommitted(result: SheetCommandResultGQL | null | undefined) {
350
+ return result?.commandStatus === 'COMMITTED' || result?.commandStatus === 'REPLAYED';
351
+ }
352
+
353
+ /*
354
+ * Return whether authority rejected the command due to an optimistic conflict.
355
+ */
356
+ export function sheetCommandResultConflicted(result: SheetCommandResultGQL | null | undefined) {
357
+ return result?.commandStatus === 'CONFLICTED' || Boolean(result?.conflict);
358
+ }