prodigy 0.4.4

Turn ad-hoc Claude sessions into reproducible development pipelines with parallel AI agents
Documentation
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
use super::provider::{InputConfig, InputProvider, ValidationIssue, ValidationSeverity};
use super::types::{
    DataFormat, ExecutionInput, InputType, VariableDefinition, VariableType, VariableValue,
};
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use std::io::{self, Read};
use tokio::io::{AsyncReadExt, BufReader};

pub struct StandardInputProvider;

#[async_trait]
impl InputProvider for StandardInputProvider {
    fn input_type(&self) -> InputType {
        InputType::StandardInput {
            format: DataFormat::Auto,
        }
    }

    async fn validate(&self, config: &InputConfig) -> Result<Vec<ValidationIssue>> {
        let mut issues = Vec::new();

        // Check if stdin is available (not in automation mode)
        if std::env::var("PRODIGY_AUTOMATION").unwrap_or_default() == "true"
            && !config.get_bool("allow_in_automation").unwrap_or(false)
        {
            issues.push(ValidationIssue {
                field: "stdin".to_string(),
                message: "Standard input not available in automation mode".to_string(),
                severity: ValidationSeverity::Error,
            });
        }

        // Validate format if specified
        if let Ok(format_str) = config.get_string("format") {
            if !["json", "yaml", "csv", "lines", "text", "auto"].contains(&format_str.as_str()) {
                issues.push(ValidationIssue {
                    field: "format".to_string(),
                    message: format!("Unsupported format for stdin: {}", format_str),
                    severity: ValidationSeverity::Error,
                });
            }
        }

        Ok(issues)
    }

    async fn generate_inputs(&self, config: &InputConfig) -> Result<Vec<ExecutionInput>> {
        let format = self.detect_format(config)?;
        let _inputs: Vec<ExecutionInput> = Vec::new();

        // Check for simulated input in test/automation mode
        if let Ok(simulated_input) = config.get_string("simulated_input") {
            return self.process_input(&simulated_input, format, config).await;
        }

        // Read from actual stdin
        let stdin_content = if config.get_bool("async_read").unwrap_or(true) {
            self.read_stdin_async().await?
        } else {
            self.read_stdin_sync()?
        };

        if stdin_content.is_empty() {
            return Ok(vec![]);
        }

        self.process_input(&stdin_content, format, config).await
    }

    fn available_variables(&self, config: &InputConfig) -> Result<Vec<VariableDefinition>> {
        let format = self.detect_format(config)?;

        let mut vars = vec![VariableDefinition {
            name: "stdin_format".to_string(),
            var_type: VariableType::String,
            description: "Format of the standard input data".to_string(),
            required: true,
            default_value: None,
            validation_rules: vec![],
        }];

        match format {
            DataFormat::Json | DataFormat::Yaml | DataFormat::Toml => {
                vars.push(VariableDefinition {
                    name: "stdin_data".to_string(),
                    var_type: VariableType::Object,
                    description: "Parsed standard input as structured data".to_string(),
                    required: true,
                    default_value: None,
                    validation_rules: vec![],
                });
            }
            DataFormat::PlainText => {
                vars.push(VariableDefinition {
                    name: "stdin_text".to_string(),
                    var_type: VariableType::String,
                    description: "Raw text from standard input".to_string(),
                    required: true,
                    default_value: None,
                    validation_rules: vec![],
                });
                vars.push(VariableDefinition {
                    name: "stdin_lines".to_string(),
                    var_type: VariableType::Array,
                    description: "Lines from standard input".to_string(),
                    required: false,
                    default_value: None,
                    validation_rules: vec![],
                });
            }
            _ => {
                vars.push(VariableDefinition {
                    name: "stdin_content".to_string(),
                    var_type: VariableType::String,
                    description: "Raw content from standard input".to_string(),
                    required: true,
                    default_value: None,
                    validation_rules: vec![],
                });
            }
        }

        Ok(vars)
    }

    fn supports(&self, config: &InputConfig) -> bool {
        config.get_bool("use_stdin").unwrap_or(false)
            || config
                .get_string("input_type")
                .map(|t| t == "stdin")
                .unwrap_or(false)
            || config.get_string("simulated_input").is_ok()
    }
}

impl StandardInputProvider {
    fn detect_format(&self, config: &InputConfig) -> Result<DataFormat> {
        if let Ok(format_str) = config.get_string("format") {
            return match format_str.as_str() {
                "json" => Ok(DataFormat::Json),
                "yaml" => Ok(DataFormat::Yaml),
                "csv" => Ok(DataFormat::Csv),
                "lines" | "text" => Ok(DataFormat::PlainText),
                "auto" => Ok(DataFormat::Auto),
                _ => Err(anyhow!("Unsupported stdin format: {}", format_str)),
            };
        }

        Ok(DataFormat::PlainText)
    }

    async fn read_stdin_async(&self) -> Result<String> {
        let stdin = tokio::io::stdin();
        let mut reader = BufReader::new(stdin);
        let mut buffer = String::new();

        // Set a timeout for reading
        let timeout_duration = std::time::Duration::from_secs(5);
        let read_future = reader.read_to_string(&mut buffer);

        match tokio::time::timeout(timeout_duration, read_future).await {
            Ok(Ok(_)) => Ok(buffer),
            Ok(Err(e)) => Err(anyhow!("Failed to read from stdin: {}", e)),
            Err(_) => Err(anyhow!("Timeout reading from stdin")),
        }
    }

    fn read_stdin_sync(&self) -> Result<String> {
        let mut buffer = String::new();
        let stdin = io::stdin();
        let mut handle = stdin.lock();

        handle
            .read_to_string(&mut buffer)
            .map_err(|e| anyhow!("Failed to read from stdin: {}", e))?;

        Ok(buffer)
    }

    async fn process_input(
        &self,
        content: &str,
        format: DataFormat,
        config: &InputConfig,
    ) -> Result<Vec<ExecutionInput>> {
        let mut inputs = Vec::new();

        match format {
            DataFormat::Json => {
                let parsed: serde_json::Value = serde_json::from_str(content)?;
                inputs.extend(self.process_json(parsed)?);
            }
            DataFormat::Yaml => {
                let parsed: serde_yaml::Value = serde_yaml::from_str(content)?;
                let json_value = serde_json::to_value(parsed)?;
                inputs.extend(self.process_json(json_value)?);
            }
            DataFormat::PlainText => {
                if config.get_bool("process_lines").unwrap_or(false) {
                    // Process each line as a separate input
                    for (index, line) in content.lines().enumerate() {
                        if line.trim().is_empty() && config.get_bool("skip_empty").unwrap_or(true) {
                            continue;
                        }

                        let mut input = ExecutionInput::new(
                            format!("stdin_line_{}", index),
                            InputType::StandardInput {
                                format: DataFormat::PlainText,
                            },
                        );

                        input.add_variable(
                            "stdin_line".to_string(),
                            VariableValue::String(line.to_string()),
                        );
                        input.add_variable(
                            "line_number".to_string(),
                            VariableValue::Number(index as i64 + 1),
                        );
                        input.add_variable(
                            "stdin_format".to_string(),
                            VariableValue::String("lines".to_string()),
                        );

                        inputs.push(input);
                    }
                } else {
                    // Process as single text block
                    let mut input = ExecutionInput::new(
                        "stdin_text".to_string(),
                        InputType::StandardInput {
                            format: DataFormat::PlainText,
                        },
                    );

                    input.add_variable(
                        "stdin_text".to_string(),
                        VariableValue::String(content.to_string()),
                    );

                    let lines: Vec<VariableValue> = content
                        .lines()
                        .map(|l| VariableValue::String(l.to_string()))
                        .collect();
                    input.add_variable("stdin_lines".to_string(), VariableValue::Array(lines));
                    input.add_variable(
                        "stdin_format".to_string(),
                        VariableValue::String("text".to_string()),
                    );

                    inputs.push(input);
                }
            }
            DataFormat::Auto => {
                // Try to auto-detect format from content
                let trimmed = content.trim();
                if trimmed.starts_with('{') || trimmed.starts_with('[') {
                    // Looks like JSON
                    if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(content) {
                        return self.process_json(parsed);
                    }
                }

                // Default to plain text - inline the logic to avoid recursion
                if config.get_bool("process_lines").unwrap_or(false) {
                    // Process each line as a separate input
                    for (index, line) in content.lines().enumerate() {
                        if line.trim().is_empty() && config.get_bool("skip_empty").unwrap_or(true) {
                            continue;
                        }

                        let mut input = ExecutionInput::new(
                            format!("stdin_line_{}", index),
                            InputType::StandardInput {
                                format: DataFormat::PlainText,
                            },
                        );

                        input.add_variable(
                            "stdin_line".to_string(),
                            VariableValue::String(line.to_string()),
                        );
                        input.add_variable(
                            "line_number".to_string(),
                            VariableValue::Number(index as i64 + 1),
                        );
                        input.add_variable(
                            "stdin_format".to_string(),
                            VariableValue::String("lines".to_string()),
                        );

                        inputs.push(input);
                    }
                } else {
                    // Process as single text block
                    let mut input = ExecutionInput::new(
                        "stdin_text".to_string(),
                        InputType::StandardInput {
                            format: DataFormat::PlainText,
                        },
                    );

                    input.add_variable(
                        "stdin_text".to_string(),
                        VariableValue::String(content.to_string()),
                    );

                    let lines: Vec<VariableValue> = content
                        .lines()
                        .map(|l| VariableValue::String(l.to_string()))
                        .collect();
                    input.add_variable("stdin_lines".to_string(), VariableValue::Array(lines));
                    input.add_variable(
                        "stdin_format".to_string(),
                        VariableValue::String("text".to_string()),
                    );

                    inputs.push(input);
                }
            }
            _ => {
                // For other formats, store as raw content
                let mut input = ExecutionInput::new(
                    "stdin_data".to_string(),
                    InputType::StandardInput {
                        format: format.clone(),
                    },
                );

                input.add_variable(
                    "stdin_content".to_string(),
                    VariableValue::String(content.to_string()),
                );
                input.add_variable(
                    "stdin_format".to_string(),
                    VariableValue::String(format!("{:?}", format)),
                );

                inputs.push(input);
            }
        }

        Ok(inputs)
    }

    fn process_json(&self, value: serde_json::Value) -> Result<Vec<ExecutionInput>> {
        let mut inputs = Vec::new();

        match value {
            serde_json::Value::Array(arr) => {
                for (index, item) in arr.iter().enumerate() {
                    let mut input = ExecutionInput::new(
                        format!("stdin_item_{}", index),
                        InputType::StandardInput {
                            format: DataFormat::Json,
                        },
                    );

                    input
                        .add_variable("stdin_data".to_string(), self.json_to_variable_value(item)?);
                    input.add_variable(
                        "item_index".to_string(),
                        VariableValue::Number(index as i64),
                    );
                    input.add_variable(
                        "stdin_format".to_string(),
                        VariableValue::String("json".to_string()),
                    );

                    inputs.push(input);
                }
            }
            _ => {
                let mut input = ExecutionInput::new(
                    "stdin_json".to_string(),
                    InputType::StandardInput {
                        format: DataFormat::Json,
                    },
                );

                input.add_variable(
                    "stdin_data".to_string(),
                    self.json_to_variable_value(&value)?,
                );
                input.add_variable(
                    "stdin_format".to_string(),
                    VariableValue::String("json".to_string()),
                );

                inputs.push(input);
            }
        }

        Ok(inputs)
    }

    #[allow(clippy::only_used_in_recursion)]
    fn json_to_variable_value(&self, value: &serde_json::Value) -> Result<VariableValue> {
        match value {
            serde_json::Value::Null => Ok(VariableValue::Null),
            serde_json::Value::Bool(b) => Ok(VariableValue::Boolean(*b)),
            serde_json::Value::Number(n) => {
                if let Some(i) = n.as_i64() {
                    Ok(VariableValue::Number(i))
                } else if let Some(f) = n.as_f64() {
                    Ok(VariableValue::Float(f))
                } else {
                    Err(anyhow!("Could not convert number"))
                }
            }
            serde_json::Value::String(s) => Ok(VariableValue::String(s.clone())),
            serde_json::Value::Array(arr) => {
                let values: Result<Vec<_>> =
                    arr.iter().map(|v| self.json_to_variable_value(v)).collect();
                Ok(VariableValue::Array(values?))
            }
            serde_json::Value::Object(obj) => {
                let mut map = std::collections::HashMap::new();
                for (k, v) in obj {
                    map.insert(k.clone(), self.json_to_variable_value(v)?);
                }
                Ok(VariableValue::Object(map))
            }
        }
    }
}