swf-runtime 1.0.0-alpha10

Runtime engine for Serverless Workflow DSL — execute, validate, and orchestrate workflows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
use crate::error::{WorkflowError, WorkflowResult};
use serde::de::DeserializeOwned;
use serde_json::Value;
use std::sync::Arc;

/// Read-only snapshot of workflow context variables available to task handlers.
///
/// Provides access to `$context`, `$secret`, `$workflow`, and other runtime variables
/// that were previously inaccessible from custom handlers.
///
/// # Example
///
/// ```no_run
/// use async_trait::async_trait;
/// use serde_json::Value;
/// use swf_runtime::{CustomTaskHandler, HandlerContext, WorkflowResult};
///
/// struct SmartHandler;
///
/// #[async_trait]
/// impl CustomTaskHandler for SmartHandler {
///     fn task_type(&self) -> &str { "smart" }
///
///     async fn handle(
///         &self,
///         task_name: &str,
///         task_type: &str,
///         task_config: &Value,
///         input: &Value,
///         context: &HandlerContext,
///     ) -> WorkflowResult<Value> {
///         // Access $context to read workflow state
///         let preferred = context.context().get("provider").and_then(|v| v.as_str());
///         // Access $secret for credentials
///         let api_key = context.secret().and_then(|s| s.get("API_KEY")).and_then(|v| v.as_str());
///         Ok(input.clone())
///     }
/// }
/// ```
#[derive(Debug, Clone)]
pub struct HandlerContext {
    context: Value,
    secret: Option<Value>,
    workflow: Value,
    authorization: Option<Value>,
}

impl HandlerContext {
    /// Creates a new HandlerContext from the current workflow context variables
    pub(crate) fn from_vars(vars: &std::collections::HashMap<String, Value>) -> Self {
        Self {
            context: vars
                .get(crate::context::vars::CONTEXT)
                .cloned()
                .unwrap_or(Value::Null),
            secret: vars.get(crate::context::vars::SECRET).cloned(),
            workflow: vars
                .get(crate::context::vars::WORKFLOW)
                .cloned()
                .unwrap_or(Value::Null),
            authorization: vars.get(crate::context::vars::AUTHORIZATION).cloned(),
        }
    }

    /// Returns the `$context` value (workflow instance state set by `export.as`)
    pub fn context(&self) -> &Value {
        &self.context
    }

    /// Returns the `$secret` value (all resolved secrets), if a secret manager is configured
    pub fn secret(&self) -> Option<&Value> {
        self.secret.as_ref()
    }

    /// Returns the `$workflow` descriptor (workflow metadata)
    pub fn workflow(&self) -> &Value {
        &self.workflow
    }

    /// Returns the `$authorization` value (set after HTTP authentication), if any
    pub fn authorization(&self) -> Option<&Value> {
        self.authorization.as_ref()
    }
}

/// Handler for call task types that require custom implementations.
///
/// Implement this trait to provide support for call types like gRPC, OpenAPI,
/// AsyncAPI, and A2A. Register handlers with `WorkflowRunner::with_call_handler()`.
///
/// # Example
///
/// ```no_run
/// use async_trait::async_trait;
/// use serde_json::Value;
/// use swf_runtime::{CallHandler, HandlerContext, WorkflowResult};
///
/// struct GrpcCallHandler;
///
/// #[async_trait]
/// impl CallHandler for GrpcCallHandler {
///     fn call_type(&self) -> &str { "grpc" }
///
///     async fn handle(
///         &self,
///         task_name: &str,
///         call_config: &Value,
///         input: &Value,
///         context: &HandlerContext,
///     ) -> WorkflowResult<Value> {
///         // Implement gRPC call logic here
///         Ok(serde_json::json!({ "result": "grpc response" }))
///     }
/// }
/// ```
#[async_trait::async_trait]
pub trait CallHandler: Send + Sync {
    /// Returns the call type this handler supports (e.g., "grpc", "openapi", "asyncapi", "a2a")
    fn call_type(&self) -> &str;

    /// Executes the call with the given configuration, input, and workflow context.
    async fn handle(
        &self,
        task_name: &str,
        call_config: &Value,
        input: &Value,
        context: &HandlerContext,
    ) -> WorkflowResult<Value>;
}

/// Handler for run task types that require custom implementations.
///
/// Implement this trait to provide support for run types like container and script.
/// Register handlers with `WorkflowRunner::with_run_handler()`.
///
/// # Example
///
/// ```no_run
/// use async_trait::async_trait;
/// use serde_json::Value;
/// use swf_runtime::{RunHandler, HandlerContext, WorkflowResult};
///
/// struct ContainerRunHandler;
///
/// #[async_trait]
/// impl RunHandler for ContainerRunHandler {
///     fn run_type(&self) -> &str { "container" }
///
///     async fn handle(
///         &self,
///         task_name: &str,
///         run_config: &Value,
///         input: &Value,
///         context: &HandlerContext,
///     ) -> WorkflowResult<Value> {
///         // Implement container run logic here
///         Ok(serde_json::json!({ "exitCode": 0 }))
///     }
/// }
/// ```
#[async_trait::async_trait]
pub trait RunHandler: Send + Sync {
    /// Returns the run type this handler supports (e.g., "container", "script")
    fn run_type(&self) -> &str;

    /// Executes the run with the given configuration, input, and workflow context.
    async fn handle(
        &self,
        task_name: &str,
        run_config: &Value,
        input: &Value,
        context: &HandlerContext,
    ) -> WorkflowResult<Value>;
}

/// Handler for custom/extension task types.
///
/// Implement this trait to provide support for custom task types that are
/// not part of the built-in Serverless Workflow specification.
/// Register handlers with `WorkflowRunner::with_custom_task_handler()`.
///
/// # Example
///
/// ```no_run
/// use async_trait::async_trait;
/// use serde_json::Value;
/// use swf_runtime::{CustomTaskHandler, HandlerContext, WorkflowResult};
///
/// struct UppercaseHandler;
///
/// #[async_trait]
/// impl CustomTaskHandler for UppercaseHandler {
///     fn task_type(&self) -> &str { "uppercase" }
///
///     async fn handle(
///         &self,
///         task_name: &str,
///         task_type: &str,
///         task_config: &Value,
///         input: &Value,
///         context: &HandlerContext,
///     ) -> WorkflowResult<Value> {
///         let text = input["text"].as_str().unwrap_or("");
///         Ok(serde_json::json!({ "result": text.to_uppercase() }))
///     }
/// }
/// ```
#[async_trait::async_trait]
pub trait CustomTaskHandler: Send + Sync {
    /// Returns the custom task type this handler supports (e.g., "myCustomTask")
    fn task_type(&self) -> &str;

    /// Executes the custom task with the given configuration, input, and workflow context.
    async fn handle(
        &self,
        task_name: &str,
        task_type: &str,
        task_config: &Value,
        input: &Value,
        context: &HandlerContext,
    ) -> WorkflowResult<Value>;
}

