| 46 | * Services are injected as SDK interfaces (not concrete implementations) |
| 47 | */ |
| 48 | export async function executeWorkflow( |
| 49 | definition: WorkflowDefinition, |
| 50 | request: WorkflowRunRequest = {}, |
| 51 | options: ExecuteWorkflowOptions = {}, |
| 52 | ): Promise<WorkflowRunResult> { |
| 53 | const runId = options.runId ?? randomUUID(); |
| 54 | console.log(`🏃 [WORKFLOW RUNNER] executeWorkflow called for runId: ${runId}`); |
| 55 | console.log(`📋 [WORKFLOW RUNNER] Definition has ${definition.actions.length} actions`); |
| 56 | console.log(`📋 [WORKFLOW RUNNER] Entrypoint ref: ${definition.entrypoint.ref}`); |
| 57 | |
| 58 | const results = new Map<string, unknown>(); |
| 59 | const actionsByRef = new Map<string, (typeof definition.actions)[number]>( |
| 60 | definition.actions.map((action) => [action.ref, action]), |
| 61 | ); |
| 62 | |
| 63 | const forwardLog: ((entry: LogEventInput) => void) | undefined = options.logs |
| 64 | ? (entry) => { |
| 65 | const parsed = new Date(entry.timestamp); |
| 66 | const timestamp = Number.isNaN(parsed.getTime()) ? new Date() : parsed; |
| 67 | void options.logs |
| 68 | ?.append({ |
| 69 | runId: entry.runId, |
| 70 | nodeRef: entry.nodeRef, |
| 71 | stream: entry.stream, |
| 72 | level: entry.level, |
| 73 | message: entry.message, |
| 74 | timestamp, |
| 75 | }) |
| 76 | .catch((error) => { |
| 77 | console.error('[Logs] Failed to append log entry', error); |
| 78 | }); |
| 79 | } |
| 80 | : undefined; |
| 81 | |
| 82 | try { |
| 83 | const runAction = async ( |
| 84 | actionRef: string, |
| 85 | schedulerContext: WorkflowSchedulerRunContext, |
| 86 | ): Promise<{ activePorts?: string[] | undefined } | null> => { |
| 87 | console.log( |
| 88 | `🎯 [WORKFLOW RUNNER] runAction called for: ${actionRef} (triggered by: ${schedulerContext.triggeredBy || 'root'})`, |
| 89 | ); |
| 90 | |
| 91 | const action = actionsByRef.get(actionRef); |
| 92 | if (!action) { |
| 93 | throw new NotFoundError(`Action not found: ${actionRef}`, { |
| 94 | resourceType: 'action', |
| 95 | resourceId: actionRef, |
| 96 | details: { runId }, |
| 97 | }); |
| 98 | } |
| 99 | |
| 100 | const { triggeredBy, failure } = schedulerContext; |
| 101 | |
| 102 | const entry = componentRegistry.getMetadata(action.componentId); |
| 103 | if (!entry) { |
| 104 | throw new NotFoundError(`Component not registered: ${action.componentId}`, { |
| 105 | resourceType: 'component', |