@platforma-sdk/model 1.54.10 → 1.55.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.
Files changed (151) hide show
  1. package/dist/bconfig/normalization.cjs +8 -1
  2. package/dist/bconfig/normalization.cjs.map +1 -1
  3. package/dist/bconfig/normalization.d.ts.map +1 -1
  4. package/dist/bconfig/normalization.js +8 -1
  5. package/dist/bconfig/normalization.js.map +1 -1
  6. package/dist/block_api_v3.d.ts +2 -2
  7. package/dist/block_api_v3.d.ts.map +1 -1
  8. package/dist/block_migrations.cjs +246 -214
  9. package/dist/block_migrations.cjs.map +1 -1
  10. package/dist/block_migrations.d.ts +180 -158
  11. package/dist/block_migrations.d.ts.map +1 -1
  12. package/dist/block_migrations.js +247 -214
  13. package/dist/block_migrations.js.map +1 -1
  14. package/dist/block_model.cjs +85 -35
  15. package/dist/block_model.cjs.map +1 -1
  16. package/dist/block_model.d.ts +66 -38
  17. package/dist/block_model.d.ts.map +1 -1
  18. package/dist/block_model.js +86 -36
  19. package/dist/block_model.js.map +1 -1
  20. package/dist/{builder.cjs → block_model_legacy.cjs} +2 -2
  21. package/dist/block_model_legacy.cjs.map +1 -0
  22. package/dist/{builder.d.ts → block_model_legacy.d.ts} +1 -1
  23. package/dist/block_model_legacy.d.ts.map +1 -0
  24. package/dist/{builder.js → block_model_legacy.js} +2 -2
  25. package/dist/block_model_legacy.js.map +1 -0
  26. package/dist/block_state_patch.d.ts +11 -1
  27. package/dist/block_state_patch.d.ts.map +1 -1
  28. package/dist/block_storage.cjs +126 -109
  29. package/dist/block_storage.cjs.map +1 -1
  30. package/dist/block_storage.d.ts +109 -112
  31. package/dist/block_storage.d.ts.map +1 -1
  32. package/dist/block_storage.js +126 -101
  33. package/dist/block_storage.js.map +1 -1
  34. package/dist/block_storage_callbacks.cjs +227 -0
  35. package/dist/block_storage_callbacks.cjs.map +1 -0
  36. package/dist/block_storage_callbacks.d.ts +113 -0
  37. package/dist/block_storage_callbacks.d.ts.map +1 -0
  38. package/dist/block_storage_callbacks.js +218 -0
  39. package/dist/block_storage_callbacks.js.map +1 -0
  40. package/dist/block_storage_facade.cjs +104 -0
  41. package/dist/block_storage_facade.cjs.map +1 -0
  42. package/dist/block_storage_facade.d.ts +168 -0
  43. package/dist/block_storage_facade.d.ts.map +1 -0
  44. package/dist/block_storage_facade.js +99 -0
  45. package/dist/block_storage_facade.js.map +1 -0
  46. package/dist/components/PlDataTable/state-migration.cjs.map +1 -1
  47. package/dist/components/PlDataTable/state-migration.js.map +1 -1
  48. package/dist/components/PlDataTable/table.cjs +11 -2
  49. package/dist/components/PlDataTable/table.cjs.map +1 -1
  50. package/dist/components/PlDataTable/table.d.ts.map +1 -1
  51. package/dist/components/PlDataTable/table.js +12 -3
  52. package/dist/components/PlDataTable/table.js.map +1 -1
  53. package/dist/components/PlDataTable/v5.d.ts +7 -4
  54. package/dist/components/PlDataTable/v5.d.ts.map +1 -1
  55. package/dist/filters/converters/filterToQuery.cjs +3 -4
  56. package/dist/filters/converters/filterToQuery.cjs.map +1 -1
  57. package/dist/filters/converters/filterToQuery.d.ts +1 -1
  58. package/dist/filters/converters/filterToQuery.d.ts.map +1 -1
  59. package/dist/filters/converters/filterToQuery.js +3 -4
  60. package/dist/filters/converters/filterToQuery.js.map +1 -1
  61. package/dist/filters/distill.cjs.map +1 -1
  62. package/dist/filters/distill.d.ts +3 -2
  63. package/dist/filters/distill.d.ts.map +1 -1
  64. package/dist/filters/distill.js.map +1 -1
  65. package/dist/filters/traverse.cjs +7 -3
  66. package/dist/filters/traverse.cjs.map +1 -1
  67. package/dist/filters/traverse.d.ts +14 -12
  68. package/dist/filters/traverse.d.ts.map +1 -1
  69. package/dist/filters/traverse.js +7 -3
  70. package/dist/filters/traverse.js.map +1 -1
  71. package/dist/index.cjs +13 -14
  72. package/dist/index.cjs.map +1 -1
  73. package/dist/index.d.ts +8 -3
  74. package/dist/index.d.ts.map +1 -1
  75. package/dist/index.js +6 -4
  76. package/dist/index.js.map +1 -1
  77. package/dist/package.json.cjs +1 -1
  78. package/dist/package.json.js +1 -1
  79. package/dist/platforma.d.ts +11 -4
  80. package/dist/platforma.d.ts.map +1 -1
  81. package/dist/plugin_model.cjs +171 -0
  82. package/dist/plugin_model.cjs.map +1 -0
  83. package/dist/plugin_model.d.ts +162 -0
  84. package/dist/plugin_model.d.ts.map +1 -0
  85. package/dist/plugin_model.js +169 -0
  86. package/dist/plugin_model.js.map +1 -0
  87. package/dist/render/api.cjs +20 -21
  88. package/dist/render/api.cjs.map +1 -1
  89. package/dist/render/api.d.ts +8 -8
  90. package/dist/render/api.d.ts.map +1 -1
  91. package/dist/render/api.js +20 -21
  92. package/dist/render/api.js.map +1 -1
  93. package/dist/render/internal.cjs.map +1 -1
  94. package/dist/render/internal.d.ts +1 -1
  95. package/dist/render/internal.d.ts.map +1 -1
  96. package/dist/render/internal.js.map +1 -1
  97. package/dist/version.cjs +4 -0
  98. package/dist/version.cjs.map +1 -1
  99. package/dist/version.d.ts +4 -0
  100. package/dist/version.d.ts.map +1 -1
  101. package/dist/version.js +4 -1
  102. package/dist/version.js.map +1 -1
  103. package/package.json +6 -6
  104. package/src/bconfig/normalization.ts +8 -1
  105. package/src/block_api_v3.ts +2 -2
  106. package/src/block_migrations.test.ts +141 -171
  107. package/src/block_migrations.ts +300 -285
  108. package/src/block_model.ts +205 -95
  109. package/src/{builder.ts → block_model_legacy.ts} +1 -1
  110. package/src/block_state_patch.ts +13 -1
  111. package/src/block_storage.test.ts +283 -95
  112. package/src/block_storage.ts +199 -188
  113. package/src/block_storage_callbacks.ts +326 -0
  114. package/src/block_storage_facade.ts +199 -0
  115. package/src/components/PlDataTable/state-migration.ts +4 -4
  116. package/src/components/PlDataTable/table.ts +16 -3
  117. package/src/components/PlDataTable/v5.ts +9 -5
  118. package/src/filters/converters/filterToQuery.ts +8 -7
  119. package/src/filters/distill.ts +19 -11
  120. package/src/filters/traverse.ts +44 -24
  121. package/src/index.ts +7 -3
  122. package/src/platforma.ts +26 -7
  123. package/src/plugin_model.test.ts +168 -0
  124. package/src/plugin_model.ts +242 -0
  125. package/src/render/api.ts +26 -24
  126. package/src/render/internal.ts +1 -1
  127. package/src/typing.test.ts +1 -1
  128. package/src/version.ts +8 -0
  129. package/dist/block_storage_vm.cjs +0 -262
  130. package/dist/block_storage_vm.cjs.map +0 -1
  131. package/dist/block_storage_vm.d.ts +0 -59
  132. package/dist/block_storage_vm.d.ts.map +0 -1
  133. package/dist/block_storage_vm.js +0 -258
  134. package/dist/block_storage_vm.js.map +0 -1
  135. package/dist/branding.d.ts +0 -7
  136. package/dist/branding.d.ts.map +0 -1
  137. package/dist/builder.cjs.map +0 -1
  138. package/dist/builder.d.ts.map +0 -1
  139. package/dist/builder.js.map +0 -1
  140. package/dist/sdk_info.cjs +0 -10
  141. package/dist/sdk_info.cjs.map +0 -1
  142. package/dist/sdk_info.d.ts +0 -5
  143. package/dist/sdk_info.d.ts.map +0 -1
  144. package/dist/sdk_info.js +0 -8
  145. package/dist/sdk_info.js.map +0 -1
  146. package/dist/unionize.d.ts +0 -12
  147. package/dist/unionize.d.ts.map +0 -1
  148. package/src/block_storage_vm.ts +0 -346
  149. package/src/branding.ts +0 -4
  150. package/src/sdk_info.ts +0 -9
  151. package/src/unionize.ts +0 -12
@@ -0,0 +1,218 @@
1
+ import { BLOCK_STORAGE_SCHEMA_VERSION, migrateBlockStorage, updateStorageData, createBlockStorage, isBlockStorage, normalizeBlockStorage, getStorageData, BLOCK_STORAGE_KEY } from './block_storage.js';
2
+ import { stringifyJson } from '@milaboratories/pl-model-common';
3
+
4
+ /**
5
+ * BlockStorage Callback Implementations - wired to facade callbacks in BlockModelV3._done().
6
+ *
7
+ * Provides pure functions for storage operations (migration, initialization,
8
+ * args derivation, updates, debug views). Each function takes its dependencies
9
+ * explicitly as parameters.
10
+ *
11
+ * @module block_storage_callbacks
12
+ * @internal
13
+ */
14
+ /**
15
+ * Normalizes raw storage data and extracts state.
16
+ * Handles all formats:
17
+ * - New BlockStorage format (has discriminator)
18
+ * - Legacy V1/V2 format ({ args, uiState })
19
+ * - Raw V3 state (any other format)
20
+ *
21
+ * @param rawStorage - Raw data from blockStorage field (may be JSON string or object)
22
+ * @returns Object with normalized storage and extracted state
23
+ */
24
+ function normalizeStorage(rawStorage) {
25
+ // Handle undefined/null
26
+ if (rawStorage === undefined || rawStorage === null) {
27
+ const storage = createBlockStorage({});
28
+ return { storage, data: {} };
29
+ }
30
+ // Parse JSON string if needed
31
+ let parsed = rawStorage;
32
+ if (typeof rawStorage === "string") {
33
+ try {
34
+ parsed = JSON.parse(rawStorage);
35
+ }
36
+ catch {
37
+ // If parsing fails, treat string as the data
38
+ const storage = createBlockStorage(rawStorage);
39
+ return { storage, data: rawStorage };
40
+ }
41
+ }
42
+ // Check for BlockStorage format (has discriminator)
43
+ if (isBlockStorage(parsed)) {
44
+ const storage = normalizeBlockStorage(parsed);
45
+ return { storage, data: getStorageData(storage) };
46
+ }
47
+ // Check for legacy V1/V2 format: { args, uiState }
48
+ if (isLegacyModelV1ApiFormat(parsed)) {
49
+ // For legacy format, the whole object IS the data
50
+ const storage = createBlockStorage(parsed);
51
+ return { storage, data: parsed };
52
+ }
53
+ // Raw V3 data - wrap it
54
+ const storage = createBlockStorage(parsed);
55
+ return { storage, data: parsed };
56
+ }
57
+ /**
58
+ * Applies a state update to existing storage.
59
+ * Used when setData is called from the frontend.
60
+ *
61
+ * @param currentStorageJson - Current storage as JSON string (must be defined)
62
+ * @param payload - Update payload with operation type and value
63
+ * @returns Updated storage as StringifiedJson<BlockStorage>
64
+ */
65
+ function applyStorageUpdate(currentStorageJson, payload) {
66
+ const { storage: currentStorage } = normalizeStorage(currentStorageJson);
67
+ // Update data while preserving other storage fields (version, plugins)
68
+ const updatedStorage = updateStorageData(currentStorage, payload);
69
+ return stringifyJson(updatedStorage);
70
+ }
71
+ /**
72
+ * Checks if data is in legacy Model API v1 format.
73
+ * Legacy format has { args, uiState? } at top level without the BlockStorage discriminator.
74
+ */
75
+ function isLegacyModelV1ApiFormat(data) {
76
+ if (data === null || typeof data !== "object")
77
+ return false;
78
+ if (isBlockStorage(data))
79
+ return false;
80
+ const obj = data;
81
+ return "args" in obj;
82
+ }
83
+ // =============================================================================
84
+ // Facade Callback Implementations
85
+ // =============================================================================
86
+ /**
87
+ * Gets storage debug view from raw storage data.
88
+ * Returns structured debug info about the storage state.
89
+ *
90
+ * @param rawStorage - Raw data from blockStorage field (may be JSON string or object)
91
+ * @returns JSON string with storage debug view
92
+ */
93
+ function getStorageDebugView(rawStorage) {
94
+ const { storage } = normalizeStorage(rawStorage);
95
+ const debugView = {
96
+ dataVersion: storage.__dataVersion,
97
+ data: storage.__data,
98
+ };
99
+ return stringifyJson(debugView);
100
+ }
101
+ /**
102
+ * Runs storage migration using the provided hooks.
103
+ * This is the main entry point for the middle layer to trigger migrations.
104
+ *
105
+ * @param currentStorageJson - Current storage as JSON string (or undefined)
106
+ * @param hooks - Migration dependencies (block/plugin data migration and creation functions)
107
+ * @returns MigrationResult
108
+ */
109
+ function migrateStorage(currentStorageJson, hooks) {
110
+ // Normalize current storage
111
+ const { storage: currentStorage } = normalizeStorage(currentStorageJson);
112
+ const newPluginRegistry = hooks.getPluginRegistry();
113
+ // Perform atomic migration of block + all plugins
114
+ const migrationResult = migrateBlockStorage(currentStorage, {
115
+ migrateBlockData: hooks.migrateBlockData,
116
+ migratePluginData: hooks.migratePluginData,
117
+ newPluginRegistry,
118
+ createPluginData: hooks.createPluginData,
119
+ });
120
+ if (!migrationResult.success) {
121
+ return {
122
+ error: `Migration failed at '${migrationResult.failedAt}': ${migrationResult.error}`,
123
+ };
124
+ }
125
+ // Build info message
126
+ const oldVersion = currentStorage.__dataVersion;
127
+ const newVersion = migrationResult.storage.__dataVersion;
128
+ const info = oldVersion === newVersion
129
+ ? `No migration needed (${oldVersion})`
130
+ : `Migrated ${oldVersion} -> ${newVersion}`;
131
+ return {
132
+ newStorageJson: stringifyJson(migrationResult.storage),
133
+ info,
134
+ };
135
+ }
136
+ // =============================================================================
137
+ // Initial Storage Creation
138
+ // =============================================================================
139
+ /**
140
+ * Creates complete initial storage (block data + all plugin data) atomically.
141
+ *
142
+ * @param hooks - Dependencies for creating initial block and plugin data
143
+ * @returns Initial storage as branded JSON string
144
+ */
145
+ function createInitialStorage(hooks) {
146
+ const blockDefault = hooks.getDefaultBlockData();
147
+ const pluginRegistry = hooks.getPluginRegistry();
148
+ const plugins = {};
149
+ for (const pluginId of Object.keys(pluginRegistry)) {
150
+ const initial = hooks.createPluginData(pluginId);
151
+ plugins[pluginId] = { __dataVersion: initial.version, __data: initial.data };
152
+ }
153
+ const storage = {
154
+ [BLOCK_STORAGE_KEY]: BLOCK_STORAGE_SCHEMA_VERSION,
155
+ __dataVersion: blockDefault.version,
156
+ __data: blockDefault.data,
157
+ __pluginRegistry: pluginRegistry,
158
+ __plugins: plugins,
159
+ };
160
+ return stringifyJson(storage);
161
+ }
162
+ /**
163
+ * Derives args from storage using the provided args function.
164
+ * This extracts data from storage and passes it to the block's args() function.
165
+ *
166
+ * @param storageJson - Storage as JSON string
167
+ * @param argsFunction - The block's args derivation function
168
+ * @returns ArgsDeriveResult with derived args or error
169
+ */
170
+ function deriveArgsFromStorage(storageJson, argsFunction) {
171
+ // Extract data from storage
172
+ const { data } = normalizeStorage(storageJson);
173
+ // Call the args function with extracted data
174
+ try {
175
+ const result = argsFunction(data);
176
+ return { value: result };
177
+ }
178
+ catch (e) {
179
+ const errorMsg = e instanceof Error ? e.message : String(e);
180
+ return { error: `args() threw: ${errorMsg}` };
181
+ }
182
+ }
183
+ /**
184
+ * Derives prerunArgs from storage.
185
+ * Uses prerunArgsFunction if provided, otherwise falls back to argsFunction.
186
+ *
187
+ * @param storageJson - Storage as JSON string
188
+ * @param argsFunction - The block's args derivation function (fallback)
189
+ * @param prerunArgsFunction - Optional prerun args derivation function
190
+ * @returns ArgsDeriveResult with derived prerunArgs or error
191
+ */
192
+ function derivePrerunArgsFromStorage(storageJson, argsFunction, prerunArgsFunction) {
193
+ // Extract data from storage
194
+ const { data } = normalizeStorage(storageJson);
195
+ // Try prerunArgs function first if available
196
+ if (prerunArgsFunction) {
197
+ try {
198
+ const result = prerunArgsFunction(data);
199
+ return { value: result };
200
+ }
201
+ catch (e) {
202
+ const errorMsg = e instanceof Error ? e.message : String(e);
203
+ return { error: `prerunArgs() threw: ${errorMsg}` };
204
+ }
205
+ }
206
+ // Fall back to args function
207
+ try {
208
+ const result = argsFunction(data);
209
+ return { value: result };
210
+ }
211
+ catch (e) {
212
+ const errorMsg = e instanceof Error ? e.message : String(e);
213
+ return { error: `args() threw (fallback): ${errorMsg}` };
214
+ }
215
+ }
216
+
217
+ export { BLOCK_STORAGE_KEY, BLOCK_STORAGE_SCHEMA_VERSION, applyStorageUpdate, createInitialStorage, deriveArgsFromStorage, derivePrerunArgsFromStorage, getStorageDebugView, migrateStorage };
218
+ //# sourceMappingURL=block_storage_callbacks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"block_storage_callbacks.js","sources":["../src/block_storage_callbacks.ts"],"sourcesContent":["/**\n * BlockStorage Callback Implementations - wired to facade callbacks in BlockModelV3._done().\n *\n * Provides pure functions for storage operations (migration, initialization,\n * args derivation, updates, debug views). Each function takes its dependencies\n * explicitly as parameters.\n *\n * @module block_storage_callbacks\n * @internal\n */\n\nimport {\n BLOCK_STORAGE_KEY,\n BLOCK_STORAGE_SCHEMA_VERSION,\n type BlockStorage,\n type MutateStoragePayload,\n type StorageDebugView,\n type PluginRegistry,\n type VersionedData,\n createBlockStorage,\n getStorageData,\n isBlockStorage,\n migrateBlockStorage,\n normalizeBlockStorage,\n updateStorageData,\n} from \"./block_storage\";\n\nimport { stringifyJson, type StringifiedJson } from \"@milaboratories/pl-model-common\";\nimport type { DataMigrationResult, DataVersioned } from \"./block_migrations\";\n\n// =============================================================================\n// Hook interfaces for dependency injection\n// =============================================================================\n\n/** Dependencies for storage migration */\nexport interface MigrationHooks {\n migrateBlockData: (versioned: DataVersioned<unknown>) => DataMigrationResult<unknown>;\n getPluginRegistry: () => PluginRegistry;\n migratePluginData: (\n pluginId: string,\n versioned: DataVersioned<unknown>,\n ) => DataMigrationResult<unknown> | undefined;\n createPluginData: (pluginId: string) => DataVersioned<unknown>;\n}\n\n/** Dependencies for initial storage creation */\nexport interface InitialStorageHooks {\n getDefaultBlockData: () => DataVersioned<unknown>;\n getPluginRegistry: () => PluginRegistry;\n createPluginData: (pluginId: string) => DataVersioned<unknown>;\n}\n\n/**\n * Result of storage normalization\n */\nexport interface NormalizeStorageResult {\n /** The normalized BlockStorage object */\n storage: BlockStorage;\n /** The extracted data (what developers see) */\n data: unknown;\n}\n\n/**\n * Normalizes raw storage data and extracts state.\n * Handles all formats:\n * - New BlockStorage format (has discriminator)\n * - Legacy V1/V2 format ({ args, uiState })\n * - Raw V3 state (any other format)\n *\n * @param rawStorage - Raw data from blockStorage field (may be JSON string or object)\n * @returns Object with normalized storage and extracted state\n */\nfunction normalizeStorage(rawStorage: unknown): NormalizeStorageResult {\n // Handle undefined/null\n if (rawStorage === undefined || rawStorage === null) {\n const storage = createBlockStorage({});\n return { storage, data: {} };\n }\n\n // Parse JSON string if needed\n let parsed = rawStorage;\n if (typeof rawStorage === \"string\") {\n try {\n parsed = JSON.parse(rawStorage);\n } catch {\n // If parsing fails, treat string as the data\n const storage = createBlockStorage(rawStorage);\n return { storage, data: rawStorage };\n }\n }\n\n // Check for BlockStorage format (has discriminator)\n if (isBlockStorage(parsed)) {\n const storage = normalizeBlockStorage(parsed);\n return { storage, data: getStorageData(storage) };\n }\n\n // Check for legacy V1/V2 format: { args, uiState }\n if (isLegacyModelV1ApiFormat(parsed)) {\n // For legacy format, the whole object IS the data\n const storage = createBlockStorage(parsed);\n return { storage, data: parsed };\n }\n\n // Raw V3 data - wrap it\n const storage = createBlockStorage(parsed);\n return { storage, data: parsed };\n}\n\n/**\n * Applies a state update to existing storage.\n * Used when setData is called from the frontend.\n *\n * @param currentStorageJson - Current storage as JSON string (must be defined)\n * @param payload - Update payload with operation type and value\n * @returns Updated storage as StringifiedJson<BlockStorage>\n */\nexport function applyStorageUpdate(\n currentStorageJson: string,\n payload: MutateStoragePayload,\n): StringifiedJson<BlockStorage> {\n const { storage: currentStorage } = normalizeStorage(currentStorageJson);\n\n // Update data while preserving other storage fields (version, plugins)\n const updatedStorage = updateStorageData(currentStorage, payload);\n\n return stringifyJson(updatedStorage);\n}\n\n/**\n * Checks if data is in legacy Model API v1 format.\n * Legacy format has { args, uiState? } at top level without the BlockStorage discriminator.\n */\nfunction isLegacyModelV1ApiFormat(data: unknown): data is { args?: unknown } {\n if (data === null || typeof data !== \"object\") return false;\n if (isBlockStorage(data)) return false;\n\n const obj = data as Record<string, unknown>;\n return \"args\" in obj;\n}\n\n// =============================================================================\n// Facade Callback Implementations\n// =============================================================================\n\n/**\n * Gets storage debug view from raw storage data.\n * Returns structured debug info about the storage state.\n *\n * @param rawStorage - Raw data from blockStorage field (may be JSON string or object)\n * @returns JSON string with storage debug view\n */\nexport function getStorageDebugView(rawStorage: unknown): StringifiedJson<StorageDebugView> {\n const { storage } = normalizeStorage(rawStorage);\n const debugView: StorageDebugView = {\n dataVersion: storage.__dataVersion,\n data: storage.__data,\n };\n return stringifyJson(debugView);\n}\n\n// =============================================================================\n// Migration Support\n// =============================================================================\n\n/**\n * Result of storage migration.\n * Returned by __pl_storage_migrate callback.\n *\n * - Error result: { error: string } - serious failure (no context, etc.)\n * - Success result: { newStorageJson: StringifiedJson<BlockStorage>, info: string } - migration succeeded\n */\nexport type MigrationResult =\n | { error: string }\n | { error?: undefined; newStorageJson: StringifiedJson<BlockStorage>; info: string };\n\n/**\n * Runs storage migration using the provided hooks.\n * This is the main entry point for the middle layer to trigger migrations.\n *\n * @param currentStorageJson - Current storage as JSON string (or undefined)\n * @param hooks - Migration dependencies (block/plugin data migration and creation functions)\n * @returns MigrationResult\n */\nexport function migrateStorage(\n currentStorageJson: string | undefined,\n hooks: MigrationHooks,\n): MigrationResult {\n // Normalize current storage\n const { storage: currentStorage } = normalizeStorage(currentStorageJson);\n\n const newPluginRegistry = hooks.getPluginRegistry();\n\n // Perform atomic migration of block + all plugins\n const migrationResult = migrateBlockStorage(currentStorage, {\n migrateBlockData: hooks.migrateBlockData,\n migratePluginData: hooks.migratePluginData,\n newPluginRegistry,\n createPluginData: hooks.createPluginData,\n });\n\n if (!migrationResult.success) {\n return {\n error: `Migration failed at '${migrationResult.failedAt}': ${migrationResult.error}`,\n };\n }\n\n // Build info message\n const oldVersion = currentStorage.__dataVersion;\n const newVersion = migrationResult.storage.__dataVersion;\n const info =\n oldVersion === newVersion\n ? `No migration needed (${oldVersion})`\n : `Migrated ${oldVersion} -> ${newVersion}`;\n\n return {\n newStorageJson: stringifyJson(migrationResult.storage),\n info,\n };\n}\n\n// =============================================================================\n// Initial Storage Creation\n// =============================================================================\n\n/**\n * Creates complete initial storage (block data + all plugin data) atomically.\n *\n * @param hooks - Dependencies for creating initial block and plugin data\n * @returns Initial storage as branded JSON string\n */\nexport function createInitialStorage(hooks: InitialStorageHooks): StringifiedJson<BlockStorage> {\n const blockDefault = hooks.getDefaultBlockData();\n const pluginRegistry = hooks.getPluginRegistry();\n\n const plugins: Record<string, VersionedData<unknown>> = {};\n for (const pluginId of Object.keys(pluginRegistry)) {\n const initial = hooks.createPluginData(pluginId);\n plugins[pluginId] = { __dataVersion: initial.version, __data: initial.data };\n }\n\n const storage: BlockStorage = {\n [BLOCK_STORAGE_KEY]: BLOCK_STORAGE_SCHEMA_VERSION,\n __dataVersion: blockDefault.version,\n __data: blockDefault.data,\n __pluginRegistry: pluginRegistry,\n __plugins: plugins,\n };\n return stringifyJson(storage);\n}\n\n// =============================================================================\n// Args Derivation from Storage\n// =============================================================================\n\n/**\n * Result of args derivation from storage.\n * Returned by __pl_args_derive and __pl_prerunArgs_derive callbacks.\n */\nexport type ArgsDeriveResult = { error: string } | { error?: undefined; value: unknown };\n\n/**\n * Derives args from storage using the provided args function.\n * This extracts data from storage and passes it to the block's args() function.\n *\n * @param storageJson - Storage as JSON string\n * @param argsFunction - The block's args derivation function\n * @returns ArgsDeriveResult with derived args or error\n */\nexport function deriveArgsFromStorage(\n storageJson: string,\n argsFunction: (data: unknown) => unknown,\n): ArgsDeriveResult {\n // Extract data from storage\n const { data } = normalizeStorage(storageJson);\n\n // Call the args function with extracted data\n try {\n const result = argsFunction(data);\n return { value: result };\n } catch (e) {\n const errorMsg = e instanceof Error ? e.message : String(e);\n return { error: `args() threw: ${errorMsg}` };\n }\n}\n\n/**\n * Derives prerunArgs from storage.\n * Uses prerunArgsFunction if provided, otherwise falls back to argsFunction.\n *\n * @param storageJson - Storage as JSON string\n * @param argsFunction - The block's args derivation function (fallback)\n * @param prerunArgsFunction - Optional prerun args derivation function\n * @returns ArgsDeriveResult with derived prerunArgs or error\n */\nexport function derivePrerunArgsFromStorage(\n storageJson: string,\n argsFunction: (data: unknown) => unknown,\n prerunArgsFunction?: (data: unknown) => unknown,\n): ArgsDeriveResult {\n // Extract data from storage\n const { data } = normalizeStorage(storageJson);\n\n // Try prerunArgs function first if available\n if (prerunArgsFunction) {\n try {\n const result = prerunArgsFunction(data);\n return { value: result };\n } catch (e) {\n const errorMsg = e instanceof Error ? e.message : String(e);\n return { error: `prerunArgs() threw: ${errorMsg}` };\n }\n }\n\n // Fall back to args function\n try {\n const result = argsFunction(data);\n return { value: result };\n } catch (e) {\n const errorMsg = e instanceof Error ? e.message : String(e);\n return { error: `args() threw (fallback): ${errorMsg}` };\n }\n}\n\n// Export discriminator key and schema version for external checks\nexport { BLOCK_STORAGE_KEY, BLOCK_STORAGE_SCHEMA_VERSION };\n"],"names":[],"mappings":";;;AAAA;;;;;;;;;AASG;AAqDH;;;;;;;;;AASG;AACH,SAAS,gBAAgB,CAAC,UAAmB,EAAA;;IAE3C,IAAI,UAAU,KAAK,SAAS,IAAI,UAAU,KAAK,IAAI,EAAE;AACnD,QAAA,MAAM,OAAO,GAAG,kBAAkB,CAAC,E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AA,MAAM,QAAQ,GAAG,CAAC,YAAY,KAAK,GAAG,CAAC,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC;AAC3D,QAAA,OAAO,EAAE,KAAK,EAAE,iBAAiB,QAAQ,CAAA,CAAE,EAAE;IAC/C;AACF;AAEA;;;;;;;;AAQG;SACa,2BAA2B,CACzC,WAAmB,EACnB,YAAwC,EACxC,kBAA+C,EAAA;;IAG/C,MAAM,EAAE,IAAI,EAAE,GAAG,gBAAgB,CAAC,WAAW,CAAC;;IAG9C,IAAI,kBAAkB,EAAE;AACtB,QAAA,IAAI;AACF,YAAA,MAAM,MAAM,GAAG,kBAAkB,CAAC,IAAI,CAAC;AACvC,YAAA,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE;QAC1B;QAAE,OAAO,CAAC,EAAE;AACV,YAAA,MAAM,QAAQ,GAAG,CAAC,YAAY,KAAK,GAAG,CAAC,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC;AAC3D,YAAA,OAAO,EAAE,KAAK,EAAE,uBAAuB,QAAQ,CAAA,CAAE,EAAE;QACrD;IACF;;AAGA,IAAA,IAAI;AACF,QAAA,MAAM,MAAM,GAAG,YAAY,CAAC,IAAI,CAAC;AACjC,QAAA,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE;IAC1B;IAAE,OAAO,CAAC,EAAE;AACV,QAAA,MAAM,QAAQ,GAAG,CAAC,YAAY,KAAK,GAAG,CAAC,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC;AAC3D,QAAA,OAAO,EAAE,KAAK,EAAE,4BAA4B,QAAQ,CAAA,CAAE,EAAE;IAC1D;AACF;;;;"}
@@ -0,0 +1,104 @@
1
+ 'use strict';
2
+
3
+ var internal = require('./internal.cjs');
4
+
5
+ /**
6
+ * Block Storage Facade - Contract between bundled blocks and middle layer.
7
+ *
8
+ * ============================================================================
9
+ * VERSIONING
10
+ * ============================================================================
11
+ *
12
+ * Blocks declare their model API version via the `requiresModelAPIVersion` feature flag
13
+ * (see BlockCodeKnownFeatureFlags). This determines how the middle layer manages block state:
14
+ *
15
+ * - Version 1: Legacy BlockModel - state is {args, uiState}, managed directly by middle layer
16
+ * - Version 2: BlockModelV3 - uses blockStorage with VM-based callbacks (this facade)
17
+ *
18
+ * This facade (BlockStorageFacade) is used by blocks with `requiresModelAPIVersion: 2`.
19
+ * The version number matches the model API version for clarity.
20
+ *
21
+ * ============================================================================
22
+ * BACKWARD COMPATIBILITY WARNING
23
+ * ============================================================================
24
+ *
25
+ * This file documents the FACADE between the SDK (bundled into blocks) and the
26
+ * middle layer. Once a block is published, its SDK version is frozen. The middle
27
+ * layer must support ALL previously released callback signatures indefinitely.
28
+ *
29
+ * RULES:
30
+ * 1. NEVER change the signature of existing callbacks
31
+ * 2. NEVER remove existing callbacks
32
+ * 3. New callbacks CAN be added (old blocks won't register them, middle layer
33
+ * should handle missing callbacks gracefully)
34
+ * 4. Callback return types can be EXTENDED (add optional fields) but not changed
35
+ * 5. Callback parameter types should remain compatible (middle layer may need
36
+ * to handle both old and new formats)
37
+ *
38
+ * The facade consists of callbacks registered via `tryRegisterCallback()` with
39
+ * the `__pl_` prefix. These are registered by the SDK when a block loads and
40
+ * called by the middle layer to perform operations.
41
+ *
42
+ * ============================================================================
43
+ * WHAT CAN BE CHANGED FREELY
44
+ * ============================================================================
45
+ *
46
+ * - Middle layer code (lib/node/pl-middle-layer)
47
+ * - SDK internal implementation (as long as callback contracts are preserved)
48
+ * - SDK exports used ONLY by middle layer (not by blocks themselves)
49
+ * - New SDK features that don't affect existing callbacks
50
+ *
51
+ * @module block_storage_facade
52
+ */
53
+ // =============================================================================
54
+ // Facade Version
55
+ // =============================================================================
56
+ /**
57
+ * The current facade version. This value is used for `requiresModelAPIVersion`
58
+ * feature flag in BlockModelV3.
59
+ */
60
+ const BLOCK_STORAGE_FACADE_VERSION = 2;
61
+ // =============================================================================
62
+ // Facade Callback Names
63
+ // =============================================================================
64
+ /**
65
+ * All facade callback names as constants.
66
+ * These are the source of truth - the interface is derived from these.
67
+ *
68
+ * IMPORTANT: When adding a new callback:
69
+ * 1. Add the constant here
70
+ * 2. Add the callback signature to FacadeCallbackTypes below
71
+ * 3. The BlockStorageFacade type will automatically include it
72
+ */
73
+ const BlockStorageFacadeCallbacks = {
74
+ StorageApplyUpdate: "__pl_storage_applyUpdate",
75
+ StorageDebugView: "__pl_storage_debugView",
76
+ StorageMigrate: "__pl_storage_migrate",
77
+ ArgsDerive: "__pl_args_derive",
78
+ PrerunArgsDerive: "__pl_prerunArgs_derive",
79
+ StorageInitial: "__pl_storage_initial",
80
+ };
81
+ /**
82
+ * Creates a map of lambda handles from a callbacks constant object.
83
+ * Keys are the callback string values (e.g., '__pl_storage_applyUpdate').
84
+ */
85
+ function createFacadeHandles(callbacks) {
86
+ return Object.fromEntries(Object.values(callbacks).map((handle) => [handle, internal.createRenderLambda({ handle })]));
87
+ }
88
+ /**
89
+ * Lambda handles for facade callbacks.
90
+ * Used by the middle layer to invoke callbacks via executeSingleLambda().
91
+ */
92
+ const BlockStorageFacadeHandles = createFacadeHandles(BlockStorageFacadeCallbacks);
93
+ /** Register all facade callbacks at once. Ensures all required callbacks are provided. */
94
+ function registerFacadeCallbacks(callbacks) {
95
+ for (const key of Object.values(BlockStorageFacadeCallbacks)) {
96
+ internal.tryRegisterCallback(key, callbacks[key]);
97
+ }
98
+ }
99
+
100
+ exports.BLOCK_STORAGE_FACADE_VERSION = BLOCK_STORAGE_FACADE_VERSION;
101
+ exports.BlockStorageFacadeCallbacks = BlockStorageFacadeCallbacks;
102
+ exports.BlockStorageFacadeHandles = BlockStorageFacadeHandles;
103
+ exports.registerFacadeCallbacks = registerFacadeCallbacks;
104
+ //# sourceMappingURL=block_storage_facade.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"block_storage_facade.cjs","sources":["../src/block_storage_facade.ts"],"sourcesContent":["/**\n * Block Storage Facade - Contract between bundled blocks and middle layer.\n *\n * ============================================================================\n * VERSIONING\n * ============================================================================\n *\n * Blocks declare their model API version via the `requiresModelAPIVersion` feature flag\n * (see BlockCodeKnownFeatureFlags). This determines how the middle layer manages block state:\n *\n * - Version 1: Legacy BlockModel - state is {args, uiState}, managed directly by middle layer\n * - Version 2: BlockModelV3 - uses blockStorage with VM-based callbacks (this facade)\n *\n * This facade (BlockStorageFacade) is used by blocks with `requiresModelAPIVersion: 2`.\n * The version number matches the model API version for clarity.\n *\n * ============================================================================\n * BACKWARD COMPATIBILITY WARNING\n * ============================================================================\n *\n * This file documents the FACADE between the SDK (bundled into blocks) and the\n * middle layer. Once a block is published, its SDK version is frozen. The middle\n * layer must support ALL previously released callback signatures indefinitely.\n *\n * RULES:\n * 1. NEVER change the signature of existing callbacks\n * 2. NEVER remove existing callbacks\n * 3. New callbacks CAN be added (old blocks won't register them, middle layer\n * should handle missing callbacks gracefully)\n * 4. Callback return types can be EXTENDED (add optional fields) but not changed\n * 5. Callback parameter types should remain compatible (middle layer may need\n * to handle both old and new formats)\n *\n * The facade consists of callbacks registered via `tryRegisterCallback()` with\n * the `__pl_` prefix. These are registered by the SDK when a block loads and\n * called by the middle layer to perform operations.\n *\n * ============================================================================\n * WHAT CAN BE CHANGED FREELY\n * ============================================================================\n *\n * - Middle layer code (lib/node/pl-middle-layer)\n * - SDK internal implementation (as long as callback contracts are preserved)\n * - SDK exports used ONLY by middle layer (not by blocks themselves)\n * - New SDK features that don't affect existing callbacks\n *\n * @module block_storage_facade\n */\n\nimport type { MutateStoragePayload } from \"./block_storage\";\nimport type { ConfigRenderLambda } from \"./bconfig\";\nimport { createRenderLambda, tryRegisterCallback } from \"./internal\";\nimport type { StringifiedJson } from \"@milaboratories/pl-model-common\";\n\n// =============================================================================\n// Facade Version\n// =============================================================================\n\n/**\n * The current facade version. This value is used for `requiresModelAPIVersion`\n * feature flag in BlockModelV3.\n */\nexport const BLOCK_STORAGE_FACADE_VERSION = 2;\n\n// =============================================================================\n// Facade Callback Names\n// =============================================================================\n\n/**\n * All facade callback names as constants.\n * These are the source of truth - the interface is derived from these.\n *\n * IMPORTANT: When adding a new callback:\n * 1. Add the constant here\n * 2. Add the callback signature to FacadeCallbackTypes below\n * 3. The BlockStorageFacade type will automatically include it\n */\nexport const BlockStorageFacadeCallbacks = {\n StorageApplyUpdate: \"__pl_storage_applyUpdate\",\n StorageDebugView: \"__pl_storage_debugView\",\n StorageMigrate: \"__pl_storage_migrate\",\n ArgsDerive: \"__pl_args_derive\",\n PrerunArgsDerive: \"__pl_prerunArgs_derive\",\n StorageInitial: \"__pl_storage_initial\",\n} as const;\n\n/**\n * Creates a map of lambda handles from a callbacks constant object.\n * Keys are the callback string values (e.g., '__pl_storage_applyUpdate').\n */\nfunction createFacadeHandles<T extends Record<string, string>>(\n callbacks: T,\n): { [K in T[keyof T]]: ConfigRenderLambda } {\n return Object.fromEntries(\n Object.values(callbacks).map((handle) => [handle, createRenderLambda({ handle })]),\n ) as { [K in T[keyof T]]: ConfigRenderLambda };\n}\n\n/**\n * Lambda handles for facade callbacks.\n * Used by the middle layer to invoke callbacks via executeSingleLambda().\n */\nexport const BlockStorageFacadeHandles = createFacadeHandles(BlockStorageFacadeCallbacks);\n\n// =============================================================================\n// Facade Interface (source of truth for callback signatures)\n// =============================================================================\n\n/**\n * The complete facade interface between bundled blocks (SDK) and middle layer.\n *\n * This interface defines ALL callbacks that a block registers. The middle layer\n * calls these callbacks to perform storage operations.\n *\n * ALL types are inlined to simplify versioning - when a callback changes,\n * the entire signature is visible in one place.\n *\n * BACKWARD COMPATIBILITY:\n * - This interface can only be EXTENDED, never shrunk\n * - Existing callback signatures MUST NOT change\n * - Middle layer should use Partial<BlockStorageFacade> when dealing with\n * blocks of unknown version (older blocks may not have all callbacks)\n *\n * Each callback is documented with:\n * - Purpose and when it's called\n * - Parameter descriptions\n * - Return value description\n */\nexport interface BlockStorageFacade {\n /**\n * Apply state update to storage.\n * Called when UI updates block state (setState) or plugin data.\n * @param currentStorageJson - Current storage as JSON string\n * @param payload - Update payload with operation type and value\n * @returns Updated storage as JSON string\n */\n [BlockStorageFacadeCallbacks.StorageApplyUpdate]: (\n currentStorageJson: StringifiedJson,\n payload: MutateStoragePayload,\n ) => StringifiedJson;\n\n /**\n * Get debug view of storage.\n * Called by developer tools to inspect storage state.\n * @param storageJson - Storage as JSON string (or undefined for new blocks)\n * @returns JSON string containing StorageDebugView\n */\n [BlockStorageFacadeCallbacks.StorageDebugView]: (\n storageJson: StringifiedJson | undefined,\n ) => StringifiedJson;\n\n /**\n * Run storage migration.\n * Called when block loads to migrate data to latest version.\n * @param currentStorageJson - Current storage as JSON string (or undefined for new blocks)\n * @returns Migration result - either error or success with new storage\n */\n [BlockStorageFacadeCallbacks.StorageMigrate]: (currentStorageJson: StringifiedJson | undefined) =>\n | { error: string }\n | {\n error?: undefined;\n newStorageJson: StringifiedJson;\n info: string;\n };\n\n /**\n * Derive args from storage.\n * Called to get block configuration args from storage.\n * @param storageJson - Storage as JSON string\n * @returns Args derivation result - either error or derived value\n */\n [BlockStorageFacadeCallbacks.ArgsDerive]: (\n storageJson: StringifiedJson,\n ) => { error: string } | { error?: undefined; value: unknown };\n\n /**\n * Derive prerunArgs from storage.\n * Called to get prerun args; falls back to args callback if not registered.\n * @param storageJson - Storage as JSON string\n * @returns Args derivation result - either error or derived value\n */\n [BlockStorageFacadeCallbacks.PrerunArgsDerive]: (\n storageJson: StringifiedJson,\n ) => { error: string } | { error?: undefined; value: unknown };\n\n /**\n * Get initial storage JSON for new blocks.\n * Called when creating a new block to get complete initial storage.\n * @returns Initial storage as JSON string\n */\n [BlockStorageFacadeCallbacks.StorageInitial]: () => StringifiedJson;\n}\n\n/** Register all facade callbacks at once. Ensures all required callbacks are provided. */\nexport function registerFacadeCallbacks(callbacks: BlockStorageFacade): void {\n for (const key of Object.values(BlockStorageFacadeCallbacks)) {\n tryRegisterCallback(key, callbacks[key] as (...args: any[]) => any);\n }\n}\n"],"names":["createRenderLambda","tryRegisterCallback"],"mappings":";;;;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CG;AAOH;AACA;AACA;AAEA;;;AAGG;AACI,MAAM,4BAA4B,GAAG;AAE5C;AACA;AACA;AAEA;;;;;;;;AAQG;AACI,MAAM,2BAA2B,GAAG;AACzC,IAAA,kBAAkB,EAAE,0BAA0B;AAC9C,IAAA,gBAAgB,EAAE,wBAAwB;AAC1C,IAAA,cAAc,EAAE,sBAAsB;AACtC,IAAA,UAAU,EAAE,kBAAkB;AAC9B,IAAA,gBAAgB,EAAE,wBAAwB;AAC1C,IAAA,cAAc,EAAE,sBAAsB;;AAGxC;;;AAGG;AACH,SAAS,mBAAmB,CAC1B,SAAY,EAAA;AAEZ,IAAA,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,KAAK,CAAC,MAAM,EAAEA,2BAAkB,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,CACtC;AAChD;AAEA;;;AAGG;MACU,yBAAyB,GAAG,mBAAmB,CAAC,2BAA2B;AA2FxF;AACM,SAAU,uBAAuB,CAAC,SAA6B,EAAA;IACnE,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,2BAA2B,CAAC,EAAE;QAC5DC,4BAAmB,CAAC,GAAG,EAAE,SAAS,CAAC,GAAG,CAA4B,CAAC;IACrE;AACF;;;;;;;"}
@@ -0,0 +1,168 @@
1
+ /**
2
+ * Block Storage Facade - Contract between bundled blocks and middle layer.
3
+ *
4
+ * ============================================================================
5
+ * VERSIONING
6
+ * ============================================================================
7
+ *
8
+ * Blocks declare their model API version via the `requiresModelAPIVersion` feature flag
9
+ * (see BlockCodeKnownFeatureFlags). This determines how the middle layer manages block state:
10
+ *
11
+ * - Version 1: Legacy BlockModel - state is {args, uiState}, managed directly by middle layer
12
+ * - Version 2: BlockModelV3 - uses blockStorage with VM-based callbacks (this facade)
13
+ *
14
+ * This facade (BlockStorageFacade) is used by blocks with `requiresModelAPIVersion: 2`.
15
+ * The version number matches the model API version for clarity.
16
+ *
17
+ * ============================================================================
18
+ * BACKWARD COMPATIBILITY WARNING
19
+ * ============================================================================
20
+ *
21
+ * This file documents the FACADE between the SDK (bundled into blocks) and the
22
+ * middle layer. Once a block is published, its SDK version is frozen. The middle
23
+ * layer must support ALL previously released callback signatures indefinitely.
24
+ *
25
+ * RULES:
26
+ * 1. NEVER change the signature of existing callbacks
27
+ * 2. NEVER remove existing callbacks
28
+ * 3. New callbacks CAN be added (old blocks won't register them, middle layer
29
+ * should handle missing callbacks gracefully)
30
+ * 4. Callback return types can be EXTENDED (add optional fields) but not changed
31
+ * 5. Callback parameter types should remain compatible (middle layer may need
32
+ * to handle both old and new formats)
33
+ *
34
+ * The facade consists of callbacks registered via `tryRegisterCallback()` with
35
+ * the `__pl_` prefix. These are registered by the SDK when a block loads and
36
+ * called by the middle layer to perform operations.
37
+ *
38
+ * ============================================================================
39
+ * WHAT CAN BE CHANGED FREELY
40
+ * ============================================================================
41
+ *
42
+ * - Middle layer code (lib/node/pl-middle-layer)
43
+ * - SDK internal implementation (as long as callback contracts are preserved)
44
+ * - SDK exports used ONLY by middle layer (not by blocks themselves)
45
+ * - New SDK features that don't affect existing callbacks
46
+ *
47
+ * @module block_storage_facade
48
+ */
49
+ import type { MutateStoragePayload } from "./block_storage";
50
+ import type { ConfigRenderLambda } from "./bconfig";
51
+ import type { StringifiedJson } from "@milaboratories/pl-model-common";
52
+ /**
53
+ * The current facade version. This value is used for `requiresModelAPIVersion`
54
+ * feature flag in BlockModelV3.
55
+ */
56
+ export declare const BLOCK_STORAGE_FACADE_VERSION = 2;
57
+ /**
58
+ * All facade callback names as constants.
59
+ * These are the source of truth - the interface is derived from these.
60
+ *
61
+ * IMPORTANT: When adding a new callback:
62
+ * 1. Add the constant here
63
+ * 2. Add the callback signature to FacadeCallbackTypes below
64
+ * 3. The BlockStorageFacade type will automatically include it
65
+ */
66
+ export declare const BlockStorageFacadeCallbacks: {
67
+ readonly StorageApplyUpdate: "__pl_storage_applyUpdate";
68
+ readonly StorageDebugView: "__pl_storage_debugView";
69
+ readonly StorageMigrate: "__pl_storage_migrate";
70
+ readonly ArgsDerive: "__pl_args_derive";
71
+ readonly PrerunArgsDerive: "__pl_prerunArgs_derive";
72
+ readonly StorageInitial: "__pl_storage_initial";
73
+ };
74
+ /**
75
+ * Lambda handles for facade callbacks.
76
+ * Used by the middle layer to invoke callbacks via executeSingleLambda().
77
+ */
78
+ export declare const BlockStorageFacadeHandles: {
79
+ __pl_storage_applyUpdate: ConfigRenderLambda<unknown>;
80
+ __pl_storage_debugView: ConfigRenderLambda<unknown>;
81
+ __pl_storage_migrate: ConfigRenderLambda<unknown>;
82
+ __pl_args_derive: ConfigRenderLambda<unknown>;
83
+ __pl_prerunArgs_derive: ConfigRenderLambda<unknown>;
84
+ __pl_storage_initial: ConfigRenderLambda<unknown>;
85
+ };
86
+ /**
87
+ * The complete facade interface between bundled blocks (SDK) and middle layer.
88
+ *
89
+ * This interface defines ALL callbacks that a block registers. The middle layer
90
+ * calls these callbacks to perform storage operations.
91
+ *
92
+ * ALL types are inlined to simplify versioning - when a callback changes,
93
+ * the entire signature is visible in one place.
94
+ *
95
+ * BACKWARD COMPATIBILITY:
96
+ * - This interface can only be EXTENDED, never shrunk
97
+ * - Existing callback signatures MUST NOT change
98
+ * - Middle layer should use Partial<BlockStorageFacade> when dealing with
99
+ * blocks of unknown version (older blocks may not have all callbacks)
100
+ *
101
+ * Each callback is documented with:
102
+ * - Purpose and when it's called
103
+ * - Parameter descriptions
104
+ * - Return value description
105
+ */
106
+ export interface BlockStorageFacade {
107
+ /**
108
+ * Apply state update to storage.
109
+ * Called when UI updates block state (setState) or plugin data.
110
+ * @param currentStorageJson - Current storage as JSON string
111
+ * @param payload - Update payload with operation type and value
112
+ * @returns Updated storage as JSON string
113
+ */
114
+ [BlockStorageFacadeCallbacks.StorageApplyUpdate]: (currentStorageJson: StringifiedJson, payload: MutateStoragePayload) => StringifiedJson;
115
+ /**
116
+ * Get debug view of storage.
117
+ * Called by developer tools to inspect storage state.
118
+ * @param storageJson - Storage as JSON string (or undefined for new blocks)
119
+ * @returns JSON string containing StorageDebugView
120
+ */
121
+ [BlockStorageFacadeCallbacks.StorageDebugView]: (storageJson: StringifiedJson | undefined) => StringifiedJson;
122
+ /**
123
+ * Run storage migration.
124
+ * Called when block loads to migrate data to latest version.
125
+ * @param currentStorageJson - Current storage as JSON string (or undefined for new blocks)
126
+ * @returns Migration result - either error or success with new storage
127
+ */
128
+ [BlockStorageFacadeCallbacks.StorageMigrate]: (currentStorageJson: StringifiedJson | undefined) => {
129
+ error: string;
130
+ } | {
131
+ error?: undefined;
132
+ newStorageJson: StringifiedJson;
133
+ info: string;
134
+ };
135
+ /**
136
+ * Derive args from storage.
137
+ * Called to get block configuration args from storage.
138
+ * @param storageJson - Storage as JSON string
139
+ * @returns Args derivation result - either error or derived value
140
+ */
141
+ [BlockStorageFacadeCallbacks.ArgsDerive]: (storageJson: StringifiedJson) => {
142
+ error: string;
143
+ } | {
144
+ error?: undefined;
145
+ value: unknown;
146
+ };
147
+ /**
148
+ * Derive prerunArgs from storage.
149
+ * Called to get prerun args; falls back to args callback if not registered.
150
+ * @param storageJson - Storage as JSON string
151
+ * @returns Args derivation result - either error or derived value
152
+ */
153
+ [BlockStorageFacadeCallbacks.PrerunArgsDerive]: (storageJson: StringifiedJson) => {
154
+ error: string;
155
+ } | {
156
+ error?: undefined;
157
+ value: unknown;
158
+ };
159
+ /**
160
+ * Get initial storage JSON for new blocks.
161
+ * Called when creating a new block to get complete initial storage.
162
+ * @returns Initial storage as JSON string
163
+ */
164
+ [BlockStorageFacadeCallbacks.StorageInitial]: () => StringifiedJson;
165
+ }
166
+ /** Register all facade callbacks at once. Ensures all required callbacks are provided. */
167
+ export declare function registerFacadeCallbacks(callbacks: BlockStorageFacade): void;
168
+ //# sourceMappingURL=block_storage_facade.d.ts.map
@@ -0,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EAAE,SAAS,CAAC;QAAC,KAAK,EAAE,OAAO,CAAA;KAAE,CAAC;IAE/D;;;;;OAKG;IACH,CAAC,2BAA2B,CAAC,gBAAgB,CAAC,EAAE,CAC9C,WAAW,EAAE,eAAe,KACzB;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,GAAG;QAAE,KAAK,CAAC,EAAE,SAAS,CAAC;QAAC,KAAK,EAAE,OAAO,CAAA;KAAE,CAAC;IAE/D;;;;OAIG;IACH,CAAC,2BAA2B,CAAC,cAAc,CAAC,EAAE,MAAM,eAAe,CAAC;CACrE;AAED,0FAA0F;AAC1F,wBAAgB,uBAAuB,CAAC,SAAS,EAAE,kBAAkB,GAAG,IAAI,CAI3E"}
@@ -0,0 +1,99 @@
1
+ import { createRenderLambda, tryRegisterCallback } from './internal.js';
2
+
3
+ /**
4
+ * Block Storage Facade - Contract between bundled blocks and middle layer.
5
+ *
6
+ * ============================================================================
7
+ * VERSIONING
8
+ * ============================================================================
9
+ *
10
+ * Blocks declare their model API version via the `requiresModelAPIVersion` feature flag
11
+ * (see BlockCodeKnownFeatureFlags). This determines how the middle layer manages block state:
12
+ *
13
+ * - Version 1: Legacy BlockModel - state is {args, uiState}, managed directly by middle layer
14
+ * - Version 2: BlockModelV3 - uses blockStorage with VM-based callbacks (this facade)
15
+ *
16
+ * This facade (BlockStorageFacade) is used by blocks with `requiresModelAPIVersion: 2`.
17
+ * The version number matches the model API version for clarity.
18
+ *
19
+ * ============================================================================
20
+ * BACKWARD COMPATIBILITY WARNING
21
+ * ============================================================================
22
+ *
23
+ * This file documents the FACADE between the SDK (bundled into blocks) and the
24
+ * middle layer. Once a block is published, its SDK version is frozen. The middle
25
+ * layer must support ALL previously released callback signatures indefinitely.
26
+ *
27
+ * RULES:
28
+ * 1. NEVER change the signature of existing callbacks
29
+ * 2. NEVER remove existing callbacks
30
+ * 3. New callbacks CAN be added (old blocks won't register them, middle layer
31
+ * should handle missing callbacks gracefully)
32
+ * 4. Callback return types can be EXTENDED (add optional fields) but not changed
33
+ * 5. Callback parameter types should remain compatible (middle layer may need
34
+ * to handle both old and new formats)
35
+ *
36
+ * The facade consists of callbacks registered via `tryRegisterCallback()` with
37
+ * the `__pl_` prefix. These are registered by the SDK when a block loads and
38
+ * called by the middle layer to perform operations.
39
+ *
40
+ * ============================================================================
41
+ * WHAT CAN BE CHANGED FREELY
42
+ * ============================================================================
43
+ *
44
+ * - Middle layer code (lib/node/pl-middle-layer)
45
+ * - SDK internal implementation (as long as callback contracts are preserved)
46
+ * - SDK exports used ONLY by middle layer (not by blocks themselves)
47
+ * - New SDK features that don't affect existing callbacks
48
+ *
49
+ * @module block_storage_facade
50
+ */
51
+ // =============================================================================
52
+ // Facade Version
53
+ // =============================================================================
54
+ /**
55
+ * The current facade version. This value is used for `requiresModelAPIVersion`
56
+ * feature flag in BlockModelV3.
57
+ */
58
+ const BLOCK_STORAGE_FACADE_VERSION = 2;
59
+ // =============================================================================
60
+ // Facade Callback Names
61
+ // =============================================================================
62
+ /**
63
+ * All facade callback names as constants.
64
+ * These are the source of truth - the interface is derived from these.
65
+ *
66
+ * IMPORTANT: When adding a new callback:
67
+ * 1. Add the constant here
68
+ * 2. Add the callback signature to FacadeCallbackTypes below
69
+ * 3. The BlockStorageFacade type will automatically include it
70
+ */
71
+ const BlockStorageFacadeCallbacks = {
72
+ StorageApplyUpdate: "__pl_storage_applyUpdate",
73
+ StorageDebugView: "__pl_storage_debugView",
74
+ StorageMigrate: "__pl_storage_migrate",
75
+ ArgsDerive: "__pl_args_derive",
76
+ PrerunArgsDerive: "__pl_prerunArgs_derive",
77
+ StorageInitial: "__pl_storage_initial",
78
+ };
79
+ /**
80
+ * Creates a map of lambda handles from a callbacks constant object.
81
+ * Keys are the callback string values (e.g., '__pl_storage_applyUpdate').
82
+ */
83
+ function createFacadeHandles(callbacks) {
84
+ return Object.fromEntries(Object.values(callbacks).map((handle) => [handle, createRenderLambda({ handle })]));
85
+ }
86
+ /**
87
+ * Lambda handles for facade callbacks.
88
+ * Used by the middle layer to invoke callbacks via executeSingleLambda().
89
+ */
90
+ const BlockStorageFacadeHandles = createFacadeHandles(BlockStorageFacadeCallbacks);
91
+ /** Register all facade callbacks at once. Ensures all required callbacks are provided. */
92
+ function registerFacadeCallbacks(callbacks) {
93
+ for (const key of Object.values(BlockStorageFacadeCallbacks)) {
94
+ tryRegisterCallback(key, callbacks[key]);
95
+ }
96
+ }
97
+
98
+ export { BLOCK_STORAGE_FACADE_VERSION, BlockStorageFacadeCallbacks, BlockStorageFacadeHandles, registerFacadeCallbacks };
99
+ //# sourceMappingURL=block_storage_facade.js.map