@milaboratories/pl-middle-layer 2.8.10 → 2.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@milaboratories/pl-middle-layer",
3
- "version": "2.8.10",
3
+ "version": "2.9.0",
4
4
  "description": "Pl Middle Layer",
5
5
  "keywords": [],
6
6
  "license": "UNLICENSED",
@@ -27,13 +27,13 @@
27
27
  "@milaboratories/pframes-rs-node": "1.3.1",
28
28
  "@milaboratories/pl-client": "3.28.2",
29
29
  "@milaboratories/pl-crash-recorder": "0.3.16",
30
- "@milaboratories/pl-drivers": "2.1.3",
31
- "@milaboratories/pl-errors": "1.4.73",
30
+ "@milaboratories/pl-drivers": "2.1.4",
31
+ "@milaboratories/pl-errors": "1.5.0",
32
32
  "@milaboratories/pl-http": "1.2.7",
33
33
  "@milaboratories/pl-model-backend": "1.4.58",
34
34
  "@milaboratories/pl-model-common": "1.60.0",
35
35
  "@milaboratories/pl-model-middle-layer": "1.41.4",
36
- "@milaboratories/pl-tree": "1.18.12",
36
+ "@milaboratories/pl-tree": "1.18.13",
37
37
  "@milaboratories/resolve-helper": "1.1.3",
38
38
  "@milaboratories/ts-helpers": "1.9.1",
39
39
  "@platforma-sdk/block-tools": "2.22.5",
@@ -1,5 +1,6 @@
1
1
  export { MiddleLayer } from "./middle_layer";
2
2
  export { Project } from "./project";
3
+ export type { AddBlockOps } from "./project";
3
4
  export * from "./driver_kit";
4
5
  export * from "./ops";
5
6
  export type { TreeSnapshotMiss, TreeSnapshotStat } from "./tree_snapshot_store";
@@ -70,10 +70,20 @@ import type {
70
70
  BlockSettings,
71
71
  BlockStateInternalV3,
72
72
  } from "@milaboratories/pl-model-middle-layer";
73
- import type { ColumnLink } from "@milaboratories/pl-model-common";
73
+ import type { BlockId, ColumnLink } from "@milaboratories/pl-model-common";
74
74
  import { activeConfigs } from "./active_cfg";
75
75
  import { NavigationStates } from "./navigation_states";
76
- import { checkBlockFlag, extractConfig, BLOCK_STORAGE_FACADE_VERSION } from "@platforma-sdk/model";
76
+ import {
77
+ checkBlockFlag,
78
+ extractConfig,
79
+ BLOCK_STORAGE_FACADE_VERSION,
80
+ BlockStorageFacadeCallbacks,
81
+ type BlockKindReference,
82
+ } from "@platforma-sdk/model";
83
+ import {
84
+ BlockInitParamsRejectedError,
85
+ ModelAPIVersionMismatchError,
86
+ } from "@milaboratories/pl-errors";
77
87
  import fs from "node:fs/promises";
78
88
  import canonicalize from "canonicalize";
79
89
  import type { ProjectOverviewLight } from "./project_overview_light";
@@ -119,6 +129,27 @@ function resetBlockState(computables: BlockStateComputables): void {
119
129
  computables.dependents.resetState();
120
130
  }
121
131
 
132
+ /** Options of {@link Project.addBlock}. */
133
+ export type AddBlockOps = {
134
+ /**
135
+ * Start the block from initialization params instead of the model's default state. The block's
136
+ * model checks the params against its kind and builds its initial storage from them. Only a
137
+ * block with model API version 2 whose SDK has the params initializer can be created this way.
138
+ */
139
+ init?: {
140
+ /** The kind's initialization params, as a template entry holds them. */
141
+ params: unknown;
142
+ /**
143
+ * Ids used inside `params` in place of block ids, each mapped to the id of a block of this
144
+ * project. The block's model replaces them in every reference it recognizes. An id that is
145
+ * not listed is left as it is, so params that already hold real block ids need no map.
146
+ */
147
+ blockIds?: Readonly<Record<string, BlockId>>;
148
+ };
149
+ /** Refuse to add a block whose model API version differs from this one. */
150
+ requireModelAPIVersion?: number;
151
+ };
152
+
122
153
  /** Data access object, to manipulate and read single opened (!) project data. */
