@protobase/workflows 0.100.23 → 0.100.25

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.
@@ -8,34 +8,68 @@ export type ConditionOperand = {
8
8
  } | {
9
9
  value: string | number | boolean;
10
10
  };
11
+ /** A regular expression a Matches node tests text against: its source and its flags, `i` and `m`, as JSONata writes `/source/flags`. */
12
+ export type ConditionPattern = {
13
+ pattern: string;
14
+ flags: string;
15
+ };
11
16
  /**
12
- * A Condition taken apart into the editor's nodes: a Comparison of two values, an And or an Or of two conditions, or a
13
- * Not of one, where a condition is another of these or a value of its own or read by name.
17
+ * A Condition taken apart into the editor's nodes: a Comparison of two values, a Matches of text against a pattern
18
+ * (`$contains`), an All of or an Any of two conditions or more (`and`, `or`), or a Not of one, where a condition is
19
+ * another of these or a value of its own or read by name.
14
20
  */
15
21
  export type ConditionPart = {
16
22
  kind: 'compare';
17
23
  op: CompareOp;
18
24
  sides: [ConditionOperand, ConditionOperand];
25
+ } | {
26
+ kind: 'matches';
27
+ sides: [ConditionOperand, ConditionPattern];
19
28
  } | {
20
29
  kind: 'logic';
21
30
  op: 'and' | 'or';
22
- sides: [ConditionSide, ConditionSide];
31
+ sides: ConditionSide[];
23
32
  } | {
24
33
  kind: 'not';
25
34
  sides: [ConditionSide];
26
35
  };
27
36
  /** What a condition node's input takes: a node of its own, or a value. */
28
37
  export type ConditionSide = ConditionPart | ConditionOperand;
38
+ /** What any input of a condition node holds: a side, or a Matches node's pattern. */
39
+ export type ConditionInput = ConditionSide | ConditionPattern;
29
40
  /** What a step's status can be, as `$states.context.Steps.<name>.Status` reads it. */
30
41
  export declare const stepStatuses: readonly ["SUCCEEDED", "FAILED", "TIMED_OUT", "ABORTED", "SKIPPED"];
31
42
  /** Whether a side is a node of its own rather than a value. */
32
- export declare const isConditionPart: (side: ConditionSide) => side is ConditionPart;
43
+ export declare const isConditionPart: (side: ConditionInput) => side is ConditionPart;
33
44
  /** The Condition a node and what it takes write: `{% $states.context.Steps.Save.Status = 'SUCCEEDED' %}`. */
34
45
  export declare const conditionValue: (part: ConditionPart) => string;
35
46
  /**
36
- * A Condition taken apart into nodes, when it is a Comparison, an And, an Or or a Not of values and reads by name and
47
+ * A Condition taken apart into nodes, when it is a Comparison, a Matches, an All of, an Any of or a Not of values and
48
+ * reads by name and
37
49
  * writing those nodes back gives it exactly as it is; undefined for any other, such as one that computes, or is spaced
38
50
  * or bracketed otherwise, which stays an expression, so no Condition is ever rewritten.
39
51
  */
40
52
  export declare const conditionParts: (value: unknown) => ConditionPart | undefined;
53
+ /** A value a Select gives or compares with: text, a number, or true or false. */
54
+ export type SelectValue = string | number | boolean;
55
+ /**
56
+ * A value chosen by another, which a Select node draws: the value it looks at, read by name or one of its own, a case
57
+ * per value it can be with what it gives then, and what it gives for any other. JSONata writes it as a chain of
58
+ * conditionals: `$topic = 'sales' ? 'sales@example.com' : $topic = 'support' ? 'help@example.com' : 'info@example.com'`.
59
+ */
60
+ export type SelectPart = {
61
+ operand: ConditionOperand;
62
+ cases: {
63
+ when: SelectValue;
64
+ then: SelectValue;
65
+ }[];
66
+ otherwise: SelectValue;
67
+ };
68
+ /** The value a Select node and what it holds write: `{% $topic = 'sales' ? 'sales@example.com' : 'info@example.com' %}`. */
69
+ export declare const selectValue: ({ operand, cases, otherwise }: SelectPart) => string;
70
+ /**
71
+ * A value taken apart into a Select node, when it is a chain of conditionals that compare one value with values of
72
+ * their own and give values of their own, and writing the node back gives it exactly as it is; undefined for any other.
73
+ */
74
+ export declare const selectParts: (value: unknown) => SelectPart | undefined;
41
75
  //#endregion