/// Type-safe custom task handler that receives a strongly-typed config instead of raw JSON.
///
/// Implement this trait to avoid manual JSON parsing in every handler. The framework
/// automatically deserializes `task_config` into [`Self::Config`] and converts
/// deserialization errors into `WorkflowError::validation`.
///
/// Use [`TypedCustomTaskHandler::into_boxed`] to convert a typed handler into a
/// `Box<dyn CustomTaskHandler>` that can be registered with `WorkflowRunner::with_custom_task_handler()`.
///
/// # Example
///
/// ```no_run
/// use async_trait::async_trait;
/// use serde::Deserialize;
/// use serde_json::{json, Value};
/// use swf_runtime::{TypedCustomTaskHandler, HandlerContext, WorkflowResult};
///
/// #[derive(Deserialize)]
/// struct ProviderConfig {
///     name: String,
///     operation: String,
///     #[serde(default)]
///     timeout: Option<String>,
/// }
///
/// struct ProviderHandler;
///
/// #[async_trait]
/// impl TypedCustomTaskHandler for ProviderHandler {
///     type Config = ProviderConfig;
///
///     fn task_type(&self) -> &str { "provider" }
///
///     async fn handle(
///         &self,
///         task_name: &str,
///         config: &ProviderConfig,
///         input: &Value,
///         context: &HandlerContext,
///     ) -> WorkflowResult<Value> {
///         // config.name and config.operation are directly available — no manual parsing
///         Ok(json!({ "provider": config.name, "op": config.operation }))
///     }
/// }
///
/// // Register with the runner:
/// // runner.with_custom_task_handler(ProviderHandler.into_boxed())
/// ```
#[async_trait::async_trait]
pub trait TypedCustomTaskHandler: Send + Sync + 'static {
    /// The strongly-typed configuration for this handler.
    type Config: DeserializeOwned + Send + Sync + 'static;

    /// Returns the custom task type this handler supports
    fn task_type(&self) -> &str;

    /// Executes the custom task with the given typed configuration, input, and workflow context.
    async fn handle(
        &self,
        task_name: &str,
        config: &Self::Config,
        input: &Value,
        context: &HandlerContext,
    ) -> WorkflowResult<Value>;

    /// Converts this typed handler into a `Box<dyn CustomTaskHandler>` for registration.
    ///
    /// The returned wrapper automatically deserializes the config JSON into `Self::Config`
    /// before calling [`handle`](TypedCustomTaskHandler::handle).
    fn into_boxed(self) -> Box<dyn CustomTaskHandler>
    where
        Self: Sized,
    {
        Box::new(TypedHandlerWrapper(self))
    }
}

/// Wrapper that adapts a [`TypedCustomTaskHandler`] into a [`CustomTaskHandler`].
struct TypedHandlerWrapper<H: TypedCustomTaskHandler>(H);

#[async_trait::async_trait]
impl<H: TypedCustomTaskHandler> CustomTaskHandler for TypedHandlerWrapper<H> {
    fn task_type(&self) -> &str {
        self.0.task_type()
    }

    async fn handle(
        &self,
        task_name: &str,
        _task_type: &str,
        task_config: &Value,
        input: &Value,
        context: &HandlerContext,
    ) -> WorkflowResult<Value> {
        let config: H::Config = serde_json::from_value(task_config.clone()).map_err(|e| {
            WorkflowError::validation(
                format!("invalid config for '{}' task: {}", self.0.task_type(), e),
                task_name,
            )
        })?;
        self.0.handle(task_name, &config, input, context).await
    }
}

/// Unified handler interface for all task types.
///
/// This trait provides a single interface that abstracts over [`CallHandler`],
/// [`RunHandler`], and [`CustomTaskHandler`]. All three can be adapted to `TaskHandler`
/// via blanket implementations, and registered in the same [`HandlerRegistry`].
///
/// Users can also implement `TaskHandler` directly for maximum flexibility,
/// bypassing the type-specific traits entirely.
#[async_trait::async_trait]
pub trait TaskHandler: Send + Sync {
    /// Returns the handler type key (e.g., "grpc", "provider", "container")
    fn handler_type(&self) -> &str;

    /// Executes the handler with the given configuration, input, and workflow context.
    async fn handle(
        &self,
        task_name: &str,
        config: &Value,
        input: &Value,
        context: &HandlerContext,
    ) -> WorkflowResult<Value>;
}

/// Wrapper adapting a [`CallHandler`] into a [`TaskHandler`].
struct CallHandlerAdapter(std::sync::Arc<dyn CallHandler>);

#[async_trait::async_trait]
impl TaskHandler for CallHandlerAdapter {
    fn handler_type(&self) -> &str {
        self.0.call_type()
    }

    async fn handle(
        &self,
        task_name: &str,
        config: &Value,
        input: &Value,
        context: &HandlerContext,
    ) -> WorkflowResult<Value> {
        self.0.handle(task_name, config, input, context).await
    }
}

/// Wrapper adapting a [`RunHandler`] into a [`TaskHandler`].
struct RunHandlerAdapter(std::sync::Arc<dyn RunHandler>);

#[async_trait::async_trait]
impl TaskHandler for RunHandlerAdapter {
    fn handler_type(&self) -> &str {
        self.0.run_type()
    }

    async fn handle(
        &self,
        task_name: &str,
        config: &Value,
        input: &Value,
        context: &HandlerContext,
    ) -> WorkflowResult<Value> {
        self.0.handle(task_name, config, input, context).await
    }
}

/// Wrapper adapting a [`CustomTaskHandler`] into a [`TaskHandler`].
struct CustomHandlerAdapter(std::sync::Arc<dyn CustomTaskHandler>);

#[async_trait::async_trait]
impl TaskHandler for CustomHandlerAdapter {
    fn handler_type(&self) -> &str {
        self.0.task_type()
    }

    async fn handle(
        &self,
        task_name: &str,
        config: &Value,
        input: &Value,
        context: &HandlerContext,
    ) -> WorkflowResult<Value> {
        self.0
            .handle(task_name, self.0.task_type(), config, input, context)
            .await
    }
}

/// Registry of call, run, and custom task handlers.
///
/// Internally stores all handlers in a unified map keyed by handler type.
/// Legacy `get_call_handler` / `get_run_handler` / `get_custom_task_handler` methods
/// remain for backward compatibility, but `get_handler` provides unified access.
#[derive(Default, Clone)]
pub struct HandlerRegistry {
    handlers: std::sync::Arc<std::collections::HashMap<String, std::sync::Arc<dyn TaskHandler>>>,
    // Legacy typed maps kept for backward-compatible getter methods
    call_handlers:
        std::sync::Arc<std::collections::HashMap<String, std::sync::Arc<dyn CallHandler>>>,
    run_handlers: std::sync::Arc<std::collections::HashMap<String, std::sync::Arc<dyn RunHandler>>>,
    custom_task_handlers:
        std::sync::Arc<std::collections::HashMap<String, std::sync::Arc<dyn CustomTaskHandler>>>,
}

impl HandlerRegistry {
    /// Creates a new empty handler registry
    pub fn new() -> Self {
        Self::default()
    }

    /// Registers a unified [`TaskHandler`].
    pub fn register_handler(&mut self, handler: std::sync::Arc<dyn TaskHandler>) {
        let key = handler.handler_type().to_string();
        Arc::make_mut(&mut self.handlers).insert(key, handler);
    }

    /// Registers a call handler (backward compatible)
    pub fn register_call_handler(&mut self, handler: Box<dyn CallHandler>) {
        let key = handler.call_type().to_string();
        let arc: std::sync::Arc<dyn CallHandler> = std::sync::Arc::from(handler);
        Arc::make_mut(&mut self.handlers).insert(
            key.clone(),
            std::sync::Arc::new(CallHandlerAdapter(arc.clone())),
        );
        Arc::make_mut(&mut self.call_handlers).insert(key, arc);
    }

    /// Registers a run handler (backward compatible)
    pub fn register_run_handler(&mut self, handler: Box<dyn RunHandler>) {
        let key = handler.run_type().to_string();
        let arc: std::sync::Arc<dyn RunHandler> = std::sync::Arc::from(handler);
        Arc::make_mut(&mut self.handlers).insert(
            key.clone(),
            std::sync::Arc::new(RunHandlerAdapter(arc.clone())),
        );
        Arc::make_mut(&mut self.run_handlers).insert(key, arc);
    }

    /// Registers a custom task handler (backward compatible)
    pub fn register_custom_task_handler(&mut self, handler: Box<dyn CustomTaskHandler>) {
        let key = handler.task_type().to_string();
        let arc: std::sync::Arc<dyn CustomTaskHandler> = std::sync::Arc::from(handler);
        Arc::make_mut(&mut self.handlers).insert(
            key.clone(),
            std::sync::Arc::new(CustomHandlerAdapter(arc.clone())),
        );
        Arc::make_mut(&mut self.custom_task_handlers).insert(key, arc);
    }

    /// Looks up a handler by type from the unified registry
    pub fn get_handler(&self, handler_type: &str) -> Option<std::sync::Arc<dyn TaskHandler>> {
        self.handlers.get(handler_type).cloned()
    }

    /// Looks up a call handler by type (backward compatible)
    pub fn get_call_handler(&self, call_type: &str) -> Option<std::sync::Arc<dyn CallHandler>> {
        self.call_handlers.get(call_type).cloned()
    }

    /// Looks up a run handler by type (backward compatible)
    pub fn get_run_handler(&self, run_type: &str) -> Option<std::sync::Arc<dyn RunHandler>> {
        self.run_handlers.get(run_type).cloned()
    }

    /// Looks up a custom task handler by task type (backward compatible)
    pub fn get_custom_task_handler(
        &self,
        task_type: &str,
    ) -> Option<std::sync::Arc<dyn CustomTaskHandler>> {
        self.custom_task_handlers.get(task_type).cloned()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::Deserialize;

    #[derive(Deserialize)]
    struct TestConfig {
        name: String,
        #[serde(default)]
        count: u32,
    }

    struct TestTypedHandler;

    #[async_trait::async_trait]
    impl TypedCustomTaskHandler for TestTypedHandler {
        type Config = TestConfig;

        fn task_type(&self) -> &str {
            "test_typed"
        }

        async fn handle(
            &self,
            _task_name: &str,
            config: &TestConfig,
            _input: &Value,
            _context: &HandlerContext,
        ) -> WorkflowResult<Value> {
            Ok(serde_json::json!({
                "name": config.name,
                "count": config.count,
            }))
        }
    }

    #[tokio::test]
    async fn test_typed_handler_wrapper() {
        let handler = TestTypedHandler.into_boxed();
        let ctx = HandlerContext::from_vars(&std::collections::HashMap::new());
        let config = serde_json::json!({ "name": "hello", "count": 42 });

        let result = handler
            .handle("task1", "test_typed", &config, &serde_json::json!({}), &ctx)
            .await
            .unwrap();

        assert_eq!(result["name"], "hello");
        assert_eq!(result["count"], 42);
    }

    #[tokio::test]
    async fn test_typed_handler_invalid_config_returns_validation_error() {
        let handler = TestTypedHandler.into_boxed();
        let ctx = HandlerContext::from_vars(&std::collections::HashMap::new());
        let bad_config = serde_json::json!({ "count": 5 });

        let result = handler
            .handle(
                "task1",
                "test_typed",
                &bad_config,
                &serde_json::json!({}),
                &ctx,
            )
            .await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("invalid config"));
        assert!(err.to_string().contains("test_typed"));
    }

    #[tokio::test]
    async fn test_typed_handler_register_in_registry() {
        let mut registry = HandlerRegistry::new();
        registry.register_custom_task_handler(TestTypedHandler.into_boxed());

        let handler = registry.get_custom_task_handler("test_typed");
        assert!(handler.is_some());

        let ctx = HandlerContext::from_vars(&std::collections::HashMap::new());
        let config = serde_json::json!({ "name": "world" });
        let result = handler
            .unwrap()
            .handle("task1", "test_typed", &config, &serde_json::json!({}), &ctx)
            .await
            .unwrap();
        assert_eq!(result["name"], "world");
        assert_eq!(result["count"], 0);
    }
}