* Execute a workflow with optional input data * @param workflowId - The ID of the workflow to execute * @param input - Input data to pass to the workflow (object, primitive, or array) * @param options - Execution options (timeout, stream, async, etc.)
(
workflowId: string,
input?: any,
options: ExecutionOptions = {}
)
| 199 | * @param options - Execution options (timeout, stream, async, etc.) |
| 200 | */ |
| 201 | async executeWorkflow( |
| 202 | workflowId: string, |
| 203 | input?: any, |
| 204 | options: ExecutionOptions = {} |
| 205 | ): Promise<WorkflowExecutionResult | AsyncExecutionResult> { |
| 206 | const url = `${this.baseUrl}/api/workflows/${workflowId}/execute` |
| 207 | const { timeout = 30000, stream, selectedOutputs, async } = options |
| 208 | |
| 209 | try { |
| 210 | const timeoutPromise = new Promise<never>((_, reject) => { |
| 211 | setTimeout(() => reject(new Error('TIMEOUT')), timeout) |
| 212 | }) |
| 213 | |
| 214 | const headers: Record<string, string> = { |
| 215 | 'Content-Type': 'application/json', |
| 216 | 'X-API-Key': this.apiKey, |
| 217 | } |
| 218 | if (async) { |
| 219 | headers['X-Execution-Mode'] = 'async' |
| 220 | } |
| 221 | |
| 222 | let jsonBody: any = {} |
| 223 | if (input !== undefined && input !== null) { |
| 224 | if (typeof input === 'object' && input !== null && !Array.isArray(input)) { |
| 225 | jsonBody = { ...input } |
| 226 | } else { |
| 227 | jsonBody = { input } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | jsonBody = await this.convertFilesToBase64(jsonBody) |
| 232 | |
| 233 | if (stream !== undefined) { |
| 234 | jsonBody.stream = stream |
| 235 | } |
| 236 | if (selectedOutputs !== undefined) { |
| 237 | jsonBody.selectedOutputs = selectedOutputs |
| 238 | } |
| 239 | |
| 240 | const fetchPromise = fetch(url, { |
| 241 | method: 'POST', |
| 242 | headers, |
| 243 | body: JSON.stringify(jsonBody), |
| 244 | }) |
| 245 | |
| 246 | const response = await Promise.race([fetchPromise, timeoutPromise]) |
| 247 | |
| 248 | this.updateRateLimitInfo(response) |
| 249 | |
| 250 | if (response.status === 429) { |
| 251 | const retryAfter = this.rateLimitInfo?.retryAfter || 1000 |
| 252 | throw new SimStudioError( |
| 253 | `Rate limit exceeded. Retry after ${retryAfter}ms`, |
| 254 | 'RATE_LIMIT_EXCEEDED', |
| 255 | 429 |
| 256 | ) |
| 257 | } |
| 258 |
no test coverage detected