@@ -45,14 +45,27 @@ const operandOf = (node) => {
45
45
  const read = referenceOf(node);
46
46
  return read && { read };
47
47
  };
48
+ const chainOf = (node, op) => node.type === "binary" && node.value === op ? [...chainOf(node.lhs, op), node.rhs] : [node];
49
+ const isRegex = (value) => value instanceof RegExp;
48
50
  const partOf = (node) => {
49
51
  if (node.type === "block" && node.expressions?.length === 1) return partOf(node.expressions[0]);
50
52
  if (node.type === "binary" && (node.value === "and" || node.value === "or")) {
51
- const [lhs, rhs] = [sideOf(node.lhs), sideOf(node.rhs)];
52
- return lhs && rhs && {
53
+ const sides = chainOf(node, node.value).map(sideOf);
54
+ return sides.every((side) => side !== void 0) ? {
53
55
  kind: "logic",
54
56
  op: node.value,
55
- sides: [lhs, rhs]
57
+ sides
58
+ } : void 0;
59
+ }
60
+ const [text, pattern] = node.arguments ?? [];
61
+ if (node.type === "function" && node.procedure?.type === "variable" && node.procedure.value === "contains" && node.arguments?.length === 2 && pattern?.type === "regex" && isRegex(pattern.value)) {
62
+ const operand = operandOf(text);
63
+ return operand && {
64
+ kind: "matches",
65
+ sides: [operand, {
66
+ pattern: pattern.value.source,
67
+ flags: pattern.value.flags.replace("g", "")
68
+ }]
56
69
  };
57
70
  }
58
71
  if (node.type === "binary" && compareOps.includes(node.value)) {
@@ -82,22 +95,24 @@ const operandText = (operand) => {
82
95
  if ("read" in operand) return readText(operand.read);
83
96
  return typeof operand.value === "string" ? quoted(operand.value) : String(operand.value);
84
97
  };
85
- const sideText = (side, parent, right) => {
98
+ const sideText = (side, parent) => {
86
99
  if (!isConditionPart(side)) return operandText(side);
87
100
  const text = partText(side);
88
101
  if (side.kind !== "logic") return text;
89
- return parent === "and" && side.op === "or" || right && side.op === parent ? `(${text})` : text;
102
+ return side.op === parent || parent === "and" && side.op === "or" ? `(${text})` : text;
90
103
  };
91
104
  const partText = (part) => {
92
105
  if (part.kind === "compare") return `${operandText(part.sides[0])} ${part.op} ${operandText(part.sides[1])}`;
93
- if (part.kind === "logic") return `${sideText(part.sides[0], part.op, false)} ${part.op} ${sideText(part.sides[1], part.op, true)}`;
106
+ if (part.kind === "matches") return `$contains(${operandText(part.sides[0])}, /${part.sides[1].pattern}/${part.sides[1].flags})`;
107
+ if (part.kind === "logic") return part.sides.map((side) => sideText(side, part.op)).join(` ${part.op} `);
94
108
  const [side] = part.sides;
95
109
  return `$not(${isConditionPart(side) ? partText(side) : operandText(side)})`;
96
110
  };
97
111
  /** The Condition a node and what it takes write: `{% $states.context.Steps.Save.Status = 'SUCCEEDED' %}`. */
98
112
  const conditionValue = (part) => `{% ${partText(part)} %}`;
99
113
  /**
100
- * A Condition taken apart into nodes, when it is a Comparison, an And, an Or or a Not of values and reads by name and
114
+ * A Condition taken apart into nodes, when it is a Comparison, a Matches, an All of, an Any of or a Not of values and
115
+ * reads by name and
101
116
  * writing those nodes back gives it exactly as it is; undefined for any other, such as one that computes, or is spaced
102
117
  * or bracketed otherwise, which stays an expression, so no Condition is ever rewritten.
103
118
  */
@@ -109,5 +124,53 @@ const conditionParts = (value) => {
109
124
  const part = partOf(parsed.expression.ast());
110
125
  return part && conditionValue(part) === value ? part : void 0;
111
126
  };
127
+ const valueOf = (node) => {
128
+ if (!node) return;
129
+ const operand = operandOf(node);
130
+ return operand && "value" in operand ? operand.value : void 0;
131
+ };
132
+ const selectOf = (node) => {
133
+ const cases = [];
134
+ let operand;
135
+ let at = node;
136
+ while (at?.type === "condition") {
137
+ const test = at.condition;
138
+ if (test?.type !== "binary" || test.value !== "=") return;
139
+ const read = operandOf(test.lhs);
140
+ const [when, then] = [valueOf(test.rhs), valueOf(at.then)];
141
+ if (!read || when === void 0 || then === void 0) return;
142
+ if (operand && operandText(operand) !== operandText(read)) return;
143
+ operand = read;
144
+ cases.push({
145
+ when,
146
+ then
147
+ });
148
+ at = at.else;
149
+ }
150
+ const otherwise = valueOf(at);
151
+ return operand && otherwise !== void 0 ? {
152
+ operand,
153
+ cases,
154
+ otherwise
155
+ } : void 0;
156
+ };
157
+ /** The value a Select node and what it holds write: `{% $topic = 'sales' ? 'sales@example.com' : 'info@example.com' %}`. */
158
+ const selectValue = ({ operand, cases, otherwise }) => {
159
+ const read = operandText(operand);
160
+ const text = (value) => operandText({ value });
161
+ return `{% ${[...cases.map(({ when, then }) => `${read} = ${text(when)} ? ${text(then)}`), text(otherwise)].join(" : ")} %}`;
162
+ };
163
+ /**
164
+ * A value taken apart into a Select node, when it is a chain of conditionals that compare one value with values of
165
+ * their own and give values of their own, and writing the node back gives it exactly as it is; undefined for any other.
166
+ */
167
+ const selectParts = (value) => {
168
+ const source = expressionSource(value);
169
+ if (source === void 0) return;
170
+ const parsed = parse(source);
171
+ if ("error" in parsed) return;
172
+ const part = selectOf(parsed.expression.ast());
173
+ return part && selectValue(part) === value ? part : void 0;
174
+ };
112
175
  //#endregion
113
- export { conditionParts, conditionValue, isConditionPart, stepStatuses };
176
+ export { conditionParts, conditionValue, isConditionPart, selectParts, selectValue, stepStatuses };
package/dist/index.d.ts CHANGED
@@ -9,7 +9,7 @@ import { dataflowDefinitionSchema, definitionSchema } from "./definition/schema.
9
9
  import { DefinitionParts, definitionFromParts, partOfProblem, partsOfDefinition } from "./definition/parts.js";
10
10
  import { expressionSource } from "./definition/expressions.js";
11
11
  import { PinKind, PinType, fits, pinType, pinTypeText } from "./definition/pins/pin-type.js";
12
- import { CompareOp, ConditionOperand, ConditionPart, ConditionSide, conditionParts, conditionValue, isConditionPart, stepStatuses } from "./definition/pins/conditions.js";
12
+ import { CompareOp, ConditionInput, ConditionOperand, ConditionPart, ConditionPattern, ConditionSide, SelectPart, SelectValue, conditionParts, conditionValue, isConditionPart, selectParts, selectValue, stepStatuses } from "./definition/pins/conditions.js";
13
13
  import { definitionWarnings } from "./definition/check-warnings.js";
14
14
  import { allStates, blockDefinition, findBlock, innerMachine } from "./definition/inner.js";
15
15
  import { resources, usedConnections } from "./definition/used-connections.js";
@@ -42,4 +42,4 @@ import { CompletedVersion, JobCompletion, jobAnswer, jobErrors } from "./runners
42
42
  import { BuildReport, JobBuild, JobFile, RunnerJob, RunnerOrder, buildKind, buildStatuses, fileInputName, fileProperties } from "./runners/job-contract.js";
43
43
  import { customPlugin, customStepOf } from "./plugin/custom-step-id.js";
44
44
  import { CustomStepBuildStatus, CustomStepManifest, CustomStepVersion, ManifestProblem, customStepEntry, customStepTestDefinition, customStepTestState, latestCustomSteps, manifestProblems, withCustomSteps } from "./plugin/custom-steps.js";
45
- export { type AnswerRefusal, type BlockCache, type BlockRun, type BlockRunStore, type BlockShare, type BlockState, type BuildReport, type CacheCall, type CacheEntry, type CacheIdentity, type CatalogEntry, type Catcher, type CheckOptions, type Checkpoint, type ChoiceRule, type ChoiceState, type CompareOp, type CompletedVersion, type ConditionOperand, type ConditionPart, type ConditionSide, type ConnectionHeader, type ConnectionHeaders, type ConnectionSupport, type CustomStepBuildStatus, type CustomStepJob, type CustomStepJobs, type CustomStepManifest, type CustomStepVersion, type Definition, type DefinitionParts, type Dependencies, type EarlierCheckpoint, type ErrorOutput, type JobBuild, type JobCompletion, type JobFile, type KeptAnswer, type KeyShape, type KnownRunner, type LoadedPlugin, type LoadedRun, type Machine, type ManifestProblem, type MapState, type MatchSettings, type Order, type PinKind, type PinSchemas, type PinType, type Placement, type PollingRunner, type Problem, type QueuedJob, type ReadBase, type RecordSchemas, type RecordUse, type Redactor, type Reference, type ReplayedStep, type RerunFrom, type Retrier, type RunChange, type RunStatus, type RunStore, type Runner, type RunnerJob, type RunnerOrder, type RunnerPort, type RunnerState, type SegmentInput, type SegmentQueue, type State, type StateMachine, type StepCacheStore, StepError, type StepLog, type StepRecord, type StepStatus, type StepWiring, type TaskAnswer, type TaskContext, type TaskHandler, type TaskHandlers, type TaskState, type TaskTokenStore, type Tier, type TraceMode, type TraceSink, type Trigger, type TriggerCheck, type TypeCatalog, type TypeEntry, type Unavailable, type WaitingTask, allStates, assignedNames, blockDefinition, buildKind, buildStatuses, checkDefinition, conditionParts, conditionValue, connectionUnavailable, createRunner, customPlugin, customStepEntry, customStepOf, customStepTestDefinition, customStepTestState, dataflowDefinitionSchema, definitionFromParts, definitionSchema, definitionWarnings, dependsOn, expressionReferences, expressionSource, fileInputName, fileProperties, findBlock, fits, heldBackLog, importStateMachine, innerMachine, inputSchema, isConditionPart, jobAnswer, jobErrors, jobKind, keptAssigned, latestCustomSteps, limits, loadPlugins, machineDependencies, manifestProblems, matchJobs, nestingDepth, orderOf, parseReference, partOfProblem, partsOfDefinition, pinType, pinTypeText, problemText, readText, redacted, referenceSchema, referenceValue, rerunCheckpoint, rerunFrom, rerunProblem, resolveSchema, resources, resultRead, resultSchema, resultState, runSegment, schemaAt, secretRedactor, shortestSecret, stepHandlers, stepStatusSchema, stepStatuses, taskResource, taskTokenHash, tierOf, tokenPattern, triggerChecks, triggerSchema, typeCatalog, typeList, usedConnections, waitSuffix, waitsForToken, withCustomSteps };
45
+ export { type AnswerRefusal, type BlockCache, type BlockRun, type BlockRunStore, type BlockShare, type BlockState, type BuildReport, type CacheCall, type CacheEntry, type CacheIdentity, type CatalogEntry, type Catcher, type CheckOptions, type Checkpoint, type ChoiceRule, type ChoiceState, type CompareOp, type CompletedVersion, type ConditionInput, type ConditionOperand, type ConditionPart, type ConditionPattern, type ConditionSide, type ConnectionHeader, type ConnectionHeaders, type ConnectionSupport, type CustomStepBuildStatus, type CustomStepJob, type CustomStepJobs, type CustomStepManifest, type CustomStepVersion, type Definition, type DefinitionParts, type Dependencies, type EarlierCheckpoint, type ErrorOutput, type JobBuild, type JobCompletion, type JobFile, type KeptAnswer, type KeyShape, type KnownRunner, type LoadedPlugin, type LoadedRun, type Machine, type ManifestProblem, type MapState, type MatchSettings, type Order, type PinKind, type PinSchemas, type PinType, type Placement, type PollingRunner, type Problem, type QueuedJob, type ReadBase, type RecordSchemas, type RecordUse, type Redactor, type Reference, type ReplayedStep, type RerunFrom, type Retrier, type RunChange, type RunStatus, type RunStore, type Runner, type RunnerJob, type RunnerOrder, type RunnerPort, type RunnerState, type SegmentInput, type SegmentQueue, type SelectPart, type SelectValue, type State, type StateMachine, type StepCacheStore, StepError, type StepLog, type StepRecord, type StepStatus, type StepWiring, type TaskAnswer, type TaskContext, type TaskHandler, type TaskHandlers, type TaskState, type TaskTokenStore, type Tier, type TraceMode, type TraceSink, type Trigger, type TriggerCheck, type TypeCatalog, type TypeEntry, type Unavailable, type WaitingTask, allStates, assignedNames, blockDefinition, buildKind, buildStatuses, checkDefinition, conditionParts, conditionValue, connectionUnavailable, createRunner, customPlugin, customStepEntry, customStepOf, customStepTestDefinition, customStepTestState, dataflowDefinitionSchema, definitionFromParts, definitionSchema, definitionWarnings, dependsOn, expressionReferences, expressionSource, fileInputName, fileProperties, findBlock, fits, heldBackLog, importStateMachine, innerMachine, inputSchema, isConditionPart, jobAnswer, jobErrors, jobKind, keptAssigned, latestCustomSteps, limits, loadPlugins, machineDependencies, manifestProblems, matchJobs, nestingDepth, orderOf, parseReference, partOfProblem, partsOfDefinition, pinType, pinTypeText, problemText, readText, redacted, referenceSchema, referenceValue, rerunCheckpoint, rerunFrom, rerunProblem, resolveSchema, resources, resultRead, resultSchema, resultState, runSegment, schemaAt, secretRedactor, selectParts, selectValue, shortestSecret, stepHandlers, stepStatusSchema, stepStatuses, taskResource, taskTokenHash, tierOf, tokenPattern, triggerChecks, triggerSchema, typeCatalog, typeList, usedConnections, waitSuffix, waitsForToken, withCustomSteps };
package/dist/index.js CHANGED
@@ -16,7 +16,7 @@ import { orderOf } from "./definition/types.js";
16
16
  import { customPlugin, customStepOf } from "./plugin/custom-step-id.js";
17
17
  import { checkDefinition } from "./definition/check-definition.js";
18
18
  import { definitionFromParts, partOfProblem, partsOfDefinition } from "./definition/parts.js";
19
- import { conditionParts, conditionValue, isConditionPart, stepStatuses } from "./definition/pins/conditions.js";
19
+ import { conditionParts, conditionValue, isConditionPart, selectParts, selectValue, stepStatuses } from "./definition/pins/conditions.js";
20
20
  import { definitionWarnings } from "./definition/check-warnings.js";
21
21
  import { importStateMachine } from "./definition/import/import-machine.js";
22
22
  import { StepError } from "./run/errors.js";
@@ -37,4 +37,4 @@ import { connectionUnavailable } from "./plugin/connection-availability.js";
37
37
  import { typeCatalog } from "./plugin/type-catalog.js";
38
38
  import { jobKind, matchJobs, tierOf } from "./runners/match-jobs.js";
39
39
  import { jobAnswer, jobErrors } from "./runners/job-answer.js";
40
- export { StepError, allStates, assignedNames, blockDefinition, buildKind, buildStatuses, checkDefinition, conditionParts, conditionValue, connectionUnavailable, createRunner, customPlugin, customStepEntry, customStepOf, customStepTestDefinition, customStepTestState, dataflowDefinitionSchema, definitionFromParts, definitionSchema, definitionWarnings, dependsOn, expressionReferences, expressionSource, fileInputName, fileProperties, findBlock, fits, heldBackLog, importStateMachine, innerMachine, inputSchema, isConditionPart, jobAnswer, jobErrors, jobKind, keptAssigned, latestCustomSteps, limits, loadPlugins, machineDependencies, manifestProblems, matchJobs, nestingDepth, orderOf, parseReference, partOfProblem, partsOfDefinition, pinType, pinTypeText, problemText, readText, redacted, referenceSchema, referenceValue, rerunCheckpoint, rerunFrom, rerunProblem, resolveSchema, resources, resultRead, resultSchema, resultState, runSegment, schemaAt, secretRedactor, shortestSecret, stepHandlers, stepStatusSchema, stepStatuses, taskResource, taskTokenHash, tierOf, tokenPattern, triggerChecks, triggerSchema, typeCatalog, typeList, usedConnections, waitSuffix, waitsForToken, withCustomSteps };
40
+ export { StepError, allStates, assignedNames, blockDefinition, buildKind, buildStatuses, checkDefinition, conditionParts, conditionValue, connectionUnavailable, createRunner, customPlugin, customStepEntry, customStepOf, customStepTestDefinition, customStepTestState, dataflowDefinitionSchema, definitionFromParts, definitionSchema, definitionWarnings, dependsOn, expressionReferences, expressionSource, fileInputName, fileProperties, findBlock, fits, heldBackLog, importStateMachine, innerMachine, inputSchema, isConditionPart, jobAnswer, jobErrors, jobKind, keptAssigned, latestCustomSteps, limits, loadPlugins, machineDependencies, manifestProblems, matchJobs, nestingDepth, orderOf, parseReference, partOfProblem, partsOfDefinition, pinType, pinTypeText, problemText, readText, redacted, referenceSchema, referenceValue, rerunCheckpoint, rerunFrom, rerunProblem, resolveSchema, resources, resultRead, resultSchema, resultState, runSegment, schemaAt, secretRedactor, selectParts, selectValue, shortestSecret, stepHandlers, stepStatusSchema, stepStatuses, taskResource, taskTokenHash, tierOf, tokenPattern, triggerChecks, triggerSchema, typeCatalog, typeList, usedConnections, waitSuffix, waitsForToken, withCustomSteps };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/workflows",
3
- "version": "0.100.23",
3
+ "version": "0.100.25",
4
4
  "description": "Workflows as state machines with JSONata expressions: their check, a runner that saves only what a run needs to go on and traces every step, and the plugin interface for trigger and step types.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {