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
//! [`Operation`] trait and [`OperationContext`] for user-defined step operations.
//!
//! This module provides the extension point for custom step types that integrate
//! into the workflow lifecycle. Common use cases include API clients (GitLab,
//! Gmail, Slack) that need full step tracking.
//!
//! # How it works
//!
//! 1. Implement [`Operation`] on your type.
//! 2. Call `WorkflowContext::operation()` inside a `WorkflowHandler`.
//! 3. The engine handles the full step lifecycle: create step record, transition
//! to Running, execute, persist output/duration, mark Completed or Failed.
//!
//! # OperationContext
//!
//! [`OperationContext`] is passed to every [`Operation::execute`] call. It
//! provides a shared [`reqwest::Client`] and a [`SecretResolver`] so that
//! operations do not need to create their own HTTP clients or manage
//! credentials manually.
//!
//! # Examples
//!
//! ```no_run
//! use ironflow_core::operation::{Operation, OperationContext};
//! use ironflow_core::error::OperationError;
//! use serde_json::{Value, json};
//! use std::future::Future;
//! use std::pin::Pin;
//!
//! struct CreateGitlabIssue {
//! project_id: u64,
//! title: String,
//! }
//!
//! impl Operation for CreateGitlabIssue {
//! fn kind(&self) -> &str {
//! "gitlab"
//! }
//!
//! fn execute<'a>(
//! &'a self,
//! ctx: &'a OperationContext,
//! ) -> Pin<Box<dyn Future<Output = Result<Value, OperationError>> + Send + 'a>> {
//! Box::pin(async move {
//! // Use ctx.http_client() for HTTP calls, ctx.secrets() for credentials.
//! Ok(json!({"issue_id": 42, "url": "https://gitlab.com/issues/42"}))
//! })
//! }
//! }
//! ```
use fmt;
use Future;
use Pin;
use Arc;
use Client;
use DeserializeOwned;
use Value;
use crateOperationError;
/// A decrypted secret value returned by [`SecretResolver::get`].
///
/// Wraps a plaintext string. The value is only available after successful
/// decryption by the underlying store.
///
/// # Examples
///
/// ```
/// use ironflow_core::operation::SecretValue;
///
/// let secret = SecretValue { value: "sk-ant-12345".to_string() };
/// assert_eq!(secret.value, "sk-ant-12345");
/// ```
/// Trait for resolving secrets at operation execution time.
///
/// Implementations provide read-only access to encrypted secrets scoped
/// to the current workflow. The engine passes a resolver through
/// [`OperationContext`] so that operations can fetch credentials without
/// depending on the store crate.
///
/// # Examples
///
/// ```
/// use ironflow_core::operation::{SecretResolver, NoopSecretResolver};
///
/// # tokio_test::block_on(async {
/// let resolver = NoopSecretResolver;
/// let result = resolver.get("any_key").await;
/// assert!(result.unwrap().is_none());
/// # });
/// ```
/// A [`SecretResolver`] that always returns `Ok(None)`.
///
/// Used in tests and when the `secret-store` feature is disabled.
///
/// # Examples
///
/// ```
/// use ironflow_core::operation::{SecretResolver, NoopSecretResolver};
///
/// # tokio_test::block_on(async {
/// let resolver = NoopSecretResolver;
/// assert!(resolver.get("anything").await.unwrap().is_none());
/// # });
/// ```
;
/// Context provided to every [`Operation::execute`] call.
///
/// Carries a shared HTTP client and a secret resolver so that operations
/// do not need to manage their own connections or credentials.
///
/// # Examples
///
/// ```
/// use ironflow_core::operation::{OperationContext, NoopSecretResolver};
/// use std::sync::Arc;
///
/// let ctx = OperationContext::new(Arc::new(NoopSecretResolver));
/// let _client = ctx.http_client();
/// ```
/// A user-defined operation that integrates into the workflow step lifecycle.
///
/// Implement this trait for custom integrations (GitLab, Gmail, Slack, etc.)
/// that need full step tracking when executed via `WorkflowContext::operation()`.
///
/// # Contract
///
/// - [`kind()`](Operation::kind) returns a short, lowercase identifier stored
/// as `StepKind::Custom` in the database (e.g. `"gitlab"`, `"gmail"`, `"slack"`).
/// - [`execute()`](Operation::execute) performs the operation and returns
/// a JSON [`Value`] on success. The engine persists this as the step output.
///
/// # Examples
///
/// ```no_run
/// use ironflow_core::operation::{Operation, OperationContext};
/// use ironflow_core::error::OperationError;
/// use serde_json::{Value, json};
/// use std::future::Future;
/// use std::pin::Pin;
///
/// struct SendSlackMessage {
/// channel: String,
/// text: String,
/// }
///
/// impl Operation for SendSlackMessage {
/// fn kind(&self) -> &str { "slack" }
///
/// fn execute<'a>(
/// &'a self,
/// ctx: &'a OperationContext,
/// ) -> Pin<Box<dyn Future<Output = Result<Value, OperationError>> + Send + 'a>> {
/// Box::pin(async move {
/// // Post to Slack API using ctx.http_client() ...
/// Ok(json!({"ok": true, "ts": "1234567890.123456"}))
/// })
/// }
/// }
/// ```
/// A typed extension of [`Operation`] that declares a concrete output type.
///
/// Implement this alongside [`Operation`] when the step output has a known
/// Rust type. Consumers can then deserialize the output without guessing
/// the shape.
///
/// # Examples
///
/// ```no_run
/// use ironflow_core::operation::{Operation, OperationContext, TypedOperation};
/// use ironflow_core::error::OperationError;
/// use serde::Deserialize;
/// use serde_json::{Value, json};
/// use std::future::Future;
/// use std::pin::Pin;
///
/// #[derive(Debug, Deserialize)]
/// struct IssueCreated {
/// iid: u64,
/// url: String,
/// }
///
/// struct CreateIssue;
///
/// impl Operation for CreateIssue {
/// fn kind(&self) -> &str { "gitlab" }
/// fn execute<'a>(
/// &'a self,
/// _ctx: &'a OperationContext,
/// ) -> Pin<Box<dyn Future<Output = Result<Value, OperationError>> + Send + 'a>> {
/// Box::pin(async { Ok(json!({"iid": 42, "url": "https://gitlab.com/issues/42"})) })
/// }
/// }
///
/// impl TypedOperation for CreateIssue {
/// type Output = IssueCreated;
/// }
/// ```