@platforma-sdk/model 1.87.0 → 1.88.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 (54) hide show
  1. package/dist/block_api_v3.d.ts +9 -0
  2. package/dist/block_api_v3.d.ts.map +1 -1
  3. package/dist/block_model.cjs +89 -9
  4. package/dist/block_model.cjs.map +1 -1
  5. package/dist/block_model.d.ts +81 -19
  6. package/dist/block_model.d.ts.map +1 -1
  7. package/dist/block_model.js +89 -9
  8. package/dist/block_model.js.map +1 -1
  9. package/dist/block_storage_callbacks.cjs +128 -25
  10. package/dist/block_storage_callbacks.cjs.map +1 -1
  11. package/dist/block_storage_callbacks.js +129 -26
  12. package/dist/block_storage_callbacks.js.map +1 -1
  13. package/dist/block_storage_facade.cjs.map +1 -1
  14. package/dist/block_storage_facade.d.ts +37 -4
  15. package/dist/block_storage_facade.d.ts.map +1 -1
  16. package/dist/block_storage_facade.js.map +1 -1
  17. package/dist/columns/column_values.cjs +126 -0
  18. package/dist/columns/column_values.cjs.map +1 -0
  19. package/dist/columns/column_values.d.ts +65 -0
  20. package/dist/columns/column_values.d.ts.map +1 -0
  21. package/dist/columns/column_values.js +124 -0
  22. package/dist/columns/column_values.js.map +1 -0
  23. package/dist/columns/index.cjs +4 -0
  24. package/dist/columns/index.d.ts +2 -1
  25. package/dist/columns/index.js +2 -1
  26. package/dist/index.cjs +6 -0
  27. package/dist/index.d.ts +3 -1
  28. package/dist/index.js +3 -1
  29. package/dist/package.cjs +1 -1
  30. package/dist/package.js +1 -1
  31. package/dist/pframe_utils/column_value_options.cjs +85 -0
  32. package/dist/pframe_utils/column_value_options.cjs.map +1 -0
  33. package/dist/pframe_utils/column_value_options.d.ts +14 -0
  34. package/dist/pframe_utils/column_value_options.d.ts.map +1 -0
  35. package/dist/pframe_utils/column_value_options.js +85 -0
  36. package/dist/pframe_utils/column_value_options.js.map +1 -0
  37. package/dist/pframe_utils/index.cjs +2 -0
  38. package/dist/pframe_utils/index.cjs.map +1 -1
  39. package/dist/pframe_utils/index.d.ts +2 -1
  40. package/dist/pframe_utils/index.d.ts.map +1 -1
  41. package/dist/pframe_utils/index.js +2 -1
  42. package/dist/pframe_utils/index.js.map +1 -1
  43. package/package.json +7 -7
  44. package/src/args_issues.test.ts +532 -0
  45. package/src/block_api_v3.ts +10 -0
  46. package/src/block_model.ts +146 -32
  47. package/src/block_storage_callbacks.ts +174 -43
  48. package/src/block_storage_facade.ts +36 -6
  49. package/src/columns/column_values.test.ts +183 -0
  50. package/src/columns/column_values.ts +194 -0
  51. package/src/columns/index.ts +1 -0
  52. package/src/pframe_utils/column_value_options.test.ts +126 -0
  53. package/src/pframe_utils/column_value_options.ts +111 -0
  54. package/src/pframe_utils/index.ts +1 -0
@@ -6,6 +6,7 @@ import type {
6
6
  BlockCodeKnownFeatureFlags,
7
7
  BlockConfigContainer,
8
8
  BlockKindReference,
9
+ ArgsIssue,
9
10
  } from "@milaboratories/pl-model-common";
10
11
  import { REQUIRES_PFRAMES_VERSION, formatKindRef } from "@milaboratories/pl-model-common";
11
12
  import { getPlatformaInstance, isInUI, createAndRegisterRenderLambda } from "./internal";
@@ -127,7 +128,11 @@ interface BlockModelV3Config<
127
128
  enrichmentTargets: ConfigRenderLambda | undefined;
128
129
  featureFlags: BlockCodeKnownFeatureFlags;
129
130
  deriveArgs: ((data: unknown) => unknown) | undefined;
131
+ /** Finds what keeps the derived args from running; set only together with `deriveArgs`. */
132
+ argsIssues: ((input: { args: unknown; data: unknown }) => ArgsIssue[]) | undefined;
130
133
  derivePrerunArgs: ((data: unknown) => unknown) | undefined;
134
+ /** Finds what keeps the prerun args from staging; set only together with `derivePrerunArgs`. */
135
+ prerunArgsIssues: ((input: { prerunArgs: unknown; data: unknown }) => ArgsIssue[]) | undefined;
131
136
  /** Projects block data back to this kind's params for template export. */
132
137
  deriveTemplateParams: ((data: Data) => Params) | undefined;
133
138
  /**
@@ -152,6 +157,7 @@ export class BlockModelV3<
152
157
  Plugins extends Record<string, PluginRecord> = {},
153
158
  Transfers extends Record<string, unknown> = {},
154
159
  Params = unknown,
160
+ PrerunArgs = unknown,
155
161
  > {
156
162
  private constructor(
157
163
  private readonly config: BlockModelV3Config<OutputsCfg, Data, Plugins, Transfers, Params>,
@@ -160,10 +166,12 @@ export class BlockModelV3<
160
166
  public static readonly FEATURE_FLAGS = {
161
167
  supportsLazyState: true,
162
168
  supportsPframeQueryRanking: true,
169
+ supportsColumnLinks: true,
163
170
  requiresUIAPIVersion: 3,
164
171
  requiresModelAPIVersion: BLOCK_STORAGE_FACADE_VERSION,
165
172
  requiresCreatePTable: 2,
166
173
  requiresPFramesVersion: REQUIRES_PFRAMES_VERSION,
174
+ requiresArgsIssues: true,
167
175
  ...BLOCK_SERVICE_FLAGS,
168
176
  } satisfies BlockCodeKnownFeatureFlags;
169
177
 
@@ -228,7 +236,9 @@ export class BlockModelV3<
228
236
  enrichmentTargets: undefined,
229
237
  featureFlags: { ...BlockModelV3.FEATURE_FLAGS },
230
238
  deriveArgs: undefined,
239
+ argsIssues: undefined,
231
240
  derivePrerunArgs: undefined,
241
+ prerunArgsIssues: undefined,
232
242
  deriveTemplateParams: undefined,
233
243
  plugins: {},
234
244
  });
@@ -257,7 +267,8 @@ export class BlockModelV3<
257
267
  Href,
258
268
  Plugins,
259
269
  Transfers,
260
- Params
270
+ Params,
271
+ PrerunArgs
261
272
  >;
262
273
  /**
263
274
  * Add output cell to the configuration
@@ -280,13 +291,14 @@ export class BlockModelV3<
280
291
  Href,
281
292
  Plugins,
282
293
  Transfers,
283
- Params
294
+ Params,
295
+ PrerunArgs
284
296
  >;
285
297
  public output(
286
298
  key: string,
287
299
  cfgOrRf: RenderFunction<Args, Data, unknown>,
288
300
  flags: ConfigRenderLambdaFlags = {},
289
- ): BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params> {
301
+ ): BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs> {
290
302
  return new BlockModelV3({
291
303
  ...this.config,
292
304
  outputs: {
@@ -316,7 +328,8 @@ export class BlockModelV3<
316
328
  Href,
317
329
  Plugins,
318
330
  Transfers,
319
- Params
331
+ Params,
332
+ PrerunArgs
320
333
  > {
321
334
  return this.output(key, rf, { retentive: true });
322
335
  }
@@ -333,21 +346,63 @@ export class BlockModelV3<
333
346
  * Sets a function to derive block args from data.
334
347
  * This is called during setData to compute the args that will be used for block execution.
335
348
  *
349
+ * Build args even from incomplete data, and say what keeps the block from running in
350
+ * {@link argsIssues}: args that exist let the platform see what the block references
351
+ * before it is ready to run. Throwing here still works, and means there are no args at all.
352
+ *
353
+ * Setting args again drops a {@link argsIssues} set before, since it checked other args.
354
+ *
336
355
  * @example
337
356
  * .args<BlockArgs>((data) => ({ numbers: data.numbers }))
338
357
  *
339
358
  * @example
340
- * .args<BlockArgs>((data) => {
341
- * if (data.numbers.length === 0) throw new Error('Numbers required'); // block not ready
342
- * return { numbers: data.numbers };
343
- * })
359
+ * .args<BlockArgs>((data) => ({ numbers: data.numbers }))
360
+ * .argsIssues(({ args }) =>
361
+ * args.numbers.length === 0 ? [{ severity: 'error', message: 'Numbers required' }] : [],
362
+ * )
344
363
  */
345
364
  public args<A>(
346
365
  lambda: (data: Data) => A,
347
- ): BlockModelV3<A, OutputsCfg, Data, Href, Plugins, Transfers, Params> {
348
- return new BlockModelV3<A, OutputsCfg, Data, Href, Plugins, Transfers, Params>({
366
+ ): BlockModelV3<A, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs> {
367
+ return new BlockModelV3<A, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs>({
349
368
  ...this.config,
350
369
  deriveArgs: lambda as (data: unknown) => unknown,
370
+ argsIssues: undefined,
371
+ });
372
+ }
373
+
374
+ /**
375
+ * Sets a function that finds what keeps the block from running with the args {@link args}
376
+ * derived (optional). It is called after `args()`, with its result and the data it was built
377
+ * from.
378
+ *
379
+ * An issue of severity `"error"` keeps the block from running; a `"warning"` does not. A throw,
380
+ * or a result that is not a list of issues, counts as one error carrying the thrown or parser
381
+ * message. Without this function, args that `args()` returns are always runnable.
382
+ *
383
+ * A block without its own {@link prerunArgs} is staged with its args, and only while this
384
+ * function finds no errors in them.
385
+ *
386
+ * Must be set after `.args()`, and is dropped when `.args()` is set again.
387
+ *
388
+ * @example
389
+ * .args((data) => ({ dataset: data.dataset, threshold: data.threshold }))
390
+ * .argsIssues(({ args }) => {
391
+ * const issues: ArgsIssue[] = [];
392
+ * if (args.dataset === undefined) issues.push({ severity: 'error', message: 'Select a dataset' });
393
+ * if (args.threshold > 0.5) issues.push({ severity: 'warning', message: 'Threshold is high' });
394
+ * return issues;
395
+ * })
396
+ */
397
+ public argsIssues(
398
+ fn: (input: { args: Args; data: Data }) => ArgsIssue[],
399
+ ): BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs> {
400
+ if (this.config.deriveArgs === undefined) {
401
+ throw new Error("argsIssues() must be set after args(): it checks the args they derive.");
402
+ }
403
+ return new BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs>({
404
+ ...this.config,
405
+ argsIssues: fn as (input: { args: unknown; data: unknown }) => ArgsIssue[],
351
406
  });
352
407
  }
353
408
 
@@ -361,6 +416,9 @@ export class BlockModelV3<
361
416
  * The staging / prerun phase runs only if currentPrerunArgs differs from the executed
362
417
  * version of prerunArgs (same comparison logic as currentArgs vs prodArgs).
363
418
  *
419
+ * Setting prerun args again drops a {@link prerunArgsIssues} set before, since it checked other
420
+ * args.
421
+ *
364
422
  * @example
365
423
  * .prerunArgs((data) => ({ numbers: data.numbers }))
366
424
  *
@@ -371,12 +429,45 @@ export class BlockModelV3<
371
429
  * return { numbers: data.numbers };
372
430
  * })
373
431
  */
