Skip to content

Commit

Permalink
Added no op event to workflow runner (#2884)
Browse files Browse the repository at this point in the history
* feat(workflow-core): added no op event to workflow runner

* feat(workflow-core): added no op action to workflow runner

* fix(workflow-core): fixed missing export and state variable

* fix(workflow-core): fixed missing export and state variable
  • Loading branch information
Omri-Levy authored Dec 10, 2024
1 parent 803b88f commit 70c74f8
Show file tree
Hide file tree
Showing 23 changed files with 121 additions and 100 deletions.
7 changes: 7 additions & 0 deletions apps/backoffice-v2/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @ballerine/backoffice-v2

## 0.7.80

### Patch Changes

- @ballerine/workflow-browser-sdk@0.6.76
- @ballerine/workflow-node-sdk@0.6.76

## 0.7.79

### Patch Changes
Expand Down
6 changes: 3 additions & 3 deletions apps/backoffice-v2/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ballerine/backoffice-v2",
"version": "0.7.79",
"version": "0.7.80",
"description": "Ballerine - Backoffice",
"homepage": "https://github.com/ballerine-io/ballerine",
"type": "module",
Expand Down Expand Up @@ -55,8 +55,8 @@
"@ballerine/common": "0.9.57",
"@ballerine/react-pdf-toolkit": "^1.2.48",
"@ballerine/ui": "^0.5.48",
"@ballerine/workflow-browser-sdk": "0.6.75",
"@ballerine/workflow-node-sdk": "0.6.75",
"@ballerine/workflow-browser-sdk": "0.6.76",
"@ballerine/workflow-node-sdk": "0.6.76",
"@botpress/webchat": "^2.1.10",
"@botpress/webchat-generator": "^0.2.9",
"@fontsource/inter": "^4.5.15",
Expand Down
6 changes: 6 additions & 0 deletions apps/kyb-app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# kyb-app

## 0.3.92

### Patch Changes

- @ballerine/workflow-browser-sdk@0.6.76

## 0.3.91

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions apps/kyb-app/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ballerine/kyb-app",
"private": true,
"version": "0.3.91",
"version": "0.3.92",
"type": "module",
"scripts": {
"dev": "vite",
Expand All @@ -18,7 +18,7 @@
"@ballerine/blocks": "0.2.28",
"@ballerine/common": "^0.9.57",
"@ballerine/ui": "0.5.49",
"@ballerine/workflow-browser-sdk": "0.6.75",
"@ballerine/workflow-browser-sdk": "0.6.76",
"@lukemorales/query-key-factory": "^1.0.3",
"@radix-ui/react-icons": "^1.3.0",
"@rjsf/core": "^5.9.0",
Expand Down
6 changes: 6 additions & 0 deletions examples/headless-example/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @ballerine/headless-example

## 0.3.75

### Patch Changes

- @ballerine/workflow-browser-sdk@0.6.76

## 0.3.74

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions examples/headless-example/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ballerine/headless-example",
"private": true,
"version": "0.3.74",
"version": "0.3.75",
"type": "module",
"scripts": {
"spellcheck": "cspell \"*\"",
Expand Down Expand Up @@ -35,7 +35,7 @@
},
"dependencies": {
"@ballerine/common": "0.9.57",
"@ballerine/workflow-browser-sdk": "0.6.75",
"@ballerine/workflow-browser-sdk": "0.6.76",
"@felte/reporter-svelte": "^1.1.5",
"@felte/validator-zod": "^1.0.13",
"@fontsource/inter": "^4.5.15",
Expand Down
6 changes: 6 additions & 0 deletions packages/workflow-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @ballerine/workflow-core

## 0.6.76

### Patch Changes

- Added no op event to workflow runner

## 0.6.75

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/workflow-core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ballerine/workflow-core",
"author": "Ballerine <[email protected]>",
"version": "0.6.75",
"version": "0.6.76",
"description": "workflow-core",
"module": "./dist/esm/index.js",
"main": "./dist/cjs/index.js",
Expand Down
1 change: 1 addition & 0 deletions packages/workflow-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ export {
logger,
setLogger,
BUILT_IN_EVENT,
BUILT_IN_ACTION,
ARRAY_MERGE_OPTION,
} from './lib';
5 changes: 5 additions & 0 deletions packages/workflow-core/src/lib/built-in-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const BUILT_IN_ACTION = {
NO_OP: 'NO_OP',
} as const;

export type BuiltInAction = (typeof BUILT_IN_ACTION)[keyof typeof BUILT_IN_ACTION];
2 changes: 2 additions & 0 deletions packages/workflow-core/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ export { HttpError } from './errors';
export { createWorkflow } from './create-workflow';
export { BUILT_IN_EVENT } from './built-in-event';
export type { BuiltInEvent } from './built-in-event';
export { BUILT_IN_ACTION } from './built-in-action';
export { type BuiltInAction } from './built-in-action';
export { ARRAY_MERGE_OPTION } from './utils/deep-merge-with-options';
export type { ArrayMergeOption } from './utils/deep-merge-with-options';
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class BallerineEmailPlugin extends BallerineApiPlugin {
ok: true,
json: () => Promise.resolve({}),
statusText: 'OK',
headers: {} as Headers,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class EmailPlugin extends ApiPlugin {
ok: true,
json: () => Promise.resolve({}),
statusText: 'OK',
headers: {} as Headers,
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { StateMachine } from 'xstate';
import { ruleValidator, TDefintionRules } from './rule-validator';
import { AnyRecord } from '@ballerine/common';
import { AnyRecord, isObject } from '@ballerine/common';
import { BUILT_IN_EVENT } from '../../built-in-event';

type TTransitionEvent = string;

Expand All @@ -9,6 +10,9 @@ type TTransitionOption =
target: string;
cond?: TDefintionRules;
}
| {
actions: string;
}
| string;
type TTransitionOptions = TTransitionOption[];
export const statesValidator = (
Expand Down Expand Up @@ -63,7 +67,22 @@ export const validateTransitionOnEvent = ({
currentState: string;
transition: TTransitionOption;
}) => {
const targetState = typeof transition === 'string' ? transition : transition.target;
const getTargetState = () => {
if (typeof transition === 'string') {
return transition;
}

if (isObject(transition) && 'target' in transition) {
return transition.target;
}

throw Error(`Unexpected transition object: ${JSON.stringify(transition)}`);
}
const targetState = getTargetState();

if (isObject(transition) && 'actions' in transition && transition.actions === BUILT_IN_EVENT.NO_OP) {
return;
}

if (!stateNames.includes(targetState)) {
throw new Error(`Invalid transition from ${currentState} to ${targetState}`);
Expand Down
9 changes: 9 additions & 0 deletions packages/workflow-core/src/lib/workflow-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as jsonLogic from 'json-logic-js';
import type { ActionFunction, MachineOptions, StateMachine } from 'xstate';
import { assign, createMachine, interpret } from 'xstate';
import { pluginsRegistry } from './constants';
import { BUILT_IN_ACTION } from './built-in-action';
import { HttpError } from './errors';
import { BUILT_IN_EVENT } from './index';
import { logger } from './logger';
Expand Down Expand Up @@ -464,9 +465,17 @@ export class WorkflowRunner {
}
}

const state = this.#__currentState;
const noOp = () => {
logger.log(`${BUILT_IN_ACTION.NO_OP} action fired`, {
state,
});
};

const actions: MachineOptions<any, any>['actions'] = {
...workflowActions,
...stateActions,
[BUILT_IN_ACTION.NO_OP]: noOp,
};

const guards: MachineOptions<any, any>['guards'] = {
Expand Down
Loading

0 comments on commit 70c74f8

Please sign in to comment.