|
| 1 | +import { getOnDoneAction, getOnErrorAction } from './base' |
| 2 | +import { AnyEventObject, assign, InvokeConfig } from 'xstate' |
| 3 | + |
| 4 | +import { CommonStateMachineStates } from './base' |
| 5 | +import { VerificationRequestMessages, VerificationRequestStateMachineEvents, VerificationRequestStateMachineStates } from '../dto/types' |
| 6 | +import { VerificationRequestStateMachineContext, VerificationRequestStateMachineService } from './verification-request.state-machine.interface' |
| 7 | +import { VerificationRequestService } from '../verification-request.service' |
| 8 | +import { CreateAiTaskDto } from '../../ai-task/dto/create-ai-task.dto' |
| 9 | +import { AiTaskType, CallbackRoute, DEFAULT_EMBEDDING_MODEL } from '../../ai-task/constants/ai-task.constants' |
| 10 | + |
| 11 | + |
| 12 | +const verificationRequestStateMachineSchema = { |
| 13 | + context: {} as VerificationRequestStateMachineContext, |
| 14 | + events: {} as |
| 15 | + | { type: VerificationRequestStateMachineEvents.CREATE } |
| 16 | + | { type: VerificationRequestStateMachineEvents.EMBED } |
| 17 | + | { type: VerificationRequestStateMachineEvents.IDENTIFY_DATA } |
| 18 | + | { type: VerificationRequestStateMachineEvents.DEFINE_TOPICS } |
| 19 | + | { type: VerificationRequestStateMachineEvents.DEFINE_IMPACT_AREA } |
| 20 | + | { type: VerificationRequestStateMachineEvents.DEFINE_SEVERITY } |
| 21 | +} |
| 22 | + |
| 23 | +const getStateInvokeSrc = ( |
| 24 | + eventName: string, |
| 25 | + getVerificationRequestService: () => VerificationRequestService, |
| 26 | +): InvokeConfig<any, any>['src'] => { |
| 27 | + switch (eventName) { |
| 28 | + case VerificationRequestStateMachineEvents.CREATE: |
| 29 | + return async (context: VerificationRequestStateMachineContext, event, meta) => { |
| 30 | + return getVerificationRequestService().create(context.verificationRequest) |
| 31 | + } |
| 32 | + case VerificationRequestStateMachineEvents.EMBED: |
| 33 | + return async (context: VerificationRequestStateMachineContext, event, meta) => { |
| 34 | + const taskDto: CreateAiTaskDto = { |
| 35 | + type: AiTaskType.TEXT_EMBEDDING, |
| 36 | + content: { |
| 37 | + text: context.verificationRequest.content, |
| 38 | + model: DEFAULT_EMBEDDING_MODEL, |
| 39 | + }, |
| 40 | + callbackRoute: CallbackRoute.VERIFICATION_UPDATE_EMBEDDING, |
| 41 | + callbackParams: { |
| 42 | + targetId: context.verificationRequest.id, |
| 43 | + field: "embedding", |
| 44 | + }, |
| 45 | + }; |
| 46 | + await getVerificationRequestService().createAiTask(taskDto) |
| 47 | + return context.result |
| 48 | + } |
| 49 | + default: |
| 50 | + return async (context: VerificationRequestStateMachineContext, event, meta) => { |
| 51 | + return |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +type ConditionalFunc = (context: any, event: any) => boolean |
| 57 | +// TODO: add conditional functions |
| 58 | + |
| 59 | +const getStateInvoke = ( |
| 60 | + eventName: VerificationRequestStateMachineEvents, |
| 61 | + stateMachineService: VerificationRequestStateMachineService, |
| 62 | +): InvokeConfig<any, any> => { |
| 63 | + const getVerificationRequestService = () => stateMachineService.verificationRequestService |
| 64 | + switch (eventName) { |
| 65 | + case VerificationRequestStateMachineEvents.CREATE: |
| 66 | + return { |
| 67 | + id: eventName, |
| 68 | + src: getStateInvokeSrc(eventName, getVerificationRequestService), |
| 69 | + onDone: [ |
| 70 | + { |
| 71 | + target: VerificationRequestStateMachineStates.EMBEDDING, |
| 72 | + actions: assign({ |
| 73 | + result: (context, event: AnyEventObject) => { |
| 74 | + return event.data |
| 75 | + }, |
| 76 | + verificationRequest: (context: any, event: AnyEventObject) => { |
| 77 | + return { id: event.data.id, ...context.verificationRequest } |
| 78 | + }, |
| 79 | + }), |
| 80 | + }, |
| 81 | + ], |
| 82 | + onError: getOnErrorAction(), |
| 83 | + } |
| 84 | + default: |
| 85 | + return { |
| 86 | + id: eventName, |
| 87 | + src: getStateInvokeSrc(eventName, getVerificationRequestService), |
| 88 | + onDone: getOnDoneAction(), |
| 89 | + onError: getOnErrorAction(), |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +const getStateTransitions = (originState: VerificationRequestStateMachineStates | CommonStateMachineStates) => { |
| 95 | + switch (originState) { |
| 96 | + case CommonStateMachineStates.REHYDRATE: |
| 97 | + return { |
| 98 | + [VerificationRequestStateMachineEvents.REQUEST]: { |
| 99 | + target: VerificationRequestStateMachineStates.REQUESTING, |
| 100 | + }, |
| 101 | + [VerificationRequestStateMachineEvents.EMBED]: { |
| 102 | + target: VerificationRequestStateMachineStates.EMBEDDING, |
| 103 | + }, |
| 104 | + } |
| 105 | + case VerificationRequestStateMachineStates.EMBEDDING: |
| 106 | + return { |
| 107 | + [VerificationRequestStateMachineEvents.EMBED]: { |
| 108 | + target: VerificationRequestStateMachineStates.EMBEDDING, |
| 109 | + }, |
| 110 | + } |
| 111 | + case VerificationRequestStateMachineStates.IDENTIFYING_DATA: |
| 112 | + return { |
| 113 | + [VerificationRequestStateMachineEvents.IDENTIFY_DATA]: { |
| 114 | + target: VerificationRequestStateMachineStates.IDENTIFYING_DATA, |
| 115 | + }, |
| 116 | + [VerificationRequestStateMachineEvents.DEFINE_TOPICS]: { |
| 117 | + target: VerificationRequestStateMachineStates.DEFINING_TOPICS, |
| 118 | + }, |
| 119 | + [VerificationRequestStateMachineEvents.DEFINE_IMPACT_AREA]: { |
| 120 | + target: VerificationRequestStateMachineStates.DEFINING_IMPACT_AREA, |
| 121 | + }, |
| 122 | + [VerificationRequestStateMachineEvents.DEFINE_SEVERITY]: { |
| 123 | + target: VerificationRequestStateMachineStates.DEFINING_SEVERITY, |
| 124 | + }, |
| 125 | + } |
| 126 | + default: |
| 127 | + return { |
| 128 | + on: {}, |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | +/** |
| 133 | + * TODO: after creation we need trigger other AI tasks to be executed |
| 134 | + */ |
| 135 | +export const getVerificationRequestStateMachineConfig = (stateMachineService: VerificationRequestStateMachineService) => { |
| 136 | + return { |
| 137 | + id: 'verificationRequest', |
| 138 | + initial: CommonStateMachineStates.REHYDRATE, |
| 139 | + states: { |
| 140 | + [CommonStateMachineStates.REHYDRATE]: { |
| 141 | + description: VerificationRequestMessages.DESCRIPTIONS.REQUESTING, |
| 142 | + on: getStateTransitions(CommonStateMachineStates.REHYDRATE), |
| 143 | + }, |
| 144 | + [VerificationRequestStateMachineStates.IDENTIFYING_DATA]: { |
| 145 | + invoke: getStateInvoke(VerificationRequestStateMachineEvents.IDENTIFY_DATA, stateMachineService), |
| 146 | + description: VerificationRequestMessages.DESCRIPTIONS.DEFAULT, |
| 147 | + }, |
| 148 | + [VerificationRequestStateMachineStates.CREATING]: { |
| 149 | + invoke: getStateInvoke(VerificationRequestStateMachineEvents.CREATE, stateMachineService), |
| 150 | + description: VerificationRequestMessages.DESCRIPTIONS.CREATING, |
| 151 | + }, |
| 152 | + [VerificationRequestStateMachineStates.REQUESTING]: { |
| 153 | + invoke: getStateInvoke(VerificationRequestStateMachineEvents.CREATE, stateMachineService), |
| 154 | + description: VerificationRequestMessages.DESCRIPTIONS.REQUESTING, |
| 155 | + }, |
| 156 | + [VerificationRequestStateMachineStates.EMBEDDING]: { |
| 157 | + invoke: getStateInvoke(VerificationRequestStateMachineEvents.EMBED, stateMachineService), |
| 158 | + description: VerificationRequestMessages.DESCRIPTIONS.EMBED, |
| 159 | + }, |
| 160 | + [CommonStateMachineStates.ERROR]: { |
| 161 | + description: VerificationRequestMessages.DESCRIPTIONS.ERROR, |
| 162 | + type: 'final' as const, |
| 163 | + }, |
| 164 | + [CommonStateMachineStates.WRAP_UP_EXECUTION]: { |
| 165 | + type: 'final' as const, |
| 166 | + }, |
| 167 | + }, |
| 168 | + on: { |
| 169 | + '*': { |
| 170 | + actions: 'logInvalidEvent', |
| 171 | + }, |
| 172 | + }, |
| 173 | + schema: verificationRequestStateMachineSchema, |
| 174 | + context: {}, |
| 175 | + predictableActionArguments: true, |
| 176 | + preserveActionOrder: true, |
| 177 | + } |
| 178 | +} |
0 commit comments