@milaboratories/pl-middle-layer 2.7.9 → 2.7.10

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.
@@ -30,6 +30,7 @@ function createBlockActionContext(tx, baggage) {
30
30
  name: baggage.name,
31
31
  "block-pack": baggage.blockPack,
32
32
  "block-id": baggage.blockId,
33
+ "project-id": baggage.projectId,
33
34
  "run-id": (0, node_crypto.randomUUID)()
34
35
  }));
35
36
  }
@@ -1 +1 @@
1
- {"version":3,"file":"render_block.cjs","names":["randomUUID","isResourceId","parseSignedResourceId","createRenderTemplateWithResource","field","Pl"],"sources":["../../../src/mutator/template/render_block.ts"],"sourcesContent":["import type {\n AnyRef,\n AnyResourceRef,\n PlTransaction,\n ResourceRef,\n ResourceType,\n} from \"@milaboratories/pl-client\";\nimport { field, isResourceId, parseSignedResourceId, Pl } from \"@milaboratories/pl-client\";\nimport { randomUUID } from \"node:crypto\";\nimport { createRenderTemplateWithResource } from \"./render_template\";\n\nexport const BContextEnd: ResourceType = { name: \"BContextEnd\", version: \"1\" };\nexport const BContext: ResourceType = { name: \"BContext\", version: \"1\" };\nexport const BContextId = \"id\";\nexport const BContextParent = \"parent\";\nexport const BContextMultiParentPrefix = \"parent/\";\n\n// TODO: add implementation for dual context heavy block.\n// export type BlockType =\n// | 'LightBlock'\n// | 'HeavyBlock'\n// | 'DualContextHeavyBlock';\n// moved to project model ==>>>\n\nexport type HeavyBlockInputs = {\n args: AnyRef;\n blockId: AnyRef;\n isProduction: AnyRef;\n context: AnyRef;\n};\n\nexport type HeavyBlockOutputs = {\n context: AnyRef;\n result: AnyRef;\n};\n\nexport const HeavyBlockOutputNames: (keyof HeavyBlockOutputs)[] = [\"context\", \"result\"];\n\n/** Baggage of the root action context of a block render. See the block action context spec. */\nexport type BlockActionContextBaggage = {\n /** Display name of the block. */\n name: string;\n /** JSON of the block pack spec. */\n blockPack: string;\n blockId: string;\n};\n\n/** Resource type of a block action context. See ActionContextV1Type in the backend. */\nexport const ActionContextType: ResourceType = { name: \"ActionContext\", version: \"1\" };\n\n/** The capability under which the backend accepts the \"actionContext\" link on a render. */\nexport const ActionContextLinkCapability = \"optionalFieldsInSchemas:v1\";\n\n/**\n * Creates the root action context of a block render: an ActionContext resource carrying the\n * baggage, with a new random `run-id`, since each render is one run of the block and the contexts\n * the templates create copy it. Undefined without baggage, or on a backend that cannot take the link.\n */\nfunction createBlockActionContext(\n tx: PlTransaction,\n baggage: BlockActionContextBaggage | undefined,\n): ResourceRef | undefined {\n if (baggage === undefined || !tx.hasCapability(ActionContextLinkCapability)) return undefined;\n return tx.createEphemeral(\n ActionContextType,\n JSON.stringify({\n name: baggage.name,\n \"block-pack\": baggage.blockPack,\n \"block-id\": baggage.blockId,\n \"run-id\": randomUUID(),\n }),\n );\n}\n\n/** The namespace the creator of a context reports under, as the backend reserves it. */\nexport const ActionContextCreatorNamespace = \"core\";\n\n/** The reason recorded when the user stops a run. */\nexport const BlockRunCancelledReason = \"cancelled\";\n\n/**\n * Marks a block run's root action context as cancelled by the user.\n *\n * Stopping a block destroys its render, and a destroyed render reports nothing, so without this\n * every node of a stopped run would read as live forever. The middle layer created the context, so\n * it reports under the creator's namespace, with the context itself as the reporting resource and\n * this machine's clock, the same key layout the backend's reporters use.\n */\nexport function recordBlockRunCancelled(tx: PlTransaction, context: AnyResourceRef): void {\n const reporter = isResourceId(context) ? String(parseSignedResourceId(context).globalId) : \"0\";\n const ns = (BigInt(Date.now()) * 1_000_000n).toString();\n tx.setKValue(\n context,\n `transition/${ActionContextCreatorNamespace}/${reporter}/${ns}`,\n JSON.stringify({ state: \"Terminated\", detail: \"\", reason: BlockRunCancelledReason }),\n );\n}\n\n/**\n * Creates the heavy block render, and with `baggage` its root action context in the same\n * transaction. Returns `actionContext`: the render is garbage collected after its outputs resolve,\n * so the caller must hold the context to keep it.\n */\nexport function createRenderHeavyBlock(\n tx: PlTransaction,\n tpl: AnyRef,\n inputs: HeavyBlockInputs,\n baggage?: BlockActionContextBaggage,\n): HeavyBlockOutputs & { actionContext?: ResourceRef } {\n const actionContext = createBlockActionContext(tx, baggage);\n const { outputs } = createRenderTemplateWithResource(\n tx,\n tpl,\n true,\n inputs,\n HeavyBlockOutputNames,\n actionContext,\n );\n return actionContext === undefined ? outputs : { ...outputs, actionContext };\n}\n\nexport type LightBlockInputs = {\n args: AnyRef;\n blockId: AnyRef;\n stagingContext: AnyRef;\n productionContext: AnyRef;\n};\n\nexport type LightBlockOutput = {\n result: AnyRef;\n};\n\nexport const LightBlockOutputNames: (keyof LightBlockOutput)[] = [\"result\"];\n\n/**\n * Creates the light block render, with the same action context as a heavy one, so template code\n * of either kind knows its block and run.\n */\nexport function createRenderLightBlock(\n tx: PlTransaction,\n tpl: ResourceRef,\n inputs: LightBlockInputs,\n baggage?: BlockActionContextBaggage,\n): LightBlockOutput & { actionContext?: ResourceRef } {\n const actionContext = createBlockActionContext(tx, baggage);\n const { outputs } = createRenderTemplateWithResource(\n tx,\n tpl,\n true,\n inputs,\n LightBlockOutputNames,\n actionContext,\n );\n return actionContext === undefined ? outputs : { ...outputs, actionContext };\n}\n\nexport function createBContextEnd(tx: PlTransaction): ResourceRef {\n const ctx = tx.createEphemeral(BContextEnd);\n tx.lock(ctx);\n return ctx;\n}\n\nexport function createBContextFromUpstreams(tx: PlTransaction, upstreamCtxs: AnyRef[]): AnyRef {\n if (upstreamCtxs.length === 0) return createBContextEnd(tx);\n\n if (upstreamCtxs.length === 1) return upstreamCtxs[0];\n\n const ctx = tx.createEphemeral(BContext);\n\n // setting id\n tx.createField(field(ctx, BContextId), \"Input\", Pl.createPlString(tx, randomUUID()));\n\n // setting parents\n for (let i = 0; i < upstreamCtxs.length; i++)\n tx.createField(field(ctx, `${BContextMultiParentPrefix}${i}`), \"Input\", upstreamCtxs[i]);\n\n tx.lock(ctx);\n\n return ctx;\n}\n"],"mappings":";;;;AAWA,MAAa,cAA4B;CAAE,MAAM;CAAe,SAAS;AAAI;AAC7E,MAAa,WAAyB;CAAE,MAAM;CAAY,SAAS;AAAI;AAGvE,MAAa,4BAA4B;AAqBzC,MAAa,wBAAqD,CAAC,WAAW,QAAQ;;AAYtF,MAAa,oBAAkC;CAAE,MAAM;CAAiB,SAAS;AAAI;;AAGrF,MAAa,8BAA8B;;;;;;AAO3C,SAAS,yBACP,IACA,SACyB;CACzB,IAAI,YAAY,KAAA,KAAa,CAAC,GAAG,cAAA,4BAAyC,GAAG,OAAO,KAAA;CACpF,OAAO,GAAG,gBACR,mBACA,KAAK,UAAU;EACb,MAAM,QAAQ;EACd,cAAc,QAAQ;EACtB,YAAY,QAAQ;EACpB,WAAA,GAAUA,YAAAA,WAAAA,CAAW;CACvB,CAAC,CACH;AACF;;AAGA,MAAa,gCAAgC;;AAG7C,MAAa,0BAA0B;;;;;;;;;AAUvC,SAAgB,wBAAwB,IAAmB,SAA+B;CACxF,MAAM,YAAA,GAAWC,0BAAAA,aAAAA,CAAa,OAAO,IAAI,QAAA,GAAOC,0BAAAA,sBAAAA,CAAsB,OAAO,CAAC,CAAC,QAAQ,IAAI;CAC3F,MAAM,MAAM,OAAO,KAAK,IAAI,CAAC,IAAI,SAAA,CAAY,SAAS;CACtD,GAAG,UACD,SACA,cAAc,8BAA8B,GAAG,SAAS,GAAG,MAC3D,KAAK,UAAU;EAAE,OAAO;EAAc,QAAQ;EAAI,QAAQ;CAAwB,CAAC,CACrF;AACF;;;;;;AAOA,SAAgB,uBACd,IACA,KACA,QACA,SACqD;CACrD,MAAM,gBAAgB,yBAAyB,IAAI,OAAO;CAC1D,MAAM,EAAE,YAAYC,wBAAAA,iCAClB,IACA,KACA,MACA,QACA,uBACA,aACF;CACA,OAAO,kBAAkB,KAAA,IAAY,UAAU;EAAE,GAAG;EAAS;CAAc;AAC7E;AAqCA,SAAgB,kBAAkB,IAAgC;CAChE,MAAM,MAAM,GAAG,gBAAgB,WAAW;CAC1C,GAAG,KAAK,GAAG;CACX,OAAO;AACT;AAEA,SAAgB,4BAA4B,IAAmB,cAAgC;CAC7F,IAAI,aAAa,WAAW,GAAG,OAAO,kBAAkB,EAAE;CAE1D,IAAI,aAAa,WAAW,GAAG,OAAO,aAAa;CAEnD,MAAM,MAAM,GAAG,gBAAgB,QAAQ;CAGvC,GAAG,aAAA,GAAYC,0BAAAA,MAAAA,CAAM,KAAA,IAAe,GAAG,SAASC,0BAAAA,GAAG,eAAe,KAAA,GAAIL,YAAAA,WAAAA,CAAW,CAAC,CAAC;CAGnF,KAAK,IAAI,IAAI,GAAG,IAAI,aAAa,QAAQ,KACvC,GAAG,aAAA,GAAYI,0BAAAA,MAAAA,CAAM,KAAK,GAAG,4BAA4B,GAAG,GAAG,SAAS,aAAa,EAAE;CAEzF,GAAG,KAAK,GAAG;CAEX,OAAO;AACT"}
1
+ {"version":3,"file":"render_block.cjs","names":["randomUUID","isResourceId","parseSignedResourceId","createRenderTemplateWithResource","field","Pl"],"sources":["../../../src/mutator/template/render_block.ts"],"sourcesContent":["import type {\n AnyRef,\n AnyResourceRef,\n PlTransaction,\n ResourceRef,\n ResourceType,\n} from \"@milaboratories/pl-client\";\nimport { field, isResourceId, parseSignedResourceId, Pl } from \"@milaboratories/pl-client\";\nimport { randomUUID } from \"node:crypto\";\nimport { createRenderTemplateWithResource } from \"./render_template\";\n\nexport const BContextEnd: ResourceType = { name: \"BContextEnd\", version: \"1\" };\nexport const BContext: ResourceType = { name: \"BContext\", version: \"1\" };\nexport const BContextId = \"id\";\nexport const BContextParent = \"parent\";\nexport const BContextMultiParentPrefix = \"parent/\";\n\n// TODO: add implementation for dual context heavy block.\n// export type BlockType =\n// | 'LightBlock'\n// | 'HeavyBlock'\n// | 'DualContextHeavyBlock';\n// moved to project model ==>>>\n\nexport type HeavyBlockInputs = {\n args: AnyRef;\n blockId: AnyRef;\n isProduction: AnyRef;\n context: AnyRef;\n};\n\nexport type HeavyBlockOutputs = {\n context: AnyRef;\n result: AnyRef;\n};\n\nexport const HeavyBlockOutputNames: (keyof HeavyBlockOutputs)[] = [\"context\", \"result\"];\n\n/** Baggage of the root action context of a block render. See the block action context spec. */\nexport type BlockActionContextBaggage = {\n /** Display name of the block. */\n name: string;\n /** JSON of the block pack spec. */\n blockPack: string;\n blockId: string;\n /** Resource id of the project, as the project overview shows it (`NG:0x…`). */\n projectId: string;\n};\n\n/** Resource type of a block action context. See ActionContextV1Type in the backend. */\nexport const ActionContextType: ResourceType = { name: \"ActionContext\", version: \"1\" };\n\n/** The capability under which the backend accepts the \"actionContext\" link on a render. */\nexport const ActionContextLinkCapability = \"optionalFieldsInSchemas:v1\";\n\n/**\n * Creates the root action context of a block render: an ActionContext resource carrying the\n * baggage, with a new random `run-id`, since each render is one run of the block and the contexts\n * the templates create copy it. Undefined without baggage, or on a backend that cannot take the link.\n */\nfunction createBlockActionContext(\n tx: PlTransaction,\n baggage: BlockActionContextBaggage | undefined,\n): ResourceRef | undefined {\n if (baggage === undefined || !tx.hasCapability(ActionContextLinkCapability)) return undefined;\n return tx.createEphemeral(\n ActionContextType,\n JSON.stringify({\n name: baggage.name,\n \"block-pack\": baggage.blockPack,\n \"block-id\": baggage.blockId,\n \"project-id\": baggage.projectId,\n \"run-id\": randomUUID(),\n }),\n );\n}\n\n/** The namespace the creator of a context reports under, as the backend reserves it. */\nexport const ActionContextCreatorNamespace = \"core\";\n\n/** The reason recorded when the user stops a run. */\nexport const BlockRunCancelledReason = \"cancelled\";\n\n/**\n * Marks a block run's root action context as cancelled by the user.\n *\n * Stopping a block destroys its render, and a destroyed render reports nothing, so without this\n * every node of a stopped run would read as live forever. The middle layer created the context, so\n * it reports under the creator's namespace, with the context itself as the reporting resource and\n * this machine's clock, the same key layout the backend's reporters use.\n */\nexport function recordBlockRunCancelled(tx: PlTransaction, context: AnyResourceRef): void {\n const reporter = isResourceId(context) ? String(parseSignedResourceId(context).globalId) : \"0\";\n const ns = (BigInt(Date.now()) * 1_000_000n).toString();\n tx.setKValue(\n context,\n `transition/${ActionContextCreatorNamespace}/${reporter}/${ns}`,\n JSON.stringify({ state: \"Terminated\", detail: \"\", reason: BlockRunCancelledReason }),\n );\n}\n\n/**\n * Creates the heavy block render, and with `baggage` its root action context in the same\n * transaction. Returns `actionContext`: the render is garbage collected after its outputs resolve,\n * so the caller must hold the context to keep it.\n */\nexport function createRenderHeavyBlock(\n tx: PlTransaction,\n tpl: AnyRef,\n inputs: HeavyBlockInputs,\n baggage?: BlockActionContextBaggage,\n): HeavyBlockOutputs & { actionContext?: ResourceRef } {\n const actionContext = createBlockActionContext(tx, baggage);\n const { outputs } = createRenderTemplateWithResource(\n tx,\n tpl,\n true,\n inputs,\n HeavyBlockOutputNames,\n actionContext,\n );\n return actionContext === undefined ? outputs : { ...outputs, actionContext };\n}\n\nexport type LightBlockInputs = {\n args: AnyRef;\n blockId: AnyRef;\n stagingContext: AnyRef;\n productionContext: AnyRef;\n};\n\nexport type LightBlockOutput = {\n result: AnyRef;\n};\n\nexport const LightBlockOutputNames: (keyof LightBlockOutput)[] = [\"result\"];\n\n/**\n * Creates the light block render, with the same action context as a heavy one, so template code\n * of either kind knows its block and run.\n */\nexport function createRenderLightBlock(\n tx: PlTransaction,\n tpl: ResourceRef,\n inputs: LightBlockInputs,\n baggage?: BlockActionContextBaggage,\n): LightBlockOutput & { actionContext?: ResourceRef } {\n const actionContext = createBlockActionContext(tx, baggage);\n const { outputs } = createRenderTemplateWithResource(\n tx,\n tpl,\n true,\n inputs,\n LightBlockOutputNames,\n actionContext,\n );\n return actionContext === undefined ? outputs : { ...outputs, actionContext };\n}\n\nexport function createBContextEnd(tx: PlTransaction): ResourceRef {\n const ctx = tx.createEphemeral(BContextEnd);\n tx.lock(ctx);\n return ctx;\n}\n\nexport function createBContextFromUpstreams(tx: PlTransaction, upstreamCtxs: AnyRef[]): AnyRef {\n if (upstreamCtxs.length === 0) return createBContextEnd(tx);\n\n if (upstreamCtxs.length === 1) return upstreamCtxs[0];\n\n const ctx = tx.createEphemeral(BContext);\n\n // setting id\n tx.createField(field(ctx, BContextId), \"Input\", Pl.createPlString(tx, randomUUID()));\n\n // setting parents\n for (let i = 0; i < upstreamCtxs.length; i++)\n tx.createField(field(ctx, `${BContextMultiParentPrefix}${i}`), \"Input\", upstreamCtxs[i]);\n\n tx.lock(ctx);\n\n return ctx;\n}\n"],"mappings":";;;;AAWA,MAAa,cAA4B;CAAE,MAAM;CAAe,SAAS;AAAI;AAC7E,MAAa,WAAyB;CAAE,MAAM;CAAY,SAAS;AAAI;AAGvE,MAAa,4BAA4B;AAqBzC,MAAa,wBAAqD,CAAC,WAAW,QAAQ;;AActF,MAAa,oBAAkC;CAAE,MAAM;CAAiB,SAAS;AAAI;;AAGrF,MAAa,8BAA8B;;;;;;AAO3C,SAAS,yBACP,IACA,SACyB;CACzB,IAAI,YAAY,KAAA,KAAa,CAAC,GAAG,cAAA,4BAAyC,GAAG,OAAO,KAAA;CACpF,OAAO,GAAG,gBACR,mBACA,KAAK,UAAU;EACb,MAAM,QAAQ;EACd,cAAc,QAAQ;EACtB,YAAY,QAAQ;EACpB,cAAc,QAAQ;EACtB,WAAA,GAAUA,YAAAA,WAAAA,CAAW;CACvB,CAAC,CACH;AACF;;AAGA,MAAa,gCAAgC;;AAG7C,MAAa,0BAA0B;;;;;;;;;AAUvC,SAAgB,wBAAwB,IAAmB,SAA+B;CACxF,MAAM,YAAA,GAAWC,0BAAAA,aAAAA,CAAa,OAAO,IAAI,QAAA,GAAOC,0BAAAA,sBAAAA,CAAsB,OAAO,CAAC,CAAC,QAAQ,IAAI;CAC3F,MAAM,MAAM,OAAO,KAAK,IAAI,CAAC,IAAI,SAAA,CAAY,SAAS;CACtD,GAAG,UACD,SACA,cAAc,8BAA8B,GAAG,SAAS,GAAG,MAC3D,KAAK,UAAU;EAAE,OAAO;EAAc,QAAQ;EAAI,QAAQ;CAAwB,CAAC,CACrF;AACF;;;;;;AAOA,SAAgB,uBACd,IACA,KACA,QACA,SACqD;CACrD,MAAM,gBAAgB,yBAAyB,IAAI,OAAO;CAC1D,MAAM,EAAE,YAAYC,wBAAAA,iCAClB,IACA,KACA,MACA,QACA,uBACA,aACF;CACA,OAAO,kBAAkB,KAAA,IAAY,UAAU;EAAE,GAAG;EAAS;CAAc;AAC7E;AAqCA,SAAgB,kBAAkB,IAAgC;CAChE,MAAM,MAAM,GAAG,gBAAgB,WAAW;CAC1C,GAAG,KAAK,GAAG;CACX,OAAO;AACT;AAEA,SAAgB,4BAA4B,IAAmB,cAAgC;CAC7F,IAAI,aAAa,WAAW,GAAG,OAAO,kBAAkB,EAAE;CAE1D,IAAI,aAAa,WAAW,GAAG,OAAO,aAAa;CAEnD,MAAM,MAAM,GAAG,gBAAgB,QAAQ;CAGvC,GAAG,aAAA,GAAYC,0BAAAA,MAAAA,CAAM,KAAA,IAAe,GAAG,SAASC,0BAAAA,GAAG,eAAe,KAAA,GAAIL,YAAAA,WAAAA,CAAW,CAAC,CAAC;CAGnF,KAAK,IAAI,IAAI,GAAG,IAAI,aAAa,QAAQ,KACvC,GAAG,aAAA,GAAYI,0BAAAA,MAAAA,CAAM,KAAK,GAAG,4BAA4B,GAAG,GAAG,SAAS,aAAa,EAAE;CAEzF,GAAG,KAAK,GAAG;CAEX,OAAO;AACT"}
@@ -30,6 +30,7 @@ function createBlockActionContext(tx, baggage) {
30
30
  name: baggage.name,
31
31
  "block-pack": baggage.blockPack,
32
32
  "block-id": baggage.blockId,
33
+ "project-id": baggage.projectId,
33
34
  "run-id": randomUUID()
34
35
  }));