123
154
  export class Project {
124
155
  /** Data for the left panel, contain basic information about block status. */
@@ -378,8 +409,13 @@ export class Project {
378
409
  * @param before id of the block to insert new block before
379
410
  * @param blockId internal id to be assigned for the block, this arg can be omitted
380
411
  * then, randomly generated UUID will be assigned automatically
412
+ * @param ops see {@link AddBlockOps}
381
413
  *
382
414
  * @return returns newly created block id
415
+ * @throws ModelAPIVersionMismatchError when `ops.requireModelAPIVersion` differs from the
416
+ * block's model API version; nothing is created
417
+ * @throws BlockInitParamsRejectedError when the block does not accept `ops.init.params`;
418
+ * nothing is created
383
419
  * */
384
420
  public async addBlock(
385
421
  blockLabel: string,
@@ -387,20 +423,60 @@ export class Project {
387
423
  before?: string,
388
424
  author: AuthorMarker | undefined = undefined,
389
425
  blockId: string = randomUUID(),
426
+ ops: AddBlockOps = {},
390
427
  ): Promise<string> {
391
428
  const preparedBp = await this.env.bpPreparer.prepare(blockPackSpec);
392
429
  const blockCfg = extractConfig(preparedBp.config);
393
430
 
431
+ if (
432
+ ops.requireModelAPIVersion !== undefined &&
433
+ blockCfg.modelAPIVersion !== ops.requireModelAPIVersion
434
+ )
435
+ throw new ModelAPIVersionMismatchError(
436
+ blockId,
437
+ ops.requireModelAPIVersion,
438
+ blockCfg.modelAPIVersion,
439
+ );
440
+
394
441
  this.env.runtimeCapabilities.throwIfIncompatible(blockCfg.featureFlags);
395
442
  throwIfMissingServerCapabilities(this.env.pl, preparedBp.requiredCapabilities);
396
443
 
444
+ // The block's own model turns the params into its initial storage, and may decline them.
445
+ // Asked before the template is cached and before the transaction, so a refusal changes
446
+ // nothing on the backend.
447
+ let initialStorage: string | undefined;
448
+ if (ops.init !== undefined) {
449
+ if (blockCfg.modelAPIVersion !== BLOCK_STORAGE_FACADE_VERSION)
450
+ throw new ModelAPIVersionMismatchError(
451
+ blockId,
452
+ BLOCK_STORAGE_FACADE_VERSION,
453
+ blockCfg.modelAPIVersion,
454
+ );
455
+ if (
456
+ blockCfg.blockLifecycleCallbacks[BlockStorageFacadeCallbacks.StorageInitialFromParams] ===
457
+ undefined
458
+ )
459
+ throw new BlockInitParamsRejectedError(
460
+ "this version of the block was built before blocks could be created from " +
461
+ "initialization params; use a newer version of the block",
462
+ );
463
+ const storage = this.env.projectHelper.getInitialStorageFromParamsInVM(
464
+ blockCfg,
465
+ ops.init.params,
466
+ new Map(Object.entries(ops.init.blockIds ?? {})),
467
+ );
468
+ if (storage.error !== undefined)
469
+ throw new BlockInitParamsRejectedError(storage.error.message);
470
+ initialStorage = storage.value;
471
+ }
472
+
397
473
  // Pre-materialize template via cache (separate transaction(s))
398
474
  const cachedBp = await cacheBlockPackTemplate(this.env.pl, preparedBp);
399
475
 
400
476
  // Build NewBlockSpec based on model API version
401
477
  const newBlockSpec =
402
478
  blockCfg.modelAPIVersion === BLOCK_STORAGE_FACADE_VERSION
403
- ? { storageMode: "fromModel" as const, blockPack: cachedBp }
479
+ ? { storageMode: "fromModel" as const, blockPack: cachedBp, initialStorage }
404
480
  : {
405
481
  storageMode: "legacy" as const,
406
482
  blockPack: cachedBp,
@@ -705,6 +781,28 @@ export class Project {
705
781
  await this.projectTree.refreshState();
706
782
  }
707
783
 
784
+ /**
785
+ * Reads the block's initialization params as its own model projects them from the current
786
+ * storage, with the kind the block declares: the entry a template export writes for this block.
787
+ * Passing the result as {@link AddBlockOps.init} to {@link addBlock} with the same block pack
788
+ * recreates the block's configuration.
789
+ *
790
+ * @returns `kind` is undefined for a block that declares none; `params` is undefined when the
791
+ * block has no storage yet
792
+ * @throws when the project has no such block, or the block's model fails to project its params
793
+ */
794
+ public async getBlockInitializationParams(
795
+ blockId: BlockId,
796
+ ): Promise<{ kind: BlockKindReference | undefined; params: unknown }> {
797
+ return await withProject(
798
+ this.env.projectHelper,
799
+ this.env.pl,
800
+ this.rid,
801
+ (prj) => prj.getBlockInitializationParams(blockId),
802
+ { name: "getBlockInitializationParams", lockId: this.projectLockId },
803
+ );
804
+ }
805
+
708
806
  /** Update block settings */
709
807
  public async setBlockSettings(blockId: string, newValue: BlockSettings) {
710
808
  await withProjectAuthored(
@@ -87,7 +87,7 @@ import {
87
87
  type DerivedArgs,
88
88
  type ArgsErrorsFieldChange,
89
89
  } from "../model/args_derivation";
90
- import type { ArgsIssue } from "@milaboratories/pl-model-common";
90
+ import type { ArgsIssue, BlockId } from "@milaboratories/pl-model-common";
91
91
  import {
92
92
  extractConfig,
93
93
  UiError,
@@ -621,6 +621,28 @@ export class ProjectMutator {
621
621
  );
622
622
  }
623
623
 
624
+ /**
625
+ * The block's initialization params as its own model projects them from the current storage,
626
+ * with the kind the block declares: what a template export writes for this block.
627
+ *
628
+ * Read-only, like {@link exportAsTemplateV1}.
629
+ *
630
+ * @returns `params` is undefined when the block has no storage yet
631
+ * @throws when the project has no such block, or the block's model fails to project its params
632
+ */
633
+ public getBlockInitializationParams(blockId: BlockId): {
634
+ kind: BlockKindReference | undefined;
635
+ params: unknown;
636
+ } {
637
+ const info = this.getBlockInfo(blockId);
638
+ const derived = this.deriveTemplateParams(blockId);
639
+ if (derived?.error !== undefined)
640
+ throw new Error(
641
+ `Block ${blockId} failed to derive its initialization params: ${derived.error}`,
642
+ );
643
+ return { kind: info.kind, params: derived?.value };
644
+ }
645
+
624
646
  private deriveTemplateParams(blockId: string): TemplateParamsResult | undefined {
625
647
  const info = this.blockInfos.get(blockId);
626
648
  // A block in the structure with no loaded info, or with no storage yet, has no