@mat3ra/wode 2026.6.5-0 → 2026.6.18-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/js/Subworkflow.d.ts +49 -12
- package/dist/js/Subworkflow.js +20 -12
- package/dist/js/Workflow.d.ts +2 -1
- package/dist/js/context/mixins/MaterialContextMixin.d.ts +1 -1
- package/dist/js/enums.d.ts +3 -1
- package/dist/js/enums.js +2 -0
- package/dist/js/generated/ErrorUnitSchemaMixin.d.ts +5 -0
- package/dist/js/generated/ErrorUnitSchemaMixin.js +21 -0
- package/dist/js/index.d.ts +3 -2
- package/dist/js/index.js +4 -1
- package/dist/js/repair/createErrorUnitData.d.ts +3 -0
- package/dist/js/repair/createErrorUnitData.js +25 -0
- package/dist/js/repair/formatRepairReason.d.ts +1 -0
- package/dist/js/repair/formatRepairReason.js +22 -0
- package/dist/js/repair/types.d.ts +16 -0
- package/dist/js/repair/types.js +2 -0
- package/dist/js/units/ErrorUnit.d.ts +16 -0
- package/dist/js/units/ErrorUnit.js +31 -0
- package/dist/js/units/factory.d.ts +13 -8
- package/dist/js/units/factory.js +5 -0
- package/dist/js/units/index.d.ts +2 -1
- package/dist/js/units/index.js +3 -1
- package/dist/js/utils/index.d.ts +1 -0
- package/dist/js/utils/index.js +2 -1
- package/dist/js/utils/repair.d.ts +2 -0
- package/dist/js/utils/repair.js +140 -0
- package/dist/js/workflows/default.d.ts +1 -1
- package/dist/js/workflows/types.d.ts +4 -0
- package/dist/js/workflows/types.js +2 -0
- package/package.json +5 -3
- package/src/js/Subworkflow.ts +23 -12
- package/src/js/Workflow.ts +2 -5
- package/src/js/context/mixins/MaterialContextMixin.ts +1 -1
- package/src/js/enums.ts +2 -0
- package/src/js/generated/ErrorUnitSchemaMixin.ts +28 -0
- package/src/js/index.ts +3 -0
- package/src/js/units/ErrorUnit.ts +42 -0
- package/src/js/units/factory.ts +35 -11
- package/src/js/units/index.ts +2 -0
- package/src/js/utils/index.ts +1 -0
- package/src/js/utils/repair.ts +171 -0
- package/src/js/workflows/default.ts +1 -1
- package/src/js/workflows/types.ts +5 -0
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { EntityError } from "@mat3ra/code/dist/js/entity/in_memory";
|
|
2
|
+
import type {
|
|
3
|
+
ConditionUnitSchema,
|
|
4
|
+
ErrorUnitSchema,
|
|
5
|
+
ExecutionUnitSchema,
|
|
6
|
+
SubworkflowSchema,
|
|
7
|
+
WorkflowBaseUnitSchema,
|
|
8
|
+
} from "@mat3ra/esse/dist/js/types";
|
|
9
|
+
import { Utils } from "@mat3ra/utils";
|
|
10
|
+
|
|
11
|
+
import { UnitStatus, UnitType } from "../enums";
|
|
12
|
+
import Subworkflow from "../Subworkflow";
|
|
13
|
+
import ConditionUnit from "../units/ConditionUnit";
|
|
14
|
+
import ExecutionUnit from "../units/ExecutionUnit";
|
|
15
|
+
import type { AnyWorkflowUnitSchema } from "../units/factory";
|
|
16
|
+
import type { WorkflowSchema } from "../workflows/types";
|
|
17
|
+
|
|
18
|
+
function toErrorUnitSchema(
|
|
19
|
+
unitData: Partial<WorkflowBaseUnitSchema>,
|
|
20
|
+
error: unknown,
|
|
21
|
+
): ErrorUnitSchema {
|
|
22
|
+
let reasonPayload: { error: unknown; json: object };
|
|
23
|
+
|
|
24
|
+
if (error instanceof EntityError && error.details) {
|
|
25
|
+
reasonPayload = {
|
|
26
|
+
error: error.details.error,
|
|
27
|
+
json: unitData as object,
|
|
28
|
+
};
|
|
29
|
+
} else if (error instanceof Error) {
|
|
30
|
+
reasonPayload = {
|
|
31
|
+
error: { message: error.message, name: error.name },
|
|
32
|
+
json: unitData as object,
|
|
33
|
+
};
|
|
34
|
+
} else {
|
|
35
|
+
reasonPayload = {
|
|
36
|
+
error,
|
|
37
|
+
json: unitData as object,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
results: [],
|
|
43
|
+
preProcessors: [],
|
|
44
|
+
postProcessors: [],
|
|
45
|
+
monitors: [],
|
|
46
|
+
name: unitData.name ?? UnitType.error,
|
|
47
|
+
type: UnitType.error,
|
|
48
|
+
status: UnitStatus.error,
|
|
49
|
+
flowchartId: unitData.flowchartId ?? Utils.uuid.getUUID(),
|
|
50
|
+
reason: JSON.stringify(reasonPayload),
|
|
51
|
+
next: unitData.next ?? "",
|
|
52
|
+
head: unitData.head ?? false,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function repairExecutionUnit(
|
|
57
|
+
unitData: Partial<ExecutionUnitSchema>,
|
|
58
|
+
): ExecutionUnitSchema | ErrorUnitSchema {
|
|
59
|
+
try {
|
|
60
|
+
return new ExecutionUnit(unitData as ExecutionUnitSchema).toJSON();
|
|
61
|
+
} catch (error: unknown) {
|
|
62
|
+
return toErrorUnitSchema(unitData as Partial<WorkflowBaseUnitSchema>, error);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function repairConditionUnit(
|
|
67
|
+
unitData: Partial<ConditionUnitSchema>,
|
|
68
|
+
): ConditionUnitSchema | ErrorUnitSchema {
|
|
69
|
+
try {
|
|
70
|
+
return new ConditionUnit(unitData as ConditionUnitSchema).toJSON();
|
|
71
|
+
} catch (error: unknown) {
|
|
72
|
+
return toErrorUnitSchema(unitData as Partial<WorkflowBaseUnitSchema>, error);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function getSubworkflowValidationError(subworkflow: SubworkflowSchema): EntityError | Error | null {
|
|
77
|
+
// Two checks, same order as the old isValid() path (construct + validate), but split so we
|
|
78
|
+
// keep AJV errors when hydration would fail first:
|
|
79
|
+
// 1. validateData — JSON Schema on raw persisted subworkflow (no Application/ModelFactory/units).
|
|
80
|
+
// Surfaces structured AJV errors (e.g. missing model) before the constructor runs.
|
|
81
|
+
// 2. new Subworkflow().validate() — hydration (app, model, units) then schema on the instance.
|
|
82
|
+
// Catches schema-valid JSON that still cannot be built (unknown model, bad units, etc.).
|
|
83
|
+
try {
|
|
84
|
+
Subworkflow.validateData({ ...subworkflow });
|
|
85
|
+
new Subworkflow(subworkflow).validate();
|
|
86
|
+
return null;
|
|
87
|
+
} catch (error: unknown) {
|
|
88
|
+
if (error instanceof EntityError || error instanceof Error) {
|
|
89
|
+
return error;
|
|
90
|
+
}
|
|
91
|
+
return new Error(String(error));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function repairSubworkflow(subworkflowData: SubworkflowSchema): SubworkflowSchema {
|
|
96
|
+
const units = subworkflowData.units.map((unit) => {
|
|
97
|
+
if (unit.type === UnitType.execution) {
|
|
98
|
+
return repairExecutionUnit(unit);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (unit.type === UnitType.condition) {
|
|
102
|
+
return repairConditionUnit(unit);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return unit;
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
return { ...subworkflowData, units };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function repairWorkflow<T extends WorkflowSchema>(workflowData: T): T {
|
|
112
|
+
const subworkflows = workflowData.subworkflows.map((subworkflow) => {
|
|
113
|
+
return repairSubworkflow(subworkflow);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
const invalidSubworkflows = subworkflows
|
|
117
|
+
.map((subworkflow) => {
|
|
118
|
+
const error = getSubworkflowValidationError(subworkflow);
|
|
119
|
+
return error ? { subworkflow, error } : null;
|
|
120
|
+
})
|
|
121
|
+
.filter(
|
|
122
|
+
(entry): entry is { subworkflow: SubworkflowSchema; error: EntityError | Error } =>
|
|
123
|
+
entry !== null,
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
const units = workflowData.units.map((unit): AnyWorkflowUnitSchema => {
|
|
127
|
+
const invalidEntry = invalidSubworkflows.find(
|
|
128
|
+
({ subworkflow }) => subworkflow._id === unit._id,
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
if (invalidEntry) {
|
|
132
|
+
const { subworkflow, error } = invalidEntry;
|
|
133
|
+
const errorUnit = toErrorUnitSchema(unit, error);
|
|
134
|
+
const reasonPayload = {
|
|
135
|
+
...JSON.parse(errorUnit.reason),
|
|
136
|
+
json: { unit, subworkflow },
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
return {
|
|
140
|
+
...errorUnit,
|
|
141
|
+
_id: unit._id,
|
|
142
|
+
name: unit.name || errorUnit.name,
|
|
143
|
+
flowchartId: unit.flowchartId,
|
|
144
|
+
reason: JSON.stringify(reasonPayload),
|
|
145
|
+
preProcessors: unit.preProcessors || [],
|
|
146
|
+
postProcessors: unit.postProcessors || [],
|
|
147
|
+
monitors: unit.monitors || [],
|
|
148
|
+
results: unit.results || [],
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return unit;
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const validSubworkflows = subworkflows.filter((subworkflow) => {
|
|
156
|
+
return !invalidSubworkflows.some(
|
|
157
|
+
({ subworkflow: invalid }) => invalid._id === subworkflow._id,
|
|
158
|
+
);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
const workflows = workflowData.workflows.map((nested) => {
|
|
162
|
+
return repairWorkflow(nested as WorkflowSchema);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
...workflowData,
|
|
167
|
+
subworkflows: validSubworkflows,
|
|
168
|
+
workflows,
|
|
169
|
+
units,
|
|
170
|
+
};
|
|
171
|
+
}
|