Skip to content

Commit

Permalink
feat: added additional context to workflow core and extended browser-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
chesterkmr committed Nov 18, 2024
1 parent d25cf30 commit bfc30d5
Show file tree
Hide file tree
Showing 29 changed files with 2,289 additions and 672 deletions.
8 changes: 8 additions & 0 deletions apps/backoffice-v2/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @ballerine/backoffice-v2

## 0.7.69

### Patch Changes

- Updated dependencies
- @ballerine/workflow-browser-sdk@0.6.65
- @ballerine/workflow-node-sdk@0.6.65

## 0.7.68

### 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.68",
"version": "0.7.69",
"description": "Ballerine - Backoffice",
"homepage": "https://github.com/ballerine-io/ballerine",
"type": "module",
Expand Down Expand Up @@ -55,8 +55,8 @@
"@ballerine/common": "0.9.51",
"@ballerine/react-pdf-toolkit": "^1.2.44",
"@ballerine/ui": "^0.5.44",
"@ballerine/workflow-browser-sdk": "0.6.64",
"@ballerine/workflow-node-sdk": "0.6.64",
"@ballerine/workflow-browser-sdk": "0.6.65",
"@ballerine/workflow-node-sdk": "0.6.65",
"@botpress/webchat": "^2.1.10",
"@botpress/webchat-generator": "^0.2.9",
"@fontsource/inter": "^4.5.15",
Expand Down
7 changes: 7 additions & 0 deletions apps/kyb-app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# kyb-app

## 0.3.80

### Patch Changes

- Updated dependencies
- @ballerine/workflow-browser-sdk@0.6.65

## 0.3.79