35
36
  }
@@ -1 +1 @@
1
- {"version":3,"file":"render_block.js","names":[],"sources":["../../../src/mutator/template/render_block.ts"],"sourcesContent":["import type {\n AnyRef,\n AnyResourceRef,\n PlTransaction,\n ResourceRef,\n ResourceType,\n} from \"@milaboratories/pl-client\";\nimport { field, isResourceId, parseSignedResourceId, Pl } from \"@milaboratories/pl-client\";\nimport { randomUUID } from \"node:crypto\";\nimport { createRenderTemplateWithResource } from \"./render_template\";\n\nexport const BContextEnd: ResourceType = { name: \"BContextEnd\", version: \"1\" };\nexport const BContext: ResourceType = { name: \"BContext\", version: \"1\" };\nexport const BContextId = \"id\";\nexport const BContextParent = \"parent\";\nexport const BContextMultiParentPrefix = \"parent/\";\n\n// TODO: add implementation for dual context heavy block.\n// export type BlockType =\n// | 'LightBlock'\n// | 'HeavyBlock'\n// | 'DualContextHeavyBlock';\n// moved to project model ==>>>\n\nexport type HeavyBlockInputs = {\n args: AnyRef;\n blockId: AnyRef;\n isProduction: AnyRef;\n context: AnyRef;\n};\n\nexport type HeavyBlockOutputs = {\n context: AnyRef;\n result: AnyRef;\n};\n\nexport const HeavyBlockOutputNames: (keyof HeavyBlockOutputs)[] = [\"context\", \"result\"];\n\n/** Baggage of the root action context of a block render. See the block action context spec. */\nexport type BlockActionContextBaggage = {\n /** Display name of the block. */\n name: string;\n /** JSON of the block pack spec. */\n blockPack: string;\n blockId: string;\n};\n\n/** Resource type of a block action context. See ActionContextV1Type in the backend. */\nexport const ActionContextType: ResourceType = { name: \"ActionContext\", version: \"1\" };\n\n/** The capability under which the backend accepts the \"actionContext\" link on a render. */\nexport const ActionContextLinkCapability = \"optionalFieldsInSchemas:v1\";\n\n/**\n * Creates the root action context of a block render: an ActionContext resource carrying the\n * baggage, with a new random `run-id`, since each render is one run of the block and the contexts\n * the templates create copy it. Undefined without baggage, or on a backend that cannot take the link.\n */\nfunction createBlockActionContext(\n tx: PlTransaction,\n baggage: BlockActionContextBaggage | undefined,\n): ResourceRef | undefined {\n if (baggage === undefined || !tx.hasCapability(ActionContextLinkCapability)) return undefined;\n return tx.createEphemeral(\n ActionContextType,\n JSON.stringify({\n name: baggage.name,\n \"block-pack\": baggage.blockPack,\n \"block-id\": baggage.blockId,\n \"run-id\": randomUUID(),\n }),\n );\n}\n\n/** The namespace the creator of a context reports under, as the backend reserves it. */\nexport const ActionContextCreatorNamespace = \"core\";\n\n/** The reason recorded when the user stops a run. */\nexport const BlockRunCancelledReason = \"cancelled\";\n\n/**\n * Marks a block run's root action context as cancelled by the user.\n *\n * Stopping a block destroys its render, and a destroyed render reports nothing, so without this\n * every node of a stopped run would read as live forever. The middle layer created the context, so\n * it reports under the creator's namespace, with the context itself as the reporting resource and\n * this machine's clock, the same key layout the backend's reporters use.\n */\nexport function recordBlockRunCancelled(tx: PlTransaction, context: AnyResourceRef): void {\n const reporter = isResourceId(context) ? String(parseSignedResourceId(context).globalId) : \"0\";\n const ns = (BigInt(Date.now()) * 1_000_000n).toString();\n tx.setKValue(\n context,\n `transition/${ActionContextCreatorNamespace}/${reporter}/${ns}`,\n JSON.stringify({ state: \"Terminated\", detail: \"\", reason: BlockRunCancelledReason }),\n );\n}\n\n/**\n * Creates the heavy block render, and with `baggage` its root action context in the same\n * transaction. Returns `actionContext`: the render is garbage collected after its outputs resolve,\n * so the caller must hold the context to keep it.\n */\nexport function createRenderHeavyBlock(\n tx: PlTransaction,\n tpl: AnyRef,\n inputs: HeavyBlockInputs,\n baggage?: BlockActionContextBaggage,\n): HeavyBlockOutputs & { actionContext?: ResourceRef } {\n const actionContext = createBlockActionContext(tx, baggage);\n const { outputs } = createRenderTemplateWithResource(\n tx,\n tpl,\n true,\n inputs,\n HeavyBlockOutputNames,\n actionContext,\n );\n return actionContext === undefined ? outputs : { ...outputs, actionContext };\n}\n\nexport type LightBlockInputs = {\n args: AnyRef;\n blockId: AnyRef;\n stagingContext: AnyRef;\n productionContext: AnyRef;\n};\n\nexport type LightBlockOutput = {\n result: AnyRef;\n};\n\nexport const LightBlockOutputNames: (keyof LightBlockOutput)[] = [\"result\"];\n\n/**\n * Creates the light block render, with the same action context as a heavy one, so template code\n * of either kind knows its block and run.\n */\nexport function createRenderLightBlock(\n tx: PlTransaction,\n tpl: ResourceRef,\n inputs: LightBlockInputs,\n baggage?: BlockActionContextBaggage,\n): LightBlockOutput & { actionContext?: ResourceRef } {\n const actionContext = createBlockActionContext(tx, baggage);\n const { outputs } = createRenderTemplateWithResource(\n tx,\n tpl,\n true,\n inputs,\n LightBlockOutputNames,\n actionContext,\n );\n return actionContext === undefined ? outputs : { ...outputs, actionContext };\n}\n\nexport function createBContextEnd(tx: PlTransaction): ResourceRef {\n const ctx = tx.createEphemeral(BContextEnd);\n tx.lock(ctx);\n return ctx;\n}\n\nexport function createBContextFromUpstreams(tx: PlTransaction, upstreamCtxs: AnyRef[]): AnyRef {\n if (upstreamCtxs.length === 0) return createBContextEnd(tx);\n\n if (upstreamCtxs.length === 1) return upstreamCtxs[0];\n\n const ctx = tx.createEphemeral(BContext);\n\n // setting id\n tx.createField(field(ctx, BContextId), \"Input\", Pl.createPlString(tx, randomUUID()));\n\n // setting parents\n for (let i = 0; i < upstreamCtxs.length; i++)\n tx.createField(field(ctx, `${BContextMultiParentPrefix}${i}`), \"Input\", upstreamCtxs[i]);\n\n tx.lock(ctx);\n\n return ctx;\n}\n"],"mappings":";;;;AAWA,MAAa,cAA4B;CAAE,MAAM;CAAe,SAAS;AAAI;AAC7E,MAAa,WAAyB;CAAE,MAAM;CAAY,SAAS;AAAI;AAGvE,MAAa,4BAA4B;AAqBzC,MAAa,wBAAqD,CAAC,WAAW,QAAQ;;AAYtF,MAAa,oBAAkC;CAAE,MAAM;CAAiB,SAAS;AAAI;;AAGrF,MAAa,8BAA8B;;;;;;AAO3C,SAAS,yBACP,IACA,SACyB;CACzB,IAAI,YAAY,KAAA,KAAa,CAAC,GAAG,cAAA,4BAAyC,GAAG,OAAO,KAAA;CACpF,OAAO,GAAG,gBACR,mBACA,KAAK,UAAU;EACb,MAAM,QAAQ;EACd,cAAc,QAAQ;EACtB,YAAY,QAAQ;EACpB,UAAU,WAAW;CACvB,CAAC,CACH;AACF;;AAGA,MAAa,gCAAgC;;AAG7C,MAAa,0BAA0B;;;;;;;;;AAUvC,SAAgB,wBAAwB,IAAmB,SAA+B;CACxF,MAAM,WAAW,aAAa,OAAO,IAAI,OAAO,sBAAsB,OAAO,CAAC,CAAC,QAAQ,IAAI;CAC3F,MAAM,MAAM,OAAO,KAAK,IAAI,CAAC,IAAI,SAAA,CAAY,SAAS;CACtD,GAAG,UACD,SACA,cAAc,8BAA8B,GAAG,SAAS,GAAG,MAC3D,KAAK,UAAU;EAAE,OAAO;EAAc,QAAQ;EAAI,QAAQ;CAAwB,CAAC,CACrF;AACF;;;;;;AAOA,SAAgB,uBACd,IACA,KACA,QACA,SACqD;CACrD,MAAM,gBAAgB,yBAAyB,IAAI,OAAO;CAC1D,MAAM,EAAE,YAAY,iCAClB,IACA,KACA,MACA,QACA,uBACA,aACF;CACA,OAAO,kBAAkB,KAAA,IAAY,UAAU;EAAE,GAAG;EAAS;CAAc;AAC7E;AAqCA,SAAgB,kBAAkB,IAAgC;CAChE,MAAM,MAAM,GAAG,gBAAgB,WAAW;CAC1C,GAAG,KAAK,GAAG;CACX,OAAO;AACT;AAEA,SAAgB,4BAA4B,IAAmB,cAAgC;CAC7F,IAAI,aAAa,WAAW,GAAG,OAAO,kBAAkB,EAAE;CAE1D,IAAI,aAAa,WAAW,GAAG,OAAO,aAAa;CAEnD,MAAM,MAAM,GAAG,gBAAgB,QAAQ;CAGvC,GAAG,YAAY,MAAM,KAAA,IAAe,GAAG,SAAS,GAAG,eAAe,IAAI,WAAW,CAAC,CAAC;CAGnF,KAAK,IAAI,IAAI,GAAG,IAAI,aAAa,QAAQ,KACvC,GAAG,YAAY,MAAM,KAAK,GAAG,4BAA4B,GAAG,GAAG,SAAS,aAAa,EAAE;CAEzF,GAAG,KAAK,GAAG;CAEX,OAAO;AACT"}
1
+ {"version":3,"file":"render_block.js","names":[],"sources":["../../../src/mutator/template/render_block.ts"],"sourcesContent":["import type {\n AnyRef,\n AnyResourceRef,\n PlTransaction,\n ResourceRef,\n ResourceType,\n} from \"@milaboratories/pl-client\";\nimport { field, isResourceId, parseSignedResourceId, Pl } from \"@milaboratories/pl-client\";\nimport { randomUUID } from \"node:crypto\";\nimport { createRenderTemplateWithResource } from \"./render_template\";\n\nexport const BContextEnd: ResourceType = { name: \"BContextEnd\", version: \"1\" };\nexport const BContext: ResourceType = { name: \"BContext\", version: \"1\" };\nexport const BContextId = \"id\";\nexport const BContextParent = \"parent\";\nexport const BContextMultiParentPrefix = \"parent/\";\n\n// TODO: add implementation for dual context heavy block.\n// export type BlockType =\n// | 'LightBlock'\n// | 'HeavyBlock'\n// | 'DualContextHeavyBlock';\n// moved to project model ==>>>\n\nexport type HeavyBlockInputs = {\n args: AnyRef;\n blockId: AnyRef;\n isProduction: AnyRef;\n context: AnyRef;\n};\n\nexport type HeavyBlockOutputs = {\n context: AnyRef;\n result: AnyRef;\n};\n\nexport const HeavyBlockOutputNames: (keyof HeavyBlockOutputs)[] = [\"context\", \"result\"];\n\n/** Baggage of the root action context of a block render. See the block action context spec. */\nexport type BlockActionContextBaggage = {\n /** Display name of the block. */\n name: string;\n /** JSON of the block pack spec. */\n blockPack: string;\n blockId: string;\n /** Resource id of the project, as the project overview shows it (`NG:0x…`). */\n projectId: string;\n};\n\n/** Resource type of a block action context. See ActionContextV1Type in the backend. */\nexport const ActionContextType: ResourceType = { name: \"ActionContext\", version: \"1\" };\n\n/** The capability under which the backend accepts the \"actionContext\" link on a render. */\nexport const ActionContextLinkCapability = \"optionalFieldsInSchemas:v1\";\n\n/**\n * Creates the root action context of a block render: an ActionContext resource carrying the\n * baggage, with a new random `run-id`, since each render is one run of the block and the contexts\n * the templates create copy it. Undefined without baggage, or on a backend that cannot take the link.\n */\nfunction createBlockActionContext(\n tx: PlTransaction,\n baggage: BlockActionContextBaggage | undefined,\n): ResourceRef | undefined {\n if (baggage === undefined || !tx.hasCapability(ActionContextLinkCapability)) return undefined;\n return tx.createEphemeral(\n ActionContextType,\n JSON.stringify({\n name: baggage.name,\n \"block-pack\": baggage.blockPack,\n \"block-id\": baggage.blockId,\n \"project-id\": baggage.projectId,\n \"run-id\": randomUUID(),\n }),\n );\n}\n\n/** The namespace the creator of a context reports under, as the backend reserves it. */\nexport const ActionContextCreatorNamespace = \"core\";\n\n/** The reason recorded when the user stops a run. */\nexport const BlockRunCancelledReason = \"cancelled\";\n\n/**\n * Marks a block run's root action context as cancelled by the user.\n *\n * Stopping a block destroys its render, and a destroyed render reports nothing, so without this\n * every node of a stopped run would read as live forever. The middle layer created the context, so\n * it reports under the creator's namespace, with the context itself as the reporting resource and\n * this machine's clock, the same key layout the backend's reporters use.\n */\nexport function recordBlockRunCancelled(tx: PlTransaction, context: AnyResourceRef): void {\n const reporter = isResourceId(context) ? String(parseSignedResourceId(context).globalId) : \"0\";\n const ns = (BigInt(Date.now()) * 1_000_000n).toString();\n tx.setKValue(\n context,\n `transition/${ActionContextCreatorNamespace}/${reporter}/${ns}`,\n JSON.stringify({ state: \"Terminated\", detail: \"\", reason: BlockRunCancelledReason }),\n );\n}\n\n/**\n * Creates the heavy block render, and with `baggage` its root action context in the same\n * transaction. Returns `actionContext`: the render is garbage collected after its outputs resolve,\n * so the caller must hold the context to keep it.\n */\nexport function createRenderHeavyBlock(\n tx: PlTransaction,\n tpl: AnyRef,\n inputs: HeavyBlockInputs,\n baggage?: BlockActionContextBaggage,\n): HeavyBlockOutputs & { actionContext?: ResourceRef } {\n const actionContext = createBlockActionContext(tx, baggage);\n const { outputs } = createRenderTemplateWithResource(\n tx,\n tpl,\n true,\n inputs,\n HeavyBlockOutputNames,\n actionContext,\n );\n return actionContext === undefined ? outputs : { ...outputs, actionContext };\n}\n\nexport type LightBlockInputs = {\n args: AnyRef;\n blockId: AnyRef;\n stagingContext: AnyRef;\n productionContext: AnyRef;\n};\n\nexport type LightBlockOutput = {\n result: AnyRef;\n};\n\nexport const LightBlockOutputNames: (keyof LightBlockOutput)[] = [\"result\"];\n\n/**\n * Creates the light block render, with the same action context as a heavy one, so template code\n * of either kind knows its block and run.\n */\nexport function createRenderLightBlock(\n tx: PlTransaction,\n tpl: ResourceRef,\n inputs: LightBlockInputs,\n baggage?: BlockActionContextBaggage,\n): LightBlockOutput & { actionContext?: ResourceRef } {\n const actionContext = createBlockActionContext(tx, baggage);\n const { outputs } = createRenderTemplateWithResource(\n tx,\n tpl,\n true,\n inputs,\n LightBlockOutputNames,\n actionContext,\n );\n return actionContext === undefined ? outputs : { ...outputs, actionContext };\n}\n\nexport function createBContextEnd(tx: PlTransaction): ResourceRef {\n const ctx = tx.createEphemeral(BContextEnd);\n tx.lock(ctx);\n return ctx;\n}\n\nexport function createBContextFromUpstreams(tx: PlTransaction, upstreamCtxs: AnyRef[]): AnyRef {\n if (upstreamCtxs.length === 0) return createBContextEnd(tx);\n\n if (upstreamCtxs.length === 1) return upstreamCtxs[0];\n\n const ctx = tx.createEphemeral(BContext);\n\n // setting id\n tx.createField(field(ctx, BContextId), \"Input\", Pl.createPlString(tx, randomUUID()));\n\n // setting parents\n for (let i = 0; i < upstreamCtxs.length; i++)\n tx.createField(field(ctx, `${BContextMultiParentPrefix}${i}`), \"Input\", upstreamCtxs[i]);\n\n tx.lock(ctx);\n\n return ctx;\n}\n"],"mappings":";;;;AAWA,MAAa,cAA4B;CAAE,MAAM;CAAe,SAAS;AAAI;AAC7E,MAAa,WAAyB;CAAE,MAAM;CAAY,SAAS;AAAI;AAGvE,MAAa,4BAA4B;AAqBzC,MAAa,wBAAqD,CAAC,WAAW,QAAQ;;AActF,MAAa,oBAAkC;CAAE,MAAM;CAAiB,SAAS;AAAI;;AAGrF,MAAa,8BAA8B;;;;;;AAO3C,SAAS,yBACP,IACA,SACyB;CACzB,IAAI,YAAY,KAAA,KAAa,CAAC,GAAG,cAAA,4BAAyC,GAAG,OAAO,KAAA;CACpF,OAAO,GAAG,gBACR,mBACA,KAAK,UAAU;EACb,MAAM,QAAQ;EACd,cAAc,QAAQ;EACtB,YAAY,QAAQ;EACpB,cAAc,QAAQ;EACtB,UAAU,WAAW;CACvB,CAAC,CACH;AACF;;AAGA,MAAa,gCAAgC;;AAG7C,MAAa,0BAA0B;;;;;;;;;AAUvC,SAAgB,wBAAwB,IAAmB,SAA+B;CACxF,MAAM,WAAW,aAAa,OAAO,IAAI,OAAO,sBAAsB,OAAO,CAAC,CAAC,QAAQ,IAAI;CAC3F,MAAM,MAAM,OAAO,KAAK,IAAI,CAAC,IAAI,SAAA,CAAY,SAAS;CACtD,GAAG,UACD,SACA,cAAc,8BAA8B,GAAG,SAAS,GAAG,MAC3D,KAAK,UAAU;EAAE,OAAO;EAAc,QAAQ;EAAI,QAAQ;CAAwB,CAAC,CACrF;AACF;;;;;;AAOA,SAAgB,uBACd,IACA,KACA,QACA,SACqD;CACrD,MAAM,gBAAgB,yBAAyB,IAAI,OAAO;CAC1D,MAAM,EAAE,YAAY,iCAClB,IACA,KACA,MACA,QACA,uBACA,aACF;CACA,OAAO,kBAAkB,KAAA,IAAY,UAAU;EAAE,GAAG;EAAS;CAAc;AAC7E;AAqCA,SAAgB,kBAAkB,IAAgC;CAChE,MAAM,MAAM,GAAG,gBAAgB,WAAW;CAC1C,GAAG,KAAK,GAAG;CACX,OAAO;AACT;AAEA,SAAgB,4BAA4B,IAAmB,cAAgC;CAC7F,IAAI,aAAa,WAAW,GAAG,OAAO,kBAAkB,EAAE;CAE1D,IAAI,aAAa,WAAW,GAAG,OAAO,aAAa;CAEnD,MAAM,MAAM,GAAG,gBAAgB,QAAQ;CAGvC,GAAG,YAAY,MAAM,KAAA,IAAe,GAAG,SAAS,GAAG,eAAe,IAAI,WAAW,CAAC,CAAC;CAGnF,KAAK,IAAI,IAAI,GAAG,IAAI,aAAa,QAAQ,KACvC,GAAG,YAAY,MAAM,KAAK,GAAG,4BAA4B,GAAG,GAAG,SAAS,aAAa,EAAE;CAEzF,GAAG,KAAK,GAAG;CAEX,OAAO;AACT"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@milaboratories/pl-middle-layer",
3
- "version": "2.7.9",
3
+ "version": "2.7.10",
4
4
  "description": "Pl Middle Layer",
5
5
  "keywords": [],
6
6
  "license": "UNLICENSED",
@@ -19,6 +19,7 @@ import {
19
19
  Pl,
20
20
  PlClient,
21
21
  readyOrDuplicateOrError,
22
+ resourceIdToString,
22
23
  } from "@milaboratories/pl-client";
23
24
  import {
24
25
  createRenderHeavyBlock,
@@ -740,6 +741,7 @@ export class ProjectMutator {
740
741
  name: this.getBlock(blockId).label,
741
742
  blockPack: JSON.stringify(info.source),
742
743
  blockId,
744
+ projectId: resourceIdToString(this.rid),
743
745
  };
744
746
  }
745
747
 
@@ -6,6 +6,7 @@ import {
6
6
  isNullSignedResourceId,
7
7
  parseSignedResourceId,
8
8
  poll,
9
+ resourceIdToString,
9
10
  TestHelpers,
10
11
  toGlobalResourceId,
11
12
  } from "@milaboratories/pl-client";
@@ -121,8 +122,15 @@ test("block action context outlives its renderer and is replaced on re-render",
121
122
  }
122
123
  const first = (await readActionContext(pl, prj, "block1"))!;
123
124
  expect(first).toBeDefined();
124
- expect(Object.keys(first.baggage).sort()).toEqual(["block-id", "block-pack", "name", "run-id"]);
125
+ expect(Object.keys(first.baggage).sort()).toEqual([
126
+ "block-id",
127
+ "block-pack",
128
+ "name",
129
+ "project-id",
130
+ "run-id",
131
+ ]);
125
132
  expect(first.baggage["block-id"]).toEqual("block1");
133
+ expect(first.baggage["project-id"]).toEqual(resourceIdToString(prj));
126
134
  expect(first.baggage["name"]).toEqual("Block1");
127
135
  expect(JSON.parse(first.baggage["block-pack"])).toBeTypeOf("object");
128
136
  expect(first.baggage["run-id"]).toMatch(/^[0-9a-f-]{36}$/);
@@ -43,6 +43,8 @@ export type BlockActionContextBaggage = {
43
43
  /** JSON of the block pack spec. */
44
44
  blockPack: string;
45
45
  blockId: string;
46
+ /** Resource id of the project, as the project overview shows it (`NG:0x…`). */
47
+ projectId: string;
46
48
  };
47
49
 
48
50
  /** Resource type of a block action context. See ActionContextV1Type in the backend. */
@@ -67,6 +69,7 @@ function createBlockActionContext(
67
69
  name: baggage.name,
68
70
  "block-pack": baggage.blockPack,
69
71
  "block-id": baggage.blockId,
72
+ "project-id": baggage.projectId,
70
73
  "run-id": randomUUID(),
71
74
  }),
72
75
  );