374
- public prerunArgs(
375
- fn: (data: Data) => unknown,
376
- ): BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params> {
377
- return new BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params>({
432
+ public prerunArgs<P>(
433
+ fn: (data: Data) => P,
434
+ ): BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, P> {
435
+ return new BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, P>({
378
436
  ...this.config,
379
437
  derivePrerunArgs: fn as (data: unknown) => unknown,
438
+ prerunArgsIssues: undefined,
439
+ });
440
+ }
441
+
442
+ /**
443
+ * Sets a function that finds what keeps the block from being staged with the args
444
+ * {@link prerunArgs} derived (optional). It is called after `prerunArgs()`, with its result
445
+ * and the data it was built from; not when `prerunArgs()` returned `undefined`, which skips
446
+ * staging anyway.
447
+ *
448
+ * An issue of severity `"error"` keeps the block from being staged; a `"warning"` does not. A
449
+ * throw, or a result that is not a list of issues, counts as one error carrying the thrown or
450
+ * parser message. {@link argsIssues} is not consulted for a block with its own prerun args.
451
+ *
452
+ * Must be set after `.prerunArgs()`, and is dropped when `.prerunArgs()` is set again.
453
+ *
454
+ * @example
455
+ * .prerunArgs((data) => ({ dataset: data.dataset }))
456
+ * .prerunArgsIssues(({ prerunArgs }) =>
457
+ * prerunArgs.dataset === undefined ? [{ severity: 'error', message: 'Select a dataset' }] : [],
458
+ * )
459
+ */
460
+ public prerunArgsIssues(
461
+ fn: (input: { prerunArgs: Exclude<PrerunArgs, undefined>; data: Data }) => ArgsIssue[],
462
+ ): BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs> {
463
+ if (this.config.derivePrerunArgs === undefined) {
464
+ throw new Error(
465
+ "prerunArgsIssues() must be set after prerunArgs(): it checks the prerun args they derive.",
466
+ );
467
+ }
468
+ return new BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs>({
469
+ ...this.config,
470
+ prerunArgsIssues: fn as (input: { prerunArgs: unknown; data: unknown }) => ArgsIssue[],
380
471
  });
381
472
  }
382
473
 
@@ -410,8 +501,8 @@ export class BlockModelV3<
410
501
  */
411
502
  public templateParams(
412
503
  fn: (data: Data) => Params,
413
- ): BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params> {
414
- return new BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params>({
504
+ ): BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs> {
505
+ return new BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs>({
415
506
  ...this.config,
416
507
  deriveTemplateParams: fn,
417
508
  });
@@ -423,7 +514,16 @@ export class BlockModelV3<
423
514
  const RF extends RenderFunction<Args, Data, Ret>,
424
515
  >(
425
516
  rf: RF,
426
- ): BlockModelV3<Args, OutputsCfg, Data, DeriveHref<ReturnType<RF>>, Plugins, Transfers, Params> {
517
+ ): BlockModelV3<
518
+ Args,
519
+ OutputsCfg,
520
+ Data,
521
+ DeriveHref<ReturnType<RF>>,
522
+ Plugins,
523
+ Transfers,
524
+ Params,
525
+ PrerunArgs
526
+ > {
427
527
  return new BlockModelV3<
428
528
  Args,
429
529
  OutputsCfg,
@@ -431,7 +531,8 @@ export class BlockModelV3<
431
531
  DeriveHref<ReturnType<RF>>,
432
532
  Plugins,
433
533
  Transfers,
434
- Params
534
+ Params,
535
+ PrerunArgs
435
536
  >({
436
537
  ...this.config,
437
538
  // Replace the default sections callback with the user-provided one
@@ -448,8 +549,8 @@ export class BlockModelV3<
448
549
  /** Sets a rendering function to derive block title, shown for the block in the left blocks-overview panel. */
449
550
  public title(
450
551
  rf: RenderFunction<Args, Data, string>,
451
- ): BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params> {
452
- return new BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params>({
552
+ ): BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs> {
553
+ return new BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs>({
453
554
  ...this.config,
454
555
  title: createAndRegisterRenderLambda({
455
556
  handle: "title",
@@ -460,8 +561,8 @@ export class BlockModelV3<
460
561
 
461
562
  public subtitle(
462
563
  rf: RenderFunction<Args, Data, string>,
463
- ): BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params> {
464
- return new BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params>({
564
+ ): BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs> {
565
+ return new BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs>({
465
566
  ...this.config,
466
567
  subtitle: createAndRegisterRenderLambda({
467
568
  handle: "subtitle",
@@ -472,8 +573,8 @@ export class BlockModelV3<
472
573
 
473
574
  public tags(
474
575
  rf: RenderFunction<Args, Data, string[]>,
475
- ): BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params> {
476
- return new BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params>({
576
+ ): BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs> {
577
+ return new BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs>({
477
578
  ...this.config,
478
579
  tags: createAndRegisterRenderLambda({
479
580
  handle: "tags",
@@ -485,8 +586,8 @@ export class BlockModelV3<
485
586
  /** Sets or overrides feature flags for the block. */
486
587
  public withFeatureFlags(
487
588
  flags: Partial<BlockCodeKnownFeatureFlags>,
488
- ): BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params> {
489
- return new BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params>({
589
+ ): BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs> {
590
+ return new BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs>({
490
591
  ...this.config,
491
592
  featureFlags: { ...this.config.featureFlags, ...flags },
492
593
  });
@@ -498,8 +599,8 @@ export class BlockModelV3<
498
599
  */
499
600
  public enriches(
500
601
  lambda: (args: Args) => PlRef[],
501
- ): BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params> {
502
- return new BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params>({
602
+ ): BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs> {
603
+ return new BlockModelV3<Args, OutputsCfg, Data, Href, Plugins, Transfers, Params, PrerunArgs>({
503
604
  ...this.config,
504
605
  enrichmentTargets: createAndRegisterRenderLambda({
505
606
  handle: "enrichmentTargets",
@@ -571,7 +672,8 @@ export class BlockModelV3<
571
672
  >;
572
673
  },
573
674
  Omit<Transfers, PluginId>,
574
- Params
675
+ Params,
676
+ PrerunArgs
575
677
  >;
576
678
  public plugin(
577
679
  instance: PluginInstanceClass,
@@ -583,7 +685,8 @@ export class BlockModelV3<
583
685
  Href,
584
686
  Record<string, PluginRecord>,
585
687
  Record<string, unknown>,
586
- Params
688
+ Params,
689
+ PrerunArgs
587
690
  > {
588
691
  const pluginId = instance.id;
589
692
  const plugin = instance[CREATE_PLUGIN_MODEL]();
@@ -650,7 +753,9 @@ export class BlockModelV3<
650
753
  const {
651
754
  dataModel,
652
755
  deriveArgs,
756
+ argsIssues,
653
757
  derivePrerunArgs,
758
+ prerunArgsIssues,
654
759
  deriveTemplateParams,
655
760
  parseInitializationParams,
656
761
  } = this.config;
@@ -682,9 +787,14 @@ export class BlockModelV3<
682
787
  createPluginData: (handle) => getPlugin(handle).model.getDefaultData(),
683
788
  }),
684
789
  [BlockStorageFacadeCallbacks.ArgsDerive]: (storageJson) =>
685
- deriveArgsFromStorage(storageJson, deriveArgs),
790
+ deriveArgsFromStorage(storageJson, { deriveArgs, argsIssues }),
686
791
  [BlockStorageFacadeCallbacks.PrerunArgsDerive]: (storageJson) =>
687
- derivePrerunArgsFromStorage(storageJson, deriveArgs, derivePrerunArgs),
792
+ derivePrerunArgsFromStorage(storageJson, {
793
+ deriveArgs,
794
+ argsIssues,
795
+ ownPrerunArgs:
796
+ derivePrerunArgs === undefined ? undefined : { derivePrerunArgs, prerunArgsIssues },
797
+ }),
688
798
  [BlockStorageFacadeCallbacks.InitializationParamsDerive]: (storageJson) =>
689
799
  deriveTemplateParamsFromStorage(
690
800
  storageJson,
@@ -827,7 +937,11 @@ type _ConfigTest = Expect<
827
937
  {
828
938
  renderingMode: BlockRenderingMode;
829
939
  deriveArgs: ((data: unknown) => unknown) | undefined;
940
+ argsIssues: ((input: { args: unknown; data: unknown }) => ArgsIssue[]) | undefined;
830
941
  derivePrerunArgs: ((data: unknown) => unknown) | undefined;
942
+ prerunArgsIssues:
943
+ | ((input: { prerunArgs: unknown; data: unknown }) => ArgsIssue[])
944
+ | undefined;
831
945
  deriveTemplateParams: ((data: _TestData) => unknown) | undefined;
832
946
  parseInitializationParams: (value: unknown) => unknown;
833
947
  dataModel: DataModel<_TestData, unknown, {}>;
@@ -28,7 +28,9 @@ import type { PluginHandle } from "./plugin_handle";
28
28
  import {
29
29
  stringifyJson,
30
30
  expandTemplateRefs,
31
+ parseArgsIssues,
31
32
  relocateBlockIds,
33
+ type ArgsIssue,
32
34
  type StringifiedJson,
33
35
  } from "@milaboratories/pl-model-common";
34
36
  import type { DataVersioned, TransferRecord } from "./block_migrations";
@@ -503,72 +505,138 @@ export function createInitialStorageFromParams(
503
505
  // =============================================================================
504
506
 
505
507
  /**
506
- * Result of args derivation from storage.
507
- * Returned by __pl_args_derive and __pl_prerunArgs_derive callbacks.
508
+ * Result of a derivation that either produced a value or failed.
509
+ * Returned by the __pl_initializationParams_derive callback.
508
510
  */
509
- export type ArgsDeriveResult = { error: string } | { error?: undefined; value: unknown };
511
+ export type DeriveResult = { error: string } | { error?: undefined; value: unknown };
510
512
 
511
513
  /**
512
- * Derives args from storage using the provided args function.
513
- * This extracts data from storage and passes it to the block's args() function.
514
+ * Result of args derivation from storage, returned by the __pl_args_derive callback.
515
+ *
516
+ * A middle layer that predates args issues reads only `error` and `value`, and each variant
517
+ * means the same to it as before: `value` is runnable args, `error` is no args.
518
+ *
519
+ * - `{ error }`: `args()` threw, so there are no args.
520
+ * - `{ error, args, issues }`: args were built, but `argsIssues()` found errors in them, so the
521
+ * block must not run. `error` joins the error messages; `issues` lists every issue found.
522
+ * - `{ value, issues }`: args were built and nothing blocks the run; `issues` holds the warnings.
523
+ */
524
+ export type ArgsDeriveResult =
525
+ | { error: string }
526
+ | { error: string; args: unknown; issues: ArgsIssue[] }
527
+ | { error?: undefined; value: unknown; issues: ArgsIssue[] };
528
+
529
+ /**
530
+ * Result of prerun args derivation from storage, returned by the __pl_prerunArgs_derive
531
+ * callback.
532
+ *
533
+ * A middle layer that predates args issues reads only `error` and `value`, and each variant
534
+ * means the same to it as before: `value` is args to stage, `error` skips staging.
535
+ *
536
+ * - `{ error }`: the lambda that builds the prerun args threw.
537
+ * - `{ error, issues }`: prerun args were built, but the issues found in them include errors, so
538
+ * the block is not staged. `error` joins the error messages; `issues` lists every issue found.
539
+ * - `{ value, issues }`: args to stage; `issues` holds the warnings. A `value` of `undefined`
540
+ * comes from a `prerunArgs()` that returned nothing, and skips staging.
541
+ */
542
+ export type PrerunArgsDeriveResult =
543
+ | { error: string }
544
+ | { error: string; issues: ArgsIssue[] }
545
+ | { error?: undefined; value: unknown; issues: ArgsIssue[] };
546
+
547
+ /** The block's lambdas that turn its data into args and find what keeps those args from running. */
548
+ export interface ArgsDerivation {
549
+ /** The block's `args()` lambda. */
550
+ deriveArgs: (data: unknown) => unknown;
551
+ /** The block's `argsIssues()` lambda; `undefined` when the block declares none. */
552
+ argsIssues: ((input: { args: unknown; data: unknown }) => ArgsIssue[]) | undefined;
553
+ }
554
+
555
+ /** The block's own lambdas for staging: its `prerunArgs()` and the `prerunArgsIssues()` set after it. */
556
+ export interface OwnPrerunArgsDerivation {
557
+ /** The block's `prerunArgs()` lambda. */
558
+ derivePrerunArgs: (data: unknown) => unknown;
559
+ /** The block's `prerunArgsIssues()` lambda; `undefined` when the block declares none. */
560
+ prerunArgsIssues: ((input: { prerunArgs: unknown; data: unknown }) => ArgsIssue[]) | undefined;
561
+ }
562
+
563
+ /** {@link ArgsDerivation} plus the block's own lambdas for staging args. */
564
+ export interface PrerunArgsDerivation extends ArgsDerivation {
565
+ /** The block's own staging lambdas; `undefined` when the block stages its args. */
566
+ ownPrerunArgs: OwnPrerunArgsDerivation | undefined;
567
+ }
568
+
569
+ /**
570
+ * Derives args from storage using the block's args function, then finds the issues in them
571
+ * with the block's `argsIssues()`.
572
+ *
573
+ * A throw from `argsIssues()`, or a result that is not a list of issues, is one error carrying
574
+ * the thrown or parser message, so a broken check keeps the block from running instead of
575
+ * letting it run unchecked.
514
576
  *
515
577
  * @param storageJson - Storage as JSON string
516
- * @param deriveArgs - The block's args derivation function
517
- * @returns ArgsDeriveResult with derived args or error
578
+ * @param derivation - The block's args and args issues lambdas
579
+ * @returns ArgsDeriveResult with derived args and args issues, or error
518
580
  */
519
581
  export function deriveArgsFromStorage(
520
582
  storageJson: string,
521
- deriveArgs: (data: unknown) => unknown,
583
+ derivation: ArgsDerivation,
522
584
  ): ArgsDeriveResult {
523
- // Extract data from storage
524
585
  const { data } = normalizeStorage(storageJson);
525
586
 
526
- // Call the args function with extracted data
527
- try {
528
- const result = deriveArgs(data);
529
- return { value: result };
530
- } catch (e) {
531
- const errorMsg = e instanceof Error ? e.message : String(e);
532
- return { error: `args() threw: ${errorMsg}` };
533
- }
587
+ const run = deriveArgsWithIssues(data, derivation);
588
+ if (run.type === "threw") return { error: `args() threw: ${run.message}` };
589
+
590
+ const { args, issues } = run;
591
+ const errors = errorMessages(issues);
592
+ if (errors.length > 0) return { error: errors.join("; "), args, issues };
593
+ return { value: args, issues };
534
594
  }
535
595
 
536
596
  /**
537
597
  * Derives prerunArgs from storage.
538
- * Uses derivePrerunArgs if provided, otherwise falls back to deriveArgs.
598
+ *
599
+ * A block with its own `prerunArgs()` is staged with what it returns, unless its
600
+ * `prerunArgsIssues()` finds errors in it; `argsIssues()` is not consulted then. A `prerunArgs()`
601
+ * that returns `undefined` skips staging, and `prerunArgsIssues()` is not called for it.
602
+ *
603
+ * A block without its own `prerunArgs()` is staged with its args, unless `argsIssues()` finds
604
+ * errors in them: args that may not run are not staged either.
605
+ *
606
+ * A throw from either issues lambda, or a result that is not a list of issues, is one error
607
+ * carrying the thrown or parser message.
539
608
  *
540
609
  * @param storageJson - Storage as JSON string
541
- * @param deriveArgs - The block's args derivation function (fallback)
542
- * @param derivePrerunArgs - Optional prerun args derivation function
543
- * @returns ArgsDeriveResult with derived prerunArgs or error
610
+ * @param derivation - The block's args, args issues and own staging lambdas
611
+ * @returns PrerunArgsDeriveResult with derived prerunArgs and their issues, or error
544
612
  */
545
613
  export function derivePrerunArgsFromStorage(
546
614
  storageJson: string,
547
- deriveArgs: (data: unknown) => unknown,
548
- derivePrerunArgs?: (data: unknown) => unknown,
549
- ): ArgsDeriveResult {
550
- // Extract data from storage
615
+ derivation: PrerunArgsDerivation,
616
+ ): PrerunArgsDeriveResult {
551
617
  const { data } = normalizeStorage(storageJson);
618
+ const { ownPrerunArgs } = derivation;
552
619
 
553
- // Try prerunArgs function first if available
554
- if (derivePrerunArgs) {
555
- try {
556
- const result = derivePrerunArgs(data);
557
- return { value: result };
558
- } catch (e) {
559
- const errorMsg = e instanceof Error ? e.message : String(e);
560
- return { error: `prerunArgs() threw: ${errorMsg}` };
561
- }
620
+ if (ownPrerunArgs !== undefined) {
621
+ const run = derivePrerunArgsWithIssues(data, ownPrerunArgs);
622
+ if (run.type === "threw") return { error: `prerunArgs() threw: ${run.message}` };
623
+
624
+ const errors = errorMessages(run.issues);
625
+ if (errors.length > 0) return { error: errors.join("; "), issues: run.issues };
626
+ return { value: run.args, issues: run.issues };
562
627
  }
563
628
 
564
- // Fall back to args function
565
- try {
566
- const result = deriveArgs(data);
567
- return { value: result };
568
- } catch (e) {
569
- const errorMsg = e instanceof Error ? e.message : String(e);
570
- return { error: `args() threw (fallback): ${errorMsg}` };
629
+ const run = deriveArgsWithIssues(data, derivation);
630
+ if (run.type === "threw") return { error: `args() threw (fallback): ${run.message}` };
631
+
632
+ const errors = errorMessages(run.issues);
633
+ if (errors.length > 0) {
634
+ return {
635
+ error: `argsIssues() found errors (fallback): ${errors.join("; ")}`,
636
+ issues: run.issues,
637
+ };
571
638
  }
639
+ return { value: run.args, issues: run.issues };
572
640
  }
573
641
 
574
642
  // =============================================================================
@@ -592,12 +660,12 @@ export function derivePrerunArgsFromStorage(
592
660
  *
593
661
  * @param storageJson - Storage as JSON string
594
662
  * @param deriveTemplateParams - The block's templateParams lambda
595
- * @returns ArgsDeriveResult holding the params exactly as the block projected them
663
+ * @returns DeriveResult holding the params exactly as the block projected them
596
664
  */
597
665
  export function deriveTemplateParamsFromStorage<TP extends (data: unknown) => unknown>(
598
666
  storageJson: string,
599
667
  deriveTemplateParams: TP,
600
- ): ArgsDeriveResult {
668
+ ): DeriveResult {
601
669
  const { data } = normalizeStorage(storageJson);
602
670
 
603
671
  try {
@@ -610,3 +678,66 @@ export function deriveTemplateParamsFromStorage<TP extends (data: unknown) => un
610
678
 
611
679
  // Export discriminator key and schema version for external checks
612
680
  export { BLOCK_STORAGE_KEY, BLOCK_STORAGE_SCHEMA_VERSION };
681
+
682
+ // =============================================================================
683
+ // Internals
684
+ // =============================================================================
685
+
686
+ /** What a lambda building args, and the issues lambda checking them, made of a block's data. */
687
+ type ArgsWithIssues =
688
+ | { type: "threw"; message: string }
689
+ | { type: "derived"; args: unknown; issues: ArgsIssue[] };
690
+
691
+ function deriveArgsWithIssues(data: unknown, derivation: ArgsDerivation): ArgsWithIssues {
692
+ const { deriveArgs, argsIssues } = derivation;
693
+
694
+ let args: unknown;
695
+ try {
696
+ args = deriveArgs(data);
697
+ } catch (e) {
698
+ return { type: "threw", message: messageOf(e) };
699
+ }
700
+
701
+ if (argsIssues === undefined) return { type: "derived", args, issues: [] };
702
+ return { type: "derived", args, issues: collectIssues(() => argsIssues({ args, data })) };
703
+ }
704
+
705
+ function derivePrerunArgsWithIssues(
706
+ data: unknown,
707
+ derivation: OwnPrerunArgsDerivation,
708
+ ): ArgsWithIssues {
709
+ const { derivePrerunArgs, prerunArgsIssues } = derivation;
710
+
711
+ let prerunArgs: unknown;
712
+ try {
713
+ prerunArgs = derivePrerunArgs(data);
714
+ } catch (e) {
715
+ return { type: "threw", message: messageOf(e) };
716
+ }
717
+
718
+ if (prerunArgsIssues === undefined || prerunArgs === undefined) {
719
+ return { type: "derived", args: prerunArgs, issues: [] };
720
+ }
721
+ return {
722
+ type: "derived",
723
+ args: prerunArgs,
724
+ issues: collectIssues(() => prerunArgsIssues({ prerunArgs, data })),
725
+ };
726
+ }
727
+
728
+ /**
729
+ * Calls an issues lambda of the block. Its result is parsed rather than trusted: a block's types
730
+ * do not hold at run time, and an entry that cannot be read must count as an error, not slip
731
+ * past. A throw, or a result that cannot be read, is one error carrying its message.
732
+ */
733
+ function collectIssues(issuesOf: () => unknown): ArgsIssue[] {
734
+ try {
735
+ return parseArgsIssues(issuesOf());
736
+ } catch (e) {
737
+ return [{ severity: "error", message: messageOf(e) }];
738
+ }
739
+ }
740
+
741
+ function errorMessages(issues: ArgsIssue[]): string[] {
742
+ return issues.filter((issue) => issue.severity !== "warning").map((issue) => issue.message);
743
+ }