### 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.79",
"version": "0.3.80",
"type": "module",
"scripts": {
"dev": "vite",
Expand All @@ -18,7 +18,7 @@
"@ballerine/blocks": "0.2.26",
"@ballerine/ui": "0.5.44",
"@ballerine/common": "^0.9.51",
"@ballerine/workflow-browser-sdk": "0.6.64",
"@ballerine/workflow-browser-sdk": "0.6.65",
"@lukemorales/query-key-factory": "^1.0.3",
"@radix-ui/react-icons": "^1.3.0",
"@rjsf/core": "^5.9.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const StateManager = ({
workflowId,
initialContext,
config,
additionalContext,
}: StateManagerProps) => {
const machine = useMemo(() => {
const initialMachineState = {
Expand All @@ -29,14 +30,15 @@ export const StateManager = ({
definitionType,
extensions,
initialMachineState,
additionalContext,
);

machine.overrideContext(initialMachineState);

return machine;
}, []);
}, [additionalContext]);

const { machineApi } = useMachineLogic(machine);
const { machineApi } = useMachineLogic(machine, additionalContext);
const {
contextPayload,
isPluginLoading,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class PluginRunnerHandler implements ActionHandler {

async run(_: CollectionFlowContext, action: Action<PluginRunnerParams>, api: StateMachineAPI) {
await api.invokePlugin(action.params.pluginName);

return api.getContext();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { CollectionFlowContext } from '@/domains/collection-flow/types/flow-context.types';
import { isErrorWithMessage } from '@ballerine/common';
import { AnyRecord, isErrorWithMessage } from '@ballerine/common';
import { AnyObject } from '@ballerine/ui';
import { WorkflowBrowserSDK } from '@ballerine/workflow-browser-sdk';
import { useCallback, useMemo, useState } from 'react';

export interface StateMachineAPI {
invokePlugin: (pluginName: string) => Promise<void>;
invokePlugin: (pluginName: string, additionalContext?: AnyRecord) => Promise<void>;
sendEvent: (eventName: string) => Promise<void>;
setContext: (newContext: CollectionFlowContext) => CollectionFlowContext;
getContext: () => CollectionFlowContext;
Expand All @@ -14,21 +14,22 @@ export interface StateMachineAPI {

export const useMachineLogic = (
machine: WorkflowBrowserSDK,
additionalContext?: AnyRecord,
): { isInvokingPlugin: boolean; machineApi: StateMachineAPI } => {
const [isInvokingPlugin, setInvokingPlugin] = useState(false);

const invokePlugin = useCallback(
async (pluginName: string) => {
setInvokingPlugin(true);
try {
await machine.invokePlugin(pluginName);
await machine.invokePlugin(pluginName, additionalContext);
} catch (error) {
console.log('Failed to invoke plugin', isErrorWithMessage(error) ? error.message : error);
} finally {
setInvokingPlugin(false);
}
},
[machine],
[machine, additionalContext],
);

const sendEvent = useCallback(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { State } from '@/components/organisms/DynamicUI/StateManager/types';
import { AnyRecord } from '@ballerine/common';
import { AnyObject } from '@ballerine/ui';
import { createWorkflow } from '@ballerine/workflow-browser-sdk';

Expand All @@ -8,6 +9,7 @@ export const createStateMachine = (
definitionType: string,
extensions: AnyObject,
workflowContext?: AnyObject,
additionalContext?: AnyRecord,
) =>
createWorkflow({
runtimeId: workflowId,
Expand All @@ -16,4 +18,5 @@ export const createStateMachine = (
definitionType: 'statechart-json',
extensions: extensions,
workflowContext: workflowContext,
additionalContext: additionalContext,
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CollectionFlowConfig,
CollectionFlowContext,
} from '@/domains/collection-flow/types/flow-context.types';
import { AnyRecord } from '@ballerine/common';
import { AnyChildren, AnyObject } from '@ballerine/ui';
import { MachineConfig } from 'xstate';

Expand All @@ -26,4 +27,5 @@ export interface StateManagerProps {
children: AnyChildren | StateManagerChildCallback;
initialContext: CollectionFlowContext | null;
config?: CollectionFlowConfig;
additionalContext?: AnyRecord;
}
5 changes: 5 additions & 0 deletions apps/kyb-app/src/pages/CollectionFlow/CollectionFlow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
} from '@ballerine/common';
import { AnyObject } from '@ballerine/ui';
import { FailedScreen } from './components/pages/FailedScreen';
import { useAdditionalWorkflowContext } from './hooks/useAdditionalWorkflowContext';

const elems = {
h1: Title,
Expand Down Expand Up @@ -73,6 +74,7 @@ export const CollectionFlow = withSessionProtected(() => {
const { customer } = useCustomer();
const { t } = useTranslation();
const { themeDefinition } = useTheme();
const additionalContext = useAdditionalWorkflowContext();

const elements = schema?.uiSchema?.elements;
const definition = schema?.definition.definition;
Expand Down Expand Up @@ -132,6 +134,8 @@ export const CollectionFlow = withSessionProtected(() => {
if (getCollectionFlowState(initialContext)?.status === CollectionFlowStatusesEnum.failed)
return <FailedScreen />;

console.log('additionalContext', additionalContext);

return definition && collectionFlowData ? (
<DynamicUI initialState={initialUIState}>
<DynamicUI.StateManager
Expand All @@ -141,6 +145,7 @@ export const CollectionFlow = withSessionProtected(() => {
extensions={schema?.definition.extensions}
definition={definition as State}
config={collectionFlowData?.config}
additionalContext={additionalContext}
>
{({ state, stateApi }) => {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useAdditionalWorkflowContext';
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useMemo } from 'react';
import { useSearchParams } from 'react-router-dom';

export const useAdditionalWorkflowContext = () => {
const [searchParams] = useSearchParams();

const context = useMemo(() => {
return {
query: {
token: searchParams.get('token'),
},
};
}, [searchParams]);

return context;
};
7 changes: 7 additions & 0 deletions examples/headless-example/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @ballerine/headless-example

## 0.3.64

### Patch Changes

- Updated dependencies
- @ballerine/workflow-browser-sdk@0.6.65

## 0.3.63

### 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.63",
"version": "0.3.64",
"type": "module",
"scripts": {
"spellcheck": "cspell \"*\"",
Expand Down Expand Up @@ -35,7 +35,7 @@
},
"dependencies": {
"@ballerine/common": "0.9.51",
"@ballerine/workflow-browser-sdk": "0.6.64",
"@ballerine/workflow-browser-sdk": "0.6.65",
"@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.65

### Patch Changes

- Added additionalContext

## 0.6.64

### 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 <dev@ballerine.com>",
"version": "0.6.64",
"version": "0.6.65",
"description": "workflow-core",
"module": "./dist/esm/index.js",
"main": "./dist/cjs/index.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class ApiPlugin {
this.displayName = pluginParams.displayName;
}

async invoke(context: TContext) {
async invoke(context: TContext, additionalContext?: AnyRecord) {
let requestPayload;

try {
Expand All @@ -59,7 +59,10 @@ export class ApiPlugin {
}
}

const _url = await this._getPluginUrl(context);
const _url = await this._getPluginUrl({
...context,
...additionalContext,
});

logger.log('API Plugin - Sending API request', {
url: _url,
Expand All @@ -70,7 +73,10 @@ export class ApiPlugin {
_url,
this.method,
requestPayload,
await this.composeRequestHeaders(this.headers!, context),
await this.composeRequestHeaders(this.headers!, {
...context,
...additionalContext,
}),
);

logger.log('API Plugin - Received response', {
Expand Down
20 changes: 10 additions & 10 deletions packages/workflow-core/src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { RiskRulePlugin } from '@/lib/plugins/common-plugin/risk-rules-plugin';
import type { AnyRecord } from '@ballerine/common';
import type { MachineConfig, MachineOptions } from 'xstate';
import type { CommonPlugins, HttpPlugins, StatePlugins } from './plugins/types';
import type {
IDispatchEventPluginParams,
ISerializableHttpPluginParams,
} from './plugins/external-plugin/types';
import type {
ChildWorkflowPluginParams,
ISerializableChildPluginParams,
Expand All @@ -12,19 +9,22 @@ import type {
ISerializableRiskRulesPlugin,
WorkflowTokenPluginParams,
} from './plugins/common-plugin/types';
import type { DispatchEventPlugin } from './plugins/external-plugin/dispatch-event-plugin';
import type {
IDispatchEventPluginParams,
ISerializableHttpPluginParams,
} from './plugins/external-plugin/types';
import type { CommonPlugins, HttpPlugins, StatePlugins } from './plugins/types';
import type { TContext } from './utils';
import type { THelperFormatingLogic } from './utils/context-transformers/types';
import type { AnyRecord } from '@ballerine/common';
import type { DispatchEventPlugin } from './plugins/external-plugin/dispatch-event-plugin';
import { RiskRulePlugin } from '@/lib/plugins/common-plugin/risk-rules-plugin';

export type ObjectValues<TObject extends Record<any, any>> = TObject[keyof TObject];

export interface Workflow {
subscribe: (eventName: string, callback: (event: WorkflowEvent) => Promise<void>) => void;
sendEvent: (event: WorkflowEventWithoutState) => Promise<void>;
sendEvent: (event: WorkflowEventWithoutState, additionalContext?: AnyRecord) => Promise<void>;
getSnapshot: () => Record<PropertyKey, any>;
invokePlugin: (pluginName: string) => Promise<void>;
invokePlugin: (pluginName: string, additionalContext?: AnyRecord) => Promise<void>;
overrideContext: (context: any) => any;
}

Expand Down
Loading

0 comments on commit bfc30d5

Please sign in to comment.