import { Static, Type } from '@sinclair/typebox' import { ExecutionToolStatus } from '../agents' import { AppConnectionValue } from '../app-connection/app-connection' import { ExecutionState, ExecutionType, ResumePayload } from '../flow-run/execution/execution-output' import { FlowRunId, RunEnvironment } from '../flow-run/flow-run' import { FlowVersion } from '../flows/flow-version' import { PiecePackage } from '../pieces' import { PlatformId } from '../platform' import { ProjectId } from '../project/project' import { ScheduleOptions } from '../trigger' export enum EngineOperationType { EXTRACT_PIECE_METADATA = 'EXTRACT_PIECE_METADATA', EXECUTE_FLOW = 'EXECUTE_FLOW', EXECUTE_PROPERTY = 'EXECUTE_PROPERTY', EXECUTE_TRIGGER_HOOK = 'EXECUTE_TRIGGER_HOOK', EXECUTE_VALIDATE_AUTH = 'EXECUTE_VALIDATE_AUTH', } export enum TriggerHookType { ON_ENABLE = 'ON_ENABLE', ON_DISABLE = 'ON_DISABLE', HANDSHAKE = 'HANDSHAKE', RENEW = 'RENEW', RUN = 'RUN', TEST = 'TEST', } export type EngineOperation = | ExecuteToolOperation | ExecuteFlowOperation | ExecutePropsOptions | ExecuteTriggerOperation | ExecuteExtractPieceMetadataOperation | ExecuteValidateAuthOperation export const enum EngineSocketEvent { ENGINE_RESPONSE = 'engine-response', ENGINE_STDOUT = 'engine-stdout', ENGINE_STDERR = 'engine-stderr', ENGINE_READY = 'engine-ready', ENGINE_OPERATION = 'engine-operation', UPDATE_RUN_PROGRESS = 'update-run-progress', SEND_FLOW_RESPONSE = 'send-flow-response', UPDATE_STEP_PROGRESS = 'update-step-progress', } export const EngineStdout = Type.Object({ message: Type.String(), }) export const EngineStderr = Type.Object({ message: Type.String(), }) export type EngineStdout = Static export type EngineStderr = Static export type BaseEngineOperation = { projectId: ProjectId engineToken: string internalApiUrl: string publicApiUrl: string timeoutInSeconds: number platformId: PlatformId } export type ExecuteValidateAuthOperation = Omit & { piece: PiecePackage auth: AppConnectionValue } export type ExecuteExtractPieceMetadata = PiecePackage & { platformId: PlatformId } export type ExecuteExtractPieceMetadataOperation = ExecuteExtractPieceMetadata & { timeoutInSeconds: number, platformId: PlatformId } export type ExecuteToolOperation = BaseEngineOperation & { actionName: string pieceName: string pieceVersion: string predefinedInput: Record instruction: string } export type ExecutePropsOptions = BaseEngineOperation & { piece: PiecePackage propertyName: string actionOrTriggerName: string flowVersion?: FlowVersion input: Record sampleData: Record searchValue?: string } type BaseExecuteFlowOperation = BaseEngineOperation & { flowVersion: FlowVersion flowRunId: FlowRunId executionType: T runEnvironment: RunEnvironment executionState: ExecutionState serverHandlerId: string | null httpRequestId: string | null progressUpdateType: ProgressUpdateType stepNameToTest: string | null sampleData?: Record logsUploadUrl?: string logsFileId?: string } export enum ProgressUpdateType { WEBHOOK_RESPONSE = 'WEBHOOK_RESPONSE', TEST_FLOW = 'TEST_FLOW', NONE = 'NONE', } export type BeginExecuteFlowOperation = BaseExecuteFlowOperation & { triggerPayload: unknown executeTrigger: boolean } export type ResumeExecuteFlowOperation = BaseExecuteFlowOperation & { resumePayload: ResumePayload } export type ExecuteFlowOperation = BeginExecuteFlowOperation | ResumeExecuteFlowOperation export type ExecuteTriggerOperation = BaseEngineOperation & { hookType: HT test: boolean flowVersion: FlowVersion webhookUrl: string triggerPayload?: TriggerPayload appWebhookUrl?: string webhookSecret?: string | Record } export const TriggerPayload = Type.Object({ body: Type.Unknown(), rawBody: Type.Optional(Type.Unknown()), headers: Type.Record(Type.String(), Type.String()), queryParams: Type.Record(Type.String(), Type.String()), }) export type TriggerPayload = { body: T rawBody?: unknown headers: Record queryParams: Record } export type EventPayload = { body: B rawBody?: unknown method: string headers: Record queryParams: Record } export type ParseEventResponse = { event?: string identifierValue?: string reply?: { headers: Record body: unknown } } export type AppEventListener = { events: string[] identifierValue: string } type ExecuteTestOrRunTriggerResponse = { success: boolean message?: string output: unknown[] } type ExecuteHandshakeTriggerResponse = { success: boolean message?: string response?: { status: number body?: unknown headers?: Record } } type ExecuteOnEnableTriggerResponse = { listeners: AppEventListener[] scheduleOptions?: ScheduleOptions } export const EngineHttpResponse = Type.Object({ status: Type.Number(), body: Type.Unknown(), headers: Type.Record(Type.String(), Type.String()), }) export type EngineHttpResponse = Static export type ExecuteTriggerResponse = H extends TriggerHookType.RUN ? ExecuteTestOrRunTriggerResponse : H extends TriggerHookType.HANDSHAKE ? ExecuteHandshakeTriggerResponse : H extends TriggerHookType.TEST ? ExecuteTestOrRunTriggerResponse : H extends TriggerHookType.RENEW ? Record : H extends TriggerHookType.ON_DISABLE ? Record : ExecuteOnEnableTriggerResponse export type ExecuteToolResponse = { status: ExecutionToolStatus output?: unknown resolvedInput: Record errorMessage?: unknown } export type ExecuteActionResponse = { success: boolean input: unknown output: unknown message?: string } type BaseExecuteValidateAuthResponseOutput = { valid: Valid } type ValidExecuteValidateAuthResponseOutput = BaseExecuteValidateAuthResponseOutput type InvalidExecuteValidateAuthResponseOutput = BaseExecuteValidateAuthResponseOutput & { error: string } export type ExecuteValidateAuthResponse = | ValidExecuteValidateAuthResponseOutput | InvalidExecuteValidateAuthResponseOutput export type EngineResponse = { status: EngineResponseStatus response: T delayInSeconds?: number error?: string } export enum EngineResponseStatus { OK = 'OK', INTERNAL_ERROR = 'INTERNAL_ERROR', TIMEOUT = 'TIMEOUT', MEMORY_ISSUE = 'MEMORY_ISSUE', }