| 6 | const logger = createLogger('TriggerBlockHandler') |
| 7 | |
| 8 | export class TriggerBlockHandler implements BlockHandler { |
| 9 | canHandle(block: SerializedBlock): boolean { |
| 10 | return isTriggerBehavior(block) |
| 11 | } |
| 12 | |
| 13 | async execute( |
| 14 | ctx: ExecutionContext, |
| 15 | block: SerializedBlock, |
| 16 | inputs: Record<string, any> |
| 17 | ): Promise<any> { |
| 18 | logger.info(`Executing trigger block: ${block.id} (Type: ${block.metadata?.id})`) |
| 19 | |
| 20 | if (block.metadata?.id === BlockType.STARTER) { |
| 21 | return this.executeStarterBlock(ctx, block, inputs) |
| 22 | } |
| 23 | |
| 24 | const existingState = ctx.blockStates.get(block.id) |
| 25 | if (existingState?.output) { |
| 26 | return existingState.output |
| 27 | } |
| 28 | |
| 29 | const starterBlock = ctx.workflow?.blocks?.find((b) => b.metadata?.id === 'starter') |
| 30 | if (starterBlock) { |
| 31 | const starterState = ctx.blockStates.get(starterBlock.id) |
| 32 | if (starterState?.output && Object.keys(starterState.output).length > 0) { |
| 33 | const starterOutput = starterState.output |
| 34 | |
| 35 | if (starterOutput.webhook?.data) { |
| 36 | const cleanOutput: Record<string, unknown> = {} |
| 37 | for (const [key, value] of Object.entries(starterOutput)) { |
| 38 | if (!isTriggerInternalKey(key)) { |
| 39 | cleanOutput[key] = value |
| 40 | } |
| 41 | } |
| 42 | return cleanOutput |
| 43 | } |
| 44 | |
| 45 | return starterOutput |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | if (inputs && Object.keys(inputs).length > 0) { |
| 50 | return inputs |
| 51 | } |
| 52 | |
| 53 | return {} |
| 54 | } |
| 55 | |
| 56 | private executeStarterBlock( |
| 57 | ctx: ExecutionContext, |
| 58 | block: SerializedBlock, |
| 59 | inputs: Record<string, any> |
| 60 | ): any { |
| 61 | logger.info(`Executing starter block: ${block.id}`, { |
| 62 | blockName: block.metadata?.name, |
| 63 | }) |
| 64 | |
| 65 | const existingState = ctx.blockStates.get(block.id) |
nothing calls this directly
no outgoing calls
no test coverage detected