@lssm/lib.workflow-composer 0.0.0-canary-20251217063201 → 0.0.0-canary-20251217072406

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/composer.js CHANGED
@@ -1 +1,28 @@
1
- import{applyWorkflowExtension as e}from"./injector.js";var t=class{extensions=[];register(e){return this.extensions.push(e),this}registerMany(e){return e.forEach(e=>this.register(e)),this}compose(t){return this.extensions.filter(e=>n(t,e)).sort((e,t)=>(e.priority??0)-(t.priority??0)).reduce((t,n)=>e(t,n),t.base)}};function n(e,t){return!(t.workflow!==e.base.meta.name||t.baseVersion&&t.baseVersion!==e.base.meta.version||t.tenantId&&t.tenantId!==e.tenantId||t.role&&t.role!==e.role||t.device&&t.device!==e.device)}export{t as WorkflowComposer};
1
+ import { applyWorkflowExtension } from "./injector.js";
2
+
3
+ //#region src/composer.ts
4
+ var WorkflowComposer = class {
5
+ extensions = [];
6
+ register(extension) {
7
+ this.extensions.push(extension);
8
+ return this;
9
+ }
10
+ registerMany(extensions) {
11
+ extensions.forEach((extension) => this.register(extension));
12
+ return this;
13
+ }
14
+ compose(params) {
15
+ return this.extensions.filter((extension) => matches(params, extension)).sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0)).reduce((acc, extension) => applyWorkflowExtension(acc, extension), params.base);
16
+ }
17
+ };
18
+ function matches(params, extension) {
19
+ if (extension.workflow !== params.base.meta.name) return false;
20
+ if (extension.baseVersion && extension.baseVersion !== params.base.meta.version) return false;
21
+ if (extension.tenantId && extension.tenantId !== params.tenantId) return false;
22
+ if (extension.role && extension.role !== params.role) return false;
23
+ if (extension.device && extension.device !== params.device) return false;
24
+ return true;
25
+ }
26
+
27
+ //#endregion
28
+ export { WorkflowComposer };
package/dist/index.js CHANGED
@@ -1 +1,7 @@
1
- import{approvalStepTemplate as e}from"./templates.js";import{validateExtension as t}from"./validator.js";import{applyWorkflowExtension as n}from"./injector.js";import{WorkflowComposer as r}from"./composer.js";import{mergeExtensions as i}from"./merger.js";export{r as WorkflowComposer,n as applyWorkflowExtension,e as approvalStepTemplate,i as mergeExtensions,t as validateExtension};
1
+ import { approvalStepTemplate } from "./templates.js";
2
+ import { validateExtension } from "./validator.js";
3
+ import { applyWorkflowExtension } from "./injector.js";
4
+ import { WorkflowComposer } from "./composer.js";
5
+ import { mergeExtensions } from "./merger.js";
6
+
7
+ export { WorkflowComposer, applyWorkflowExtension, approvalStepTemplate, mergeExtensions, validateExtension };
package/dist/injector.js CHANGED
@@ -1 +1,72 @@
1
- import{validateExtension as e}from"./validator.js";function t(t,r){e(r,t);let s=o(t),c=[...s.definition.steps],l=[...s.definition.transitions],u=new Set(r.hiddenSteps??[]);return u.forEach(e=>{let t=c.findIndex(t=>t.id===e);t!==-1&&c.splice(t,1)}),u.size&&(l=l.filter(e=>!u.has(e.from)&&!u.has(e.to))),r.customSteps?.forEach(e=>{n(c,e),i(l,e)}),s.definition.steps=c,s.definition.transitions=a(l),s.meta={...s.meta,version:s.meta.version},s}function n(e,t){let n=r(e,t);if(n===-1)throw Error(`Unable to place injected step "${t.inject.id}"`);e.splice(n,0,{...t.inject})}function r(e,t){if(t.after){let n=e.findIndex(e=>e.id===t.after);return n===-1?-1:n+1}if(t.before){let n=e.findIndex(e=>e.id===t.before);return n===-1?-1:n}return e.length}function i(e,t){t.inject.id&&(t.transitionFrom&&e.push({from:t.transitionFrom,to:t.inject.id,condition:t.when}),t.transitionTo&&e.push({from:t.inject.id,to:t.transitionTo,condition:t.when}))}function a(e){let t=new Set,n=[];return e.forEach(e=>{let r=`${e.from}->${e.to}:${e.condition??``}`;t.has(r)||(t.add(r),n.push(e))}),n}function o(e){return JSON.parse(JSON.stringify(e))}export{t as applyWorkflowExtension};
1
+ import { validateExtension } from "./validator.js";
2
+
3
+ //#region src/injector.ts
4
+ function applyWorkflowExtension(base, extension) {
5
+ validateExtension(extension, base);
6
+ const spec = cloneWorkflowSpec(base);
7
+ const steps = [...spec.definition.steps];
8
+ let transitions = [...spec.definition.transitions];
9
+ const hiddenSet = new Set(extension.hiddenSteps ?? []);
10
+ hiddenSet.forEach((stepId) => {
11
+ const idx = steps.findIndex((step) => step.id === stepId);
12
+ if (idx !== -1) steps.splice(idx, 1);
13
+ });
14
+ if (hiddenSet.size) transitions = transitions.filter((transition) => !hiddenSet.has(transition.from) && !hiddenSet.has(transition.to));
15
+ extension.customSteps?.forEach((injection) => {
16
+ insertStep(steps, injection);
17
+ wireTransitions(transitions, injection);
18
+ });
19
+ spec.definition.steps = steps;
20
+ spec.definition.transitions = dedupeTransitions(transitions);
21
+ spec.meta = {
22
+ ...spec.meta,
23
+ version: spec.meta.version
24
+ };
25
+ return spec;
26
+ }
27
+ function insertStep(steps, injection) {
28
+ const anchorIndex = resolveAnchorIndex(steps, injection);
29
+ if (anchorIndex === -1) throw new Error(`Unable to place injected step "${injection.inject.id}"`);
30
+ steps.splice(anchorIndex, 0, { ...injection.inject });
31
+ }
32
+ function resolveAnchorIndex(steps, injection) {
33
+ if (injection.after) {
34
+ const idx = steps.findIndex((step) => step.id === injection.after);
35
+ return idx === -1 ? -1 : idx + 1;
36
+ }
37
+ if (injection.before) {
38
+ const idx = steps.findIndex((step) => step.id === injection.before);
39
+ return idx === -1 ? -1 : idx;
40
+ }
41
+ return steps.length;
42
+ }
43
+ function wireTransitions(transitions, injection) {
44
+ if (!injection.inject.id) return;
45
+ if (injection.transitionFrom) transitions.push({
46
+ from: injection.transitionFrom,
47
+ to: injection.inject.id,
48
+ condition: injection.when
49
+ });
50
+ if (injection.transitionTo) transitions.push({
51
+ from: injection.inject.id,
52
+ to: injection.transitionTo,
53
+ condition: injection.when
54
+ });
55
+ }
56
+ function dedupeTransitions(transitions) {
57
+ const seen = /* @__PURE__ */ new Set();
58
+ const result = [];
59
+ transitions.forEach((transition) => {
60
+ const key = `${transition.from}->${transition.to}:${transition.condition ?? ""}`;
61
+ if (seen.has(key)) return;
62
+ seen.add(key);
63
+ result.push(transition);
64
+ });
65
+ return result;
66
+ }
67
+ function cloneWorkflowSpec(spec) {
68
+ return JSON.parse(JSON.stringify(spec));
69
+ }
70
+
71
+ //#endregion
72
+ export { applyWorkflowExtension };
package/dist/merger.js CHANGED
@@ -1 +1,7 @@
1
- function e(e){return e.sort((e,t)=>(e.priority??0)-(t.priority??0))}export{e as mergeExtensions};
1
+ //#region src/merger.ts
2
+ function mergeExtensions(extensions) {
3
+ return extensions.sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0));
4
+ }
5
+
6
+ //#endregion
7
+ export { mergeExtensions };
package/dist/templates.js CHANGED
@@ -1 +1,17 @@
1
- function e(e){return{id:e.id,label:e.label,type:e.type??`human`,description:e.description??`Tenant-specific approval`,action:e.action,guard:e.guardExpression?{type:`expression`,value:e.guardExpression}:void 0}}export{e as approvalStepTemplate};
1
+ //#region src/templates.ts
2
+ function approvalStepTemplate(options) {
3
+ return {
4
+ id: options.id,
5
+ label: options.label,
6
+ type: options.type ?? "human",
7
+ description: options.description ?? "Tenant-specific approval",
8
+ action: options.action,
9
+ guard: options.guardExpression ? {
10
+ type: "expression",
11
+ value: options.guardExpression
12
+ } : void 0
13
+ };
14
+ }
15
+
16
+ //#endregion
17
+ export { approvalStepTemplate };
package/dist/validator.js CHANGED
@@ -1 +1,36 @@
1
- function e(e,t){let n=[],r=new Set(t.definition.steps.map(e=>e.id));if(e.customSteps?.forEach((e,t)=>{e.inject.id||n.push({code:`workflow.extension.step.id`,message:`customSteps[${t}] is missing an id`}),!e.after&&!e.before&&n.push({code:`workflow.extension.step.anchor`,message:`customSteps[${t}] must set after or before`}),e.after&&!r.has(e.after)&&n.push({code:`workflow.extension.step.after`,message:`customSteps[${t}] references unknown step "${e.after}"`}),e.before&&!r.has(e.before)&&n.push({code:`workflow.extension.step.before`,message:`customSteps[${t}] references unknown step "${e.before}"`})}),e.hiddenSteps?.forEach(e=>{r.has(e)||n.push({code:`workflow.extension.hidden-step`,message:`hidden step "${e}" does not exist`})}),n.length){let t=n.map(e=>`${e.code}: ${e.message}`).join(`; `);throw Error(`Invalid workflow extension for ${e.workflow}: ${t}`)}}export{e as validateExtension};
1
+ //#region src/validator.ts
2
+ function validateExtension(extension, base) {
3
+ const issues = [];
4
+ const stepIds = new Set(base.definition.steps.map((step) => step.id));
5
+ extension.customSteps?.forEach((injection, idx) => {
6
+ if (!injection.inject.id) issues.push({
7
+ code: "workflow.extension.step.id",
8
+ message: `customSteps[${idx}] is missing an id`
9
+ });
10
+ if (!injection.after && !injection.before) issues.push({
11
+ code: "workflow.extension.step.anchor",
12
+ message: `customSteps[${idx}] must set after or before`
13
+ });
14
+ if (injection.after && !stepIds.has(injection.after)) issues.push({
15
+ code: "workflow.extension.step.after",
16
+ message: `customSteps[${idx}] references unknown step "${injection.after}"`
17
+ });
18
+ if (injection.before && !stepIds.has(injection.before)) issues.push({
19
+ code: "workflow.extension.step.before",
20
+ message: `customSteps[${idx}] references unknown step "${injection.before}"`
21
+ });
22
+ });
23
+ extension.hiddenSteps?.forEach((stepId) => {
24
+ if (!stepIds.has(stepId)) issues.push({
25
+ code: "workflow.extension.hidden-step",
26
+ message: `hidden step "${stepId}" does not exist`
27
+ });
28
+ });
29
+ if (issues.length) {
30
+ const reason = issues.map((issue) => `${issue.code}: ${issue.message}`).join("; ");
31
+ throw new Error(`Invalid workflow extension for ${extension.workflow}: ${reason}`);
32
+ }
33
+ }
34
+
35
+ //#endregion
36
+ export { validateExtension };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lssm/lib.workflow-composer",
3
- "version": "0.0.0-canary-20251217063201",
3
+ "version": "0.0.0-canary-20251217072406",
4
4
  "description": "Tenant-aware workflow composition helpers for ContractSpec.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -24,11 +24,11 @@
24
24
  "test": "bun run"
25
25
  },
26
26
  "dependencies": {
27
- "@lssm/lib.contracts": "0.0.0-canary-20251217063201"
27
+ "@lssm/lib.contracts": "0.0.0-canary-20251217072406"
28
28
  },
29
29
  "devDependencies": {
30
- "@lssm/tool.tsdown": "0.0.0-canary-20251217063201",
31
- "@lssm/tool.typescript": "0.0.0-canary-20251217063201",
30
+ "@lssm/tool.tsdown": "0.0.0-canary-20251217072406",
31
+ "@lssm/tool.typescript": "0.0.0-canary-20251217072406",
32
32
  "tsdown": "^0.17.4",
33
33
  "typescript": "^5.9.3"
34
34
  },