@platforma-sdk/model 1.54.13 → 1.56.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.
- package/dist/bconfig/normalization.cjs +8 -1
- package/dist/bconfig/normalization.cjs.map +1 -1
- package/dist/bconfig/normalization.d.ts.map +1 -1
- package/dist/bconfig/normalization.js +8 -1
- package/dist/bconfig/normalization.js.map +1 -1
- package/dist/block_api_v3.d.ts +2 -2
- package/dist/block_api_v3.d.ts.map +1 -1
- package/dist/block_migrations.cjs +246 -214
- package/dist/block_migrations.cjs.map +1 -1
- package/dist/block_migrations.d.ts +180 -158
- package/dist/block_migrations.d.ts.map +1 -1
- package/dist/block_migrations.js +247 -214
- package/dist/block_migrations.js.map +1 -1
- package/dist/block_model.cjs +85 -35
- package/dist/block_model.cjs.map +1 -1
- package/dist/block_model.d.ts +66 -38
- package/dist/block_model.d.ts.map +1 -1
- package/dist/block_model.js +86 -36
- package/dist/block_model.js.map +1 -1
- package/dist/{builder.cjs → block_model_legacy.cjs} +2 -2
- package/dist/block_model_legacy.cjs.map +1 -0
- package/dist/{builder.d.ts → block_model_legacy.d.ts} +1 -1
- package/dist/block_model_legacy.d.ts.map +1 -0
- package/dist/{builder.js → block_model_legacy.js} +2 -2
- package/dist/block_model_legacy.js.map +1 -0
- package/dist/block_state_patch.d.ts +11 -1
- package/dist/block_state_patch.d.ts.map +1 -1
- package/dist/block_storage.cjs +126 -109
- package/dist/block_storage.cjs.map +1 -1
- package/dist/block_storage.d.ts +109 -112
- package/dist/block_storage.d.ts.map +1 -1
- package/dist/block_storage.js +126 -101
- package/dist/block_storage.js.map +1 -1
- package/dist/block_storage_callbacks.cjs +227 -0
- package/dist/block_storage_callbacks.cjs.map +1 -0
- package/dist/block_storage_callbacks.d.ts +113 -0
- package/dist/block_storage_callbacks.d.ts.map +1 -0
- package/dist/block_storage_callbacks.js +218 -0
- package/dist/block_storage_callbacks.js.map +1 -0
- package/dist/block_storage_facade.cjs +104 -0
- package/dist/block_storage_facade.cjs.map +1 -0
- package/dist/block_storage_facade.d.ts +168 -0
- package/dist/block_storage_facade.d.ts.map +1 -0
- package/dist/block_storage_facade.js +99 -0
- package/dist/block_storage_facade.js.map +1 -0
- package/dist/index.cjs +13 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +8 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -4
- package/dist/index.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/platforma.d.ts +11 -4
- package/dist/platforma.d.ts.map +1 -1
- package/dist/plugin_model.cjs +171 -0
- package/dist/plugin_model.cjs.map +1 -0
- package/dist/plugin_model.d.ts +162 -0
- package/dist/plugin_model.d.ts.map +1 -0
- package/dist/plugin_model.js +169 -0
- package/dist/plugin_model.js.map +1 -0
- package/dist/render/api.cjs +20 -21
- package/dist/render/api.cjs.map +1 -1
- package/dist/render/api.d.ts +8 -8
- package/dist/render/api.d.ts.map +1 -1
- package/dist/render/api.js +20 -21
- package/dist/render/api.js.map +1 -1
- package/dist/render/internal.cjs.map +1 -1
- package/dist/render/internal.d.ts +2 -1
- package/dist/render/internal.d.ts.map +1 -1
- package/dist/render/internal.js.map +1 -1
- package/dist/version.cjs +4 -0
- package/dist/version.cjs.map +1 -1
- package/dist/version.d.ts +4 -0
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +4 -1
- package/dist/version.js.map +1 -1
- package/package.json +5 -5
- package/src/bconfig/normalization.ts +8 -1
- package/src/block_api_v3.ts +2 -2
- package/src/block_migrations.test.ts +141 -171
- package/src/block_migrations.ts +300 -285
- package/src/block_model.ts +205 -95
- package/src/{builder.ts → block_model_legacy.ts} +1 -1
- package/src/block_state_patch.ts +13 -1
- package/src/block_storage.test.ts +283 -95
- package/src/block_storage.ts +199 -188
- package/src/block_storage_callbacks.ts +326 -0
- package/src/block_storage_facade.ts +199 -0
- package/src/index.ts +7 -3
- package/src/platforma.ts +26 -7
- package/src/plugin_model.test.ts +168 -0
- package/src/plugin_model.ts +242 -0
- package/src/render/api.ts +26 -24
- package/src/render/internal.ts +3 -1
- package/src/typing.test.ts +1 -1
- package/src/version.ts +8 -0
- package/dist/block_storage_vm.cjs +0 -262
- package/dist/block_storage_vm.cjs.map +0 -1
- package/dist/block_storage_vm.d.ts +0 -59
- package/dist/block_storage_vm.d.ts.map +0 -1
- package/dist/block_storage_vm.js +0 -258
- package/dist/block_storage_vm.js.map +0 -1
- package/dist/branding.d.ts +0 -7
- package/dist/branding.d.ts.map +0 -1
- package/dist/builder.cjs.map +0 -1
- package/dist/builder.d.ts.map +0 -1
- package/dist/builder.js.map +0 -1
- package/dist/sdk_info.cjs +0 -10
- package/dist/sdk_info.cjs.map +0 -1
- package/dist/sdk_info.d.ts +0 -5
- package/dist/sdk_info.d.ts.map +0 -1
- package/dist/sdk_info.js +0 -8
- package/dist/sdk_info.js.map +0 -1
- package/dist/unionize.d.ts +0 -12
- package/dist/unionize.d.ts.map +0 -1
- package/src/block_storage_vm.ts +0 -346
- package/src/branding.ts +0 -4
- package/src/sdk_info.ts +0 -9
- package/src/unionize.ts +0 -12
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BlockStorage Callback Implementations - wired to facade callbacks in BlockModelV3._done().
|
|
3
|
+
*
|
|
4
|
+
* Provides pure functions for storage operations (migration, initialization,
|
|
5
|
+
* args derivation, updates, debug views). Each function takes its dependencies
|
|
6
|
+
* explicitly as parameters.
|
|
7
|
+
*
|
|
8
|
+
* @module block_storage_callbacks
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
BLOCK_STORAGE_KEY,
|
|
14
|
+
BLOCK_STORAGE_SCHEMA_VERSION,
|
|
15
|
+
type BlockStorage,
|
|
16
|
+
type MutateStoragePayload,
|
|
17
|
+
type StorageDebugView,
|
|
18
|
+
type PluginRegistry,
|
|
19
|
+
type VersionedData,
|
|
20
|
+
createBlockStorage,
|
|
21
|
+
getStorageData,
|
|
22
|
+
isBlockStorage,
|
|
23
|
+
migrateBlockStorage,
|
|
24
|
+
normalizeBlockStorage,
|
|
25
|
+
updateStorageData,
|
|
26
|
+
} from "./block_storage";
|
|
27
|
+
|
|
28
|
+
import { stringifyJson, type StringifiedJson } from "@milaboratories/pl-model-common";
|
|
29
|
+
import type { DataMigrationResult, DataVersioned } from "./block_migrations";
|
|
30
|
+
|
|
31
|
+
// =============================================================================
|
|
32
|
+
// Hook interfaces for dependency injection
|
|
33
|
+
// =============================================================================
|
|
34
|
+
|
|
35
|
+
/** Dependencies for storage migration */
|
|
36
|
+
export interface MigrationHooks {
|
|
37
|
+
migrateBlockData: (versioned: DataVersioned<unknown>) => DataMigrationResult<unknown>;
|
|
38
|
+
getPluginRegistry: () => PluginRegistry;
|
|
39
|
+
migratePluginData: (
|
|
40
|
+
pluginId: string,
|
|
41
|
+
versioned: DataVersioned<unknown>,
|
|
42
|
+
) => DataMigrationResult<unknown> | undefined;
|
|
43
|
+
createPluginData: (pluginId: string) => DataVersioned<unknown>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Dependencies for initial storage creation */
|
|
47
|
+
export interface InitialStorageHooks {
|
|
48
|
+
getDefaultBlockData: () => DataVersioned<unknown>;
|
|
49
|
+
getPluginRegistry: () => PluginRegistry;
|
|
50
|
+
createPluginData: (pluginId: string) => DataVersioned<unknown>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Result of storage normalization
|
|
55
|
+
*/
|
|
56
|
+
export interface NormalizeStorageResult {
|
|
57
|
+
/** The normalized BlockStorage object */
|
|
58
|
+
storage: BlockStorage;
|
|
59
|
+
/** The extracted data (what developers see) */
|
|
60
|
+
data: unknown;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Normalizes raw storage data and extracts state.
|
|
65
|
+
* Handles all formats:
|
|
66
|
+
* - New BlockStorage format (has discriminator)
|
|
67
|
+
* - Legacy V1/V2 format ({ args, uiState })
|
|
68
|
+
* - Raw V3 state (any other format)
|
|
69
|
+
*
|
|
70
|
+
* @param rawStorage - Raw data from blockStorage field (may be JSON string or object)
|
|
71
|
+
* @returns Object with normalized storage and extracted state
|
|
72
|
+
*/
|
|
73
|
+
function normalizeStorage(rawStorage: unknown): NormalizeStorageResult {
|
|
74
|
+
// Handle undefined/null
|
|
75
|
+
if (rawStorage === undefined || rawStorage === null) {
|
|
76
|
+
const storage = createBlockStorage({});
|
|
77
|
+
return { storage, data: {} };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Parse JSON string if needed
|
|
81
|
+
let parsed = rawStorage;
|
|
82
|
+
if (typeof rawStorage === "string") {
|
|
83
|
+
try {
|
|
84
|
+
parsed = JSON.parse(rawStorage);
|
|
85
|
+
} catch {
|
|
86
|
+
// If parsing fails, treat string as the data
|
|
87
|
+
const storage = createBlockStorage(rawStorage);
|
|
88
|
+
return { storage, data: rawStorage };
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Check for BlockStorage format (has discriminator)
|
|
93
|
+
if (isBlockStorage(parsed)) {
|
|
94
|
+
const storage = normalizeBlockStorage(parsed);
|
|
95
|
+
return { storage, data: getStorageData(storage) };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Check for legacy V1/V2 format: { args, uiState }
|
|
99
|
+
if (isLegacyModelV1ApiFormat(parsed)) {
|
|
100
|
+
// For legacy format, the whole object IS the data
|
|
101
|
+
const storage = createBlockStorage(parsed);
|
|
102
|
+
return { storage, data: parsed };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Raw V3 data - wrap it
|
|
106
|
+
const storage = createBlockStorage(parsed);
|
|
107
|
+
return { storage, data: parsed };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Applies a state update to existing storage.
|
|
112
|
+
* Used when setData is called from the frontend.
|
|
113
|
+
*
|
|
114
|
+
* @param currentStorageJson - Current storage as JSON string (must be defined)
|
|
115
|
+
* @param payload - Update payload with operation type and value
|
|
116
|
+
* @returns Updated storage as StringifiedJson<BlockStorage>
|
|
117
|
+
*/
|
|
118
|
+
export function applyStorageUpdate(
|
|
119
|
+
currentStorageJson: string,
|
|
120
|
+
payload: MutateStoragePayload,
|
|
121
|
+
): StringifiedJson<BlockStorage> {
|
|
122
|
+
const { storage: currentStorage } = normalizeStorage(currentStorageJson);
|
|
123
|
+
|
|
124
|
+
// Update data while preserving other storage fields (version, plugins)
|
|
125
|
+
const updatedStorage = updateStorageData(currentStorage, payload);
|
|
126
|
+
|
|
127
|
+
return stringifyJson(updatedStorage);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Checks if data is in legacy Model API v1 format.
|
|
132
|
+
* Legacy format has { args, uiState? } at top level without the BlockStorage discriminator.
|
|
133
|
+
*/
|
|
134
|
+
function isLegacyModelV1ApiFormat(data: unknown): data is { args?: unknown } {
|
|
135
|
+
if (data === null || typeof data !== "object") return false;
|
|
136
|
+
if (isBlockStorage(data)) return false;
|
|
137
|
+
|
|
138
|
+
const obj = data as Record<string, unknown>;
|
|
139
|
+
return "args" in obj;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// =============================================================================
|
|
143
|
+
// Facade Callback Implementations
|
|
144
|
+
// =============================================================================
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Gets storage debug view from raw storage data.
|
|
148
|
+
* Returns structured debug info about the storage state.
|
|
149
|
+
*
|
|
150
|
+
* @param rawStorage - Raw data from blockStorage field (may be JSON string or object)
|
|
151
|
+
* @returns JSON string with storage debug view
|
|
152
|
+
*/
|
|
153
|
+
export function getStorageDebugView(rawStorage: unknown): StringifiedJson<StorageDebugView> {
|
|
154
|
+
const { storage } = normalizeStorage(rawStorage);
|
|
155
|
+
const debugView: StorageDebugView = {
|
|
156
|
+
dataVersion: storage.__dataVersion,
|
|
157
|
+
data: storage.__data,
|
|
158
|
+
};
|
|
159
|
+
return stringifyJson(debugView);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// =============================================================================
|
|
163
|
+
// Migration Support
|
|
164
|
+
// =============================================================================
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Result of storage migration.
|
|
168
|
+
* Returned by __pl_storage_migrate callback.
|
|
169
|
+
*
|
|
170
|
+
* - Error result: { error: string } - serious failure (no context, etc.)
|
|
171
|
+
* - Success result: { newStorageJson: StringifiedJson<BlockStorage>, info: string } - migration succeeded
|
|
172
|
+
*/
|
|
173
|
+
export type MigrationResult =
|
|
174
|
+
| { error: string }
|
|
175
|
+
| { error?: undefined; newStorageJson: StringifiedJson<BlockStorage>; info: string };
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Runs storage migration using the provided hooks.
|
|
179
|
+
* This is the main entry point for the middle layer to trigger migrations.
|
|
180
|
+
*
|
|
181
|
+
* @param currentStorageJson - Current storage as JSON string (or undefined)
|
|
182
|
+
* @param hooks - Migration dependencies (block/plugin data migration and creation functions)
|
|
183
|
+
* @returns MigrationResult
|
|
184
|
+
*/
|
|
185
|
+
export function migrateStorage(
|
|
186
|
+
currentStorageJson: string | undefined,
|
|
187
|
+
hooks: MigrationHooks,
|
|
188
|
+
): MigrationResult {
|
|
189
|
+
// Normalize current storage
|
|
190
|
+
const { storage: currentStorage } = normalizeStorage(currentStorageJson);
|
|
191
|
+
|
|
192
|
+
const newPluginRegistry = hooks.getPluginRegistry();
|
|
193
|
+
|
|
194
|
+
// Perform atomic migration of block + all plugins
|
|
195
|
+
const migrationResult = migrateBlockStorage(currentStorage, {
|
|
196
|
+
migrateBlockData: hooks.migrateBlockData,
|
|
197
|
+
migratePluginData: hooks.migratePluginData,
|
|
198
|
+
newPluginRegistry,
|
|
199
|
+
createPluginData: hooks.createPluginData,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
if (!migrationResult.success) {
|
|
203
|
+
return {
|
|
204
|
+
error: `Migration failed at '${migrationResult.failedAt}': ${migrationResult.error}`,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Build info message
|
|
209
|
+
const oldVersion = currentStorage.__dataVersion;
|
|
210
|
+
const newVersion = migrationResult.storage.__dataVersion;
|
|
211
|
+
const info =
|
|
212
|
+
oldVersion === newVersion
|
|
213
|
+
? `No migration needed (${oldVersion})`
|
|
214
|
+
: `Migrated ${oldVersion} -> ${newVersion}`;
|
|
215
|
+
|
|
216
|
+
return {
|
|
217
|
+
newStorageJson: stringifyJson(migrationResult.storage),
|
|
218
|
+
info,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// =============================================================================
|
|
223
|
+
// Initial Storage Creation
|
|
224
|
+
// =============================================================================
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Creates complete initial storage (block data + all plugin data) atomically.
|
|
228
|
+
*
|
|
229
|
+
* @param hooks - Dependencies for creating initial block and plugin data
|
|
230
|
+
* @returns Initial storage as branded JSON string
|
|
231
|
+
*/
|
|
232
|
+
export function createInitialStorage(hooks: InitialStorageHooks): StringifiedJson<BlockStorage> {
|
|
233
|
+
const blockDefault = hooks.getDefaultBlockData();
|
|
234
|
+
const pluginRegistry = hooks.getPluginRegistry();
|
|
235
|
+
|
|
236
|
+
const plugins: Record<string, VersionedData<unknown>> = {};
|
|
237
|
+
for (const pluginId of Object.keys(pluginRegistry)) {
|
|
238
|
+
const initial = hooks.createPluginData(pluginId);
|
|
239
|
+
plugins[pluginId] = { __dataVersion: initial.version, __data: initial.data };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const storage: BlockStorage = {
|
|
243
|
+
[BLOCK_STORAGE_KEY]: BLOCK_STORAGE_SCHEMA_VERSION,
|
|
244
|
+
__dataVersion: blockDefault.version,
|
|
245
|
+
__data: blockDefault.data,
|
|
246
|
+
__pluginRegistry: pluginRegistry,
|
|
247
|
+
__plugins: plugins,
|
|
248
|
+
};
|
|
249
|
+
return stringifyJson(storage);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// =============================================================================
|
|
253
|
+
// Args Derivation from Storage
|
|
254
|
+
// =============================================================================
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Result of args derivation from storage.
|
|
258
|
+
* Returned by __pl_args_derive and __pl_prerunArgs_derive callbacks.
|
|
259
|
+
*/
|
|
260
|
+
export type ArgsDeriveResult = { error: string } | { error?: undefined; value: unknown };
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Derives args from storage using the provided args function.
|
|
264
|
+
* This extracts data from storage and passes it to the block's args() function.
|
|
265
|
+
*
|
|
266
|
+
* @param storageJson - Storage as JSON string
|
|
267
|
+
* @param argsFunction - The block's args derivation function
|
|
268
|
+
* @returns ArgsDeriveResult with derived args or error
|
|
269
|
+
*/
|
|
270
|
+
export function deriveArgsFromStorage(
|
|
271
|
+
storageJson: string,
|
|
272
|
+
argsFunction: (data: unknown) => unknown,
|
|
273
|
+
): ArgsDeriveResult {
|
|
274
|
+
// Extract data from storage
|
|
275
|
+
const { data } = normalizeStorage(storageJson);
|
|
276
|
+
|
|
277
|
+
// Call the args function with extracted data
|
|
278
|
+
try {
|
|
279
|
+
const result = argsFunction(data);
|
|
280
|
+
return { value: result };
|
|
281
|
+
} catch (e) {
|
|
282
|
+
const errorMsg = e instanceof Error ? e.message : String(e);
|
|
283
|
+
return { error: `args() threw: ${errorMsg}` };
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Derives prerunArgs from storage.
|
|
289
|
+
* Uses prerunArgsFunction if provided, otherwise falls back to argsFunction.
|
|
290
|
+
*
|
|
291
|
+
* @param storageJson - Storage as JSON string
|
|
292
|
+
* @param argsFunction - The block's args derivation function (fallback)
|
|
293
|
+
* @param prerunArgsFunction - Optional prerun args derivation function
|
|
294
|
+
* @returns ArgsDeriveResult with derived prerunArgs or error
|
|
295
|
+
*/
|
|
296
|
+
export function derivePrerunArgsFromStorage(
|
|
297
|
+
storageJson: string,
|
|
298
|
+
argsFunction: (data: unknown) => unknown,
|
|
299
|
+
prerunArgsFunction?: (data: unknown) => unknown,
|
|
300
|
+
): ArgsDeriveResult {
|
|
301
|
+
// Extract data from storage
|
|
302
|
+
const { data } = normalizeStorage(storageJson);
|
|
303
|
+
|
|
304
|
+
// Try prerunArgs function first if available
|
|
305
|
+
if (prerunArgsFunction) {
|
|
306
|
+
try {
|
|
307
|
+
const result = prerunArgsFunction(data);
|
|
308
|
+
return { value: result };
|
|
309
|
+
} catch (e) {
|
|
310
|
+
const errorMsg = e instanceof Error ? e.message : String(e);
|
|
311
|
+
return { error: `prerunArgs() threw: ${errorMsg}` };
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Fall back to args function
|
|
316
|
+
try {
|
|
317
|
+
const result = argsFunction(data);
|
|
318
|
+
return { value: result };
|
|
319
|
+
} catch (e) {
|
|
320
|
+
const errorMsg = e instanceof Error ? e.message : String(e);
|
|
321
|
+
return { error: `args() threw (fallback): ${errorMsg}` };
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Export discriminator key and schema version for external checks
|
|
326
|
+
export { BLOCK_STORAGE_KEY, BLOCK_STORAGE_SCHEMA_VERSION };
|
|
@@ -0,0 +1,199 @@
|
|
|
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
|
+
|
|
50
|
+
import type { MutateStoragePayload } from "./block_storage";
|
|
51
|
+
import type { ConfigRenderLambda } from "./bconfig";
|
|
52
|
+
import { createRenderLambda, tryRegisterCallback } from "./internal";
|
|
53
|
+
import type { StringifiedJson } from "@milaboratories/pl-model-common";
|
|
54
|
+
|
|
55
|
+
// =============================================================================
|
|
56
|
+
// Facade Version
|
|
57
|
+
// =============================================================================
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The current facade version. This value is used for `requiresModelAPIVersion`
|
|
61
|
+
* feature flag in BlockModelV3.
|
|
62
|
+
*/
|
|
63
|
+
export const BLOCK_STORAGE_FACADE_VERSION = 2;
|
|
64
|
+
|
|
65
|
+
// =============================================================================
|
|
66
|
+
// Facade Callback Names
|
|
67
|
+
// =============================================================================
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* All facade callback names as constants.
|
|
71
|
+
* These are the source of truth - the interface is derived from these.
|
|
72
|
+
*
|
|
73
|
+
* IMPORTANT: When adding a new callback:
|
|
74
|
+
* 1. Add the constant here
|
|
75
|
+
* 2. Add the callback signature to FacadeCallbackTypes below
|
|
76
|
+
* 3. The BlockStorageFacade type will automatically include it
|
|
77
|
+
*/
|
|
78
|
+
export const BlockStorageFacadeCallbacks = {
|
|
79
|
+
StorageApplyUpdate: "__pl_storage_applyUpdate",
|
|
80
|
+
StorageDebugView: "__pl_storage_debugView",
|
|
81
|
+
StorageMigrate: "__pl_storage_migrate",
|
|
82
|
+
ArgsDerive: "__pl_args_derive",
|
|
83
|
+
PrerunArgsDerive: "__pl_prerunArgs_derive",
|
|
84
|
+
StorageInitial: "__pl_storage_initial",
|
|
85
|
+
} as const;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Creates a map of lambda handles from a callbacks constant object.
|
|
89
|
+
* Keys are the callback string values (e.g., '__pl_storage_applyUpdate').
|
|
90
|
+
*/
|
|
91
|
+
function createFacadeHandles<T extends Record<string, string>>(
|
|
92
|
+
callbacks: T,
|
|
93
|
+
): { [K in T[keyof T]]: ConfigRenderLambda } {
|
|
94
|
+
return Object.fromEntries(
|
|
95
|
+
Object.values(callbacks).map((handle) => [handle, createRenderLambda({ handle })]),
|
|
96
|
+
) as { [K in T[keyof T]]: ConfigRenderLambda };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Lambda handles for facade callbacks.
|
|
101
|
+
* Used by the middle layer to invoke callbacks via executeSingleLambda().
|
|
102
|
+
*/
|
|
103
|
+
export const BlockStorageFacadeHandles = createFacadeHandles(BlockStorageFacadeCallbacks);
|
|
104
|
+
|
|
105
|
+
// =============================================================================
|
|
106
|
+
// Facade Interface (source of truth for callback signatures)
|
|
107
|
+
// =============================================================================
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The complete facade interface between bundled blocks (SDK) and middle layer.
|
|
111
|
+
*
|
|
112
|
+
* This interface defines ALL callbacks that a block registers. The middle layer
|
|
113
|
+
* calls these callbacks to perform storage operations.
|
|
114
|
+
*
|
|
115
|
+
* ALL types are inlined to simplify versioning - when a callback changes,
|
|
116
|
+
* the entire signature is visible in one place.
|
|
117
|
+
*
|
|
118
|
+
* BACKWARD COMPATIBILITY:
|
|
119
|
+
* - This interface can only be EXTENDED, never shrunk
|
|
120
|
+
* - Existing callback signatures MUST NOT change
|
|
121
|
+
* - Middle layer should use Partial<BlockStorageFacade> when dealing with
|
|
122
|
+
* blocks of unknown version (older blocks may not have all callbacks)
|
|
123
|
+
*
|
|
124
|
+
* Each callback is documented with:
|
|
125
|
+
* - Purpose and when it's called
|
|
126
|
+
* - Parameter descriptions
|
|
127
|
+
* - Return value description
|
|
128
|
+
*/
|
|
129
|
+
export interface BlockStorageFacade {
|
|
130
|
+
/**
|
|
131
|
+
* Apply state update to storage.
|
|
132
|
+
* Called when UI updates block state (setState) or plugin data.
|
|
133
|
+
* @param currentStorageJson - Current storage as JSON string
|
|
134
|
+
* @param payload - Update payload with operation type and value
|
|
135
|
+
* @returns Updated storage as JSON string
|
|
136
|
+
*/
|
|
137
|
+
[BlockStorageFacadeCallbacks.StorageApplyUpdate]: (
|
|
138
|
+
currentStorageJson: StringifiedJson,
|
|
139
|
+
payload: MutateStoragePayload,
|
|
140
|
+
) => StringifiedJson;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Get debug view of storage.
|
|
144
|
+
* Called by developer tools to inspect storage state.
|
|
145
|
+
* @param storageJson - Storage as JSON string (or undefined for new blocks)
|
|
146
|
+
* @returns JSON string containing StorageDebugView
|
|
147
|
+
*/
|
|
148
|
+
[BlockStorageFacadeCallbacks.StorageDebugView]: (
|
|
149
|
+
storageJson: StringifiedJson | undefined,
|
|
150
|
+
) => StringifiedJson;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Run storage migration.
|
|
154
|
+
* Called when block loads to migrate data to latest version.
|
|
155
|
+
* @param currentStorageJson - Current storage as JSON string (or undefined for new blocks)
|
|
156
|
+
* @returns Migration result - either error or success with new storage
|
|
157
|
+
*/
|
|
158
|
+
[BlockStorageFacadeCallbacks.StorageMigrate]: (currentStorageJson: StringifiedJson | undefined) =>
|
|
159
|
+
| { error: string }
|
|
160
|
+
| {
|
|
161
|
+
error?: undefined;
|
|
162
|
+
newStorageJson: StringifiedJson;
|
|
163
|
+
info: string;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Derive args from storage.
|
|
168
|
+
* Called to get block configuration args from storage.
|
|
169
|
+
* @param storageJson - Storage as JSON string
|
|
170
|
+
* @returns Args derivation result - either error or derived value
|
|
171
|
+
*/
|
|
172
|
+
[BlockStorageFacadeCallbacks.ArgsDerive]: (
|
|
173
|
+
storageJson: StringifiedJson,
|
|
174
|
+
) => { error: string } | { error?: undefined; value: unknown };
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Derive prerunArgs from storage.
|
|
178
|
+
* Called to get prerun args; falls back to args callback if not registered.
|
|
179
|
+
* @param storageJson - Storage as JSON string
|
|
180
|
+
* @returns Args derivation result - either error or derived value
|
|
181
|
+
*/
|
|
182
|
+
[BlockStorageFacadeCallbacks.PrerunArgsDerive]: (
|
|
183
|
+
storageJson: StringifiedJson,
|
|
184
|
+
) => { error: string } | { error?: undefined; value: unknown };
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Get initial storage JSON for new blocks.
|
|
188
|
+
* Called when creating a new block to get complete initial storage.
|
|
189
|
+
* @returns Initial storage as JSON string
|
|
190
|
+
*/
|
|
191
|
+
[BlockStorageFacadeCallbacks.StorageInitial]: () => StringifiedJson;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Register all facade callbacks at once. Ensures all required callbacks are provided. */
|
|
195
|
+
export function registerFacadeCallbacks(callbacks: BlockStorageFacade): void {
|
|
196
|
+
for (const key of Object.values(BlockStorageFacadeCallbacks)) {
|
|
197
|
+
tryRegisterCallback(key, callbacks[key] as (...args: any[]) => any);
|
|
198
|
+
}
|
|
199
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
export * from "./block_state_patch";
|
|
2
2
|
export * from "./block_state_util";
|
|
3
3
|
export * from "./block_storage";
|
|
4
|
-
export * from "./
|
|
4
|
+
export * from "./block_storage_facade";
|
|
5
|
+
export * from "./block_model_legacy";
|
|
5
6
|
export { BlockModelV3 } from "./block_model";
|
|
7
|
+
export type { PluginInstance, ParamsInput } from "./block_model";
|
|
6
8
|
export {
|
|
7
9
|
DataModel,
|
|
8
10
|
DataModelBuilder,
|
|
9
11
|
DataUnrecoverableError,
|
|
10
12
|
isDataUnrecoverableError,
|
|
11
|
-
defineDataVersions,
|
|
12
13
|
defaultRecover,
|
|
13
14
|
makeDataVersioned,
|
|
14
15
|
} from "./block_migrations";
|
|
16
|
+
export type { LegacyV1State } from "./block_migrations";
|
|
17
|
+
export { PluginModel } from "./plugin_model";
|
|
18
|
+
export type { PluginRenderCtx } from "./plugin_model";
|
|
15
19
|
export * from "./bconfig";
|
|
16
20
|
export * from "./components";
|
|
17
21
|
export * from "./config";
|
|
@@ -19,7 +23,7 @@ export * from "./pframe";
|
|
|
19
23
|
export * from "./platforma";
|
|
20
24
|
export * from "./ref_util";
|
|
21
25
|
export * from "./render";
|
|
22
|
-
export * from "./
|
|
26
|
+
export * from "./version";
|
|
23
27
|
export * from "./raw_globals";
|
|
24
28
|
export * from "./block_api_v1";
|
|
25
29
|
export * from "./block_api_v2";
|
package/src/platforma.ts
CHANGED
|
@@ -7,8 +7,9 @@ import type {
|
|
|
7
7
|
DriverKit,
|
|
8
8
|
OutputWithStatus,
|
|
9
9
|
} from "@milaboratories/pl-model-common";
|
|
10
|
-
import type { SdkInfo } from "./
|
|
10
|
+
import type { SdkInfo } from "./version";
|
|
11
11
|
import type { BlockStatePatch } from "./block_state_patch";
|
|
12
|
+
import type { PluginInstance } from "./block_model";
|
|
12
13
|
|
|
13
14
|
/** Defines all methods to interact with the platform environment from within a block UI. @deprecated */
|
|
14
15
|
export interface PlatformaV1<
|
|
@@ -43,18 +44,21 @@ export interface PlatformaV2<
|
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
export interface PlatformaV3<
|
|
47
|
+
Data = unknown,
|
|
46
48
|
Args = unknown,
|
|
47
49
|
Outputs extends Record<string, OutputWithStatus<unknown>> = Record<
|
|
48
50
|
string,
|
|
49
51
|
OutputWithStatus<unknown>
|
|
50
52
|
>,
|
|
51
|
-
Data = unknown,
|
|
52
53
|
Href extends `/${string}` = `/${string}`,
|
|
54
|
+
Plugins extends Record<string, unknown> = Record<string, unknown>,
|
|
53
55
|
>
|
|
54
|
-
extends BlockApiV3<Args, Outputs,
|
|
56
|
+
extends BlockApiV3<Data, Args, Outputs, Href>, DriverKit {
|
|
55
57
|
/** Information about SDK version current platforma environment was compiled with. */
|
|
56
58
|
readonly sdkInfo: SdkInfo;
|
|
57
59
|
readonly apiVersion: 3;
|
|
60
|
+
/** @internal Type brand for plugin type inference. Not used at runtime. */
|
|
61
|
+
readonly __pluginsBrand?: Plugins;
|
|
58
62
|
}
|
|
59
63
|
|
|
60
64
|
export type Platforma<
|
|
@@ -63,12 +67,12 @@ export type Platforma<
|
|
|
63
67
|
string,
|
|
64
68
|
OutputWithStatus<unknown>
|
|
65
69
|
>,
|
|
66
|
-
|
|
70
|
+
UiStateOrData = unknown,
|
|
67
71
|
Href extends `/${string}` = `/${string}`,
|
|
68
72
|
> =
|
|
69
|
-
| PlatformaV1<Args, Outputs,
|
|
70
|
-
| PlatformaV2<Args, Outputs,
|
|
71
|
-
| PlatformaV3<Args, Outputs,
|
|
73
|
+
| PlatformaV1<Args, Outputs, UiStateOrData, Href>
|
|
74
|
+
| PlatformaV2<Args, Outputs, UiStateOrData, Href>
|
|
75
|
+
| PlatformaV3<UiStateOrData, Args, Outputs, Href>;
|
|
72
76
|
|
|
73
77
|
export type PlatformaExtended<Pl extends Platforma = Platforma> = Pl & {
|
|
74
78
|
blockModelInfo: BlockModelInfo;
|
|
@@ -106,6 +110,7 @@ export type InferHrefType<Pl extends Platforma> =
|
|
|
106
110
|
export type PlatformaFactory = (config: { sdkVersion: string }) => Platforma;
|
|
107
111
|
|
|
108
112
|
export type InferBlockState<Pl extends Platforma> = BlockStateV3<
|
|
113
|
+
InferDataType<Pl>,
|
|
109
114
|
InferOutputsType<Pl>,
|
|
110
115
|
InferHrefType<Pl>
|
|
111
116
|
>;
|
|
@@ -116,3 +121,17 @@ export type InferBlockStatePatch<Pl extends Platforma> = BlockStatePatch<
|
|
|
116
121
|
InferUiState<Pl>,
|
|
117
122
|
InferHrefType<Pl>
|
|
118
123
|
>;
|
|
124
|
+
|
|
125
|
+
/** Extract plugin IDs as a string literal union from a Platforma type. */
|
|
126
|
+
export type InferPluginNames<Pl> =
|
|
127
|
+
Pl extends PlatformaV3<any, any, any, any, infer P> ? string & keyof P : never;
|
|
128
|
+
|
|
129
|
+
/** Extract the Data type for a specific plugin by its ID. */
|
|
130
|
+
export type InferPluginData<Pl, PluginId extends string> =
|
|
131
|
+
Pl extends PlatformaV3<any, any, any, any, infer P>
|
|
132
|
+
? PluginId extends keyof P
|
|
133
|
+
? P[PluginId] extends PluginInstance<infer D, any, any>
|
|
134
|
+
? D
|
|
135
|
+
: never
|
|
136
|
+
: never
|
|
137
|
+
: never;
|