nika 0.35.4

Semantic YAML workflow engine for AI tasks - DAG execution, MCP integration, multi-provider LLM support
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
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//! Analyzed task AST.
//!
//! Tasks with resolved references - TaskId instead of String.
//!

use indexmap::IndexMap;

use super::ids::TaskId;
use crate::ast::artifact::ArtifactSpec;
use crate::ast::decompose::DecomposeSpec;
use crate::ast::logging::LogConfig;
use crate::ast::structured::StructuredOutputSpec;
use crate::binding::WithSpec;
use crate::source::Span;

/// An analyzed task - validated and resolved.
///
/// All string references are replaced with interned IDs.
///
#[derive(Debug, Clone)]
pub struct AnalyzedTask {
    /// Task ID (interned)
    pub id: TaskId,

    /// Task name (for display/debugging)
    pub name: String,

    /// Optional description
    pub description: Option<String>,

    /// The action this task performs
    pub action: AnalyzedTaskAction,

    /// Task-specific provider override
    pub provider: Option<String>,

    /// Task-specific model override
    pub model: Option<String>,

    /// Parsed `with:` bindings (alias → WithEntry with source, transforms, defaults)
    ///
    /// Phase 2 parses raw `Spanned<String>` values via `parse_with_entry()`.
    /// Each entry has a `BindingPath` source, optional transforms, defaults, and type.
    pub with_spec: WithSpec,

    /// Explicit ordering dependencies: `depends_on: [task_id1, task_id2]`
    ///
    /// These are pure ordering edges — no data flows through them.
    /// Resolved from raw string task names to interned `TaskId`.
    pub depends_on: Vec<TaskId>,

    /// Implicit dependencies auto-extracted from `with:` bindings.
    ///
    /// When a `WithEntry` source references a task (e.g., `step1.data`),
    /// the analyzer extracts `step1` as an implicit dependency.
    /// These are used by the DAG builder alongside `depends_on`.
    pub implicit_deps: Vec<TaskId>,

    /// Output configuration
    pub output: Option<AnalyzedOutput>,

    /// For-each iteration configuration
    pub for_each: Option<AnalyzedForEach>,

    /// Retry configuration
    pub retry: Option<AnalyzedRetry>,

    /// Decompose modifier for runtime DAG expansion
    pub decompose: Option<DecomposeSpec>,

    /// Standalone concurrency (used with decompose when no for_each)
    pub concurrency: Option<u32>,

    /// Standalone fail_fast (used with decompose when no for_each)
    pub fail_fast: Option<bool>,

    /// Artifact configuration for file persistence
    pub artifact: Option<ArtifactSpec>,

    /// Per-task log configuration
    pub log: Option<LogConfig>,

    /// Structured output specification (JSON schema enforcement)
    pub structured: Option<StructuredOutputSpec>,

    /// Span of the task
    pub span: Span,
}

/// The action a task performs (analyzed).
#[derive(Debug, Clone)]
pub enum AnalyzedTaskAction {
    /// LLM inference
    Infer(AnalyzedInferAction),

    /// Shell command execution
    Exec(AnalyzedExecAction),

    /// HTTP fetch
    Fetch(AnalyzedFetchAction),

    /// MCP tool invocation
    Invoke(AnalyzedInvokeAction),

    /// Autonomous agent
    Agent(AnalyzedAgentAction),
}

impl Default for AnalyzedTaskAction {
    fn default() -> Self {
        AnalyzedTaskAction::Infer(AnalyzedInferAction::default())
    }
}

impl AnalyzedTaskAction {
    /// Get the verb name.
    pub fn verb_name(&self) -> &'static str {
        match self {
            AnalyzedTaskAction::Infer(_) => "infer",
            AnalyzedTaskAction::Exec(_) => "exec",
            AnalyzedTaskAction::Fetch(_) => "fetch",
            AnalyzedTaskAction::Invoke(_) => "invoke",
            AnalyzedTaskAction::Agent(_) => "agent",
        }
    }
}

/// Analyzed infer action.
#[derive(Debug, Clone, Default)]
pub struct AnalyzedInferAction {
    /// The prompt to send to the LLM (may be empty when content is present)
    pub prompt: String,

    /// System prompt override
    pub system: Option<String>,

    /// Temperature (validated: 0.0 - 2.0)
    pub temperature: Option<f64>,

    /// Maximum tokens to generate
    pub max_tokens: Option<u32>,

    /// Enable extended thinking
    pub thinking: Option<bool>,

    /// Thinking budget tokens
    pub thinking_budget: Option<u32>,

    /// Multimodal content parts for vision (analyzed, spans stripped)
    pub content: Option<Vec<crate::ast::content::AnalyzedContentPart>>,

    /// Expected response format: text, json, markdown
    pub response_format: Option<String>,

    /// Guardrails for validating infer output
    pub guardrails: Vec<crate::ast::guardrails::GuardrailConfig>,

    /// Span of the action
    pub span: Span,
}

/// Analyzed exec action.
#[derive(Debug, Clone, Default)]
pub struct AnalyzedExecAction {
    /// Command to execute
    pub command: String,

    /// Run through shell
    pub shell: bool,

    /// Working directory
    pub working_dir: Option<String>,

    /// Environment variables
    pub env: IndexMap<String, String>,

    /// Timeout in milliseconds
    pub timeout_ms: Option<u64>,

    /// Span of the action
    pub span: Span,
}

/// Analyzed fetch action.
#[derive(Debug, Clone, Default)]
pub struct AnalyzedFetchAction {
    /// URL to fetch
    pub url: String,

    /// HTTP method
    pub method: HttpMethod,

    /// HTTP headers
    pub headers: IndexMap<String, String>,

    /// Request body
    pub body: Option<String>,

    /// Request body as JSON
    pub json: Option<serde_json::Value>,

    /// Timeout in milliseconds
    pub timeout_ms: Option<u64>,

    /// Follow redirects
    pub follow_redirects: bool,

    /// Response mode: "full" or "binary"
    pub response: Option<String>,

    /// Extraction mode: markdown, article, text, selector, metadata, links, feed, jsonpath, llm_txt
    pub extract: Option<String>,

    /// CSS selector or JSONPath expression (used with extract)
    pub selector: Option<String>,

    /// Span of the action
    pub span: Span,
}

/// HTTP methods.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum HttpMethod {
    #[default]
    Get,
    Post,
    Put,
    Patch,
    Delete,
    Head,
    Options,
}

impl HttpMethod {
    /// Parse an HTTP method string.
    pub fn parse(s: &str) -> Option<Self> {
        match s.to_uppercase().as_str() {
            "GET" => Some(Self::Get),
            "POST" => Some(Self::Post),
            "PUT" => Some(Self::Put),
            "PATCH" => Some(Self::Patch),
            "DELETE" => Some(Self::Delete),
            "HEAD" => Some(Self::Head),
            "OPTIONS" => Some(Self::Options),
            _ => None,
        }
    }

    /// Get the method as a string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Get => "GET",
            Self::Post => "POST",
            Self::Put => "PUT",
            Self::Patch => "PATCH",
            Self::Delete => "DELETE",
            Self::Head => "HEAD",
            Self::Options => "OPTIONS",
        }
    }
}

/// Analyzed invoke action.
#[derive(Debug, Clone, Default)]
pub struct AnalyzedInvokeAction {
    /// MCP server name (None = first available)
    pub server: Option<String>,

    /// Tool name
    pub tool: String,

    /// Tool parameters
    pub params: Option<serde_json::Value>,

    /// Timeout for tool execution
    pub timeout_ms: Option<u64>,

    /// Span of the action
    pub span: Span,
}

/// Analyzed agent action.
#[derive(Debug, Clone, Default)]
pub struct AnalyzedAgentAction {
    /// The prompt for the agent
    pub prompt: String,

    /// Available tools
    pub tools: Vec<String>,

    /// Maximum iterations
    pub max_iterations: Option<u32>,

    /// Maximum tokens per response
    pub max_tokens: Option<u32>,

    /// Agent definition reference (resolved)
    pub from: Option<String>,

    /// Skills to inject
    pub skills: Vec<String>,

    /// MCP servers for tool access
    pub mcp: Vec<String>,

    /// System prompt (agent persona)
    pub system: Option<String>,

    /// Temperature for LLM sampling
    pub temperature: Option<f64>,

    /// Token budget for the agent
    pub token_budget: Option<u32>,

    /// Enable extended thinking (Claude)
    pub extended_thinking: Option<bool>,

    /// Thinking budget tokens
    pub thinking_budget: Option<u32>,

    /// Max spawn_agent recursion depth
    pub depth_limit: Option<u32>,

    /// Tool choice behavior: auto, required, none
    pub tool_choice: Option<String>,

    /// Sequences that stop generation (passed to LLM)
    pub stop_sequences: Vec<String>,

    /// Scope preset (full, minimal, debug)
    pub scope: Option<String>,

    /// Span of the action
    pub span: Span,
}

/// Analyzed output configuration.
#[derive(Debug, Clone)]
pub struct AnalyzedOutput {
    /// Output format
    pub format: OutputFormat,

    /// JSON Schema for validation (validated)
    pub schema: Option<serde_json::Value>,

    /// Schema reference: file path or named ref (from `schema_ref:` / `$ref:`)
    pub schema_ref: Option<String>,

    /// Maximum retries on validation failure
    pub max_retries: Option<u32>,

    /// Span of the output config
    pub span: Span,
}

/// Output format.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum OutputFormat {
    #[default]
    Text,
    Json,
    Yaml,
}

impl OutputFormat {
    /// Parse an output format string.
    pub fn parse(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "text" => Some(Self::Text),
            "json" => Some(Self::Json),
            "yaml" => Some(Self::Yaml),
            _ => None,
        }
    }

    /// Get the format as a string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Text => "text",
            Self::Json => "json",
            Self::Yaml => "yaml",
        }
    }
}

/// Analyzed for-each iteration configuration.
#[derive(Debug, Clone)]
pub struct AnalyzedForEach {
    /// Items expression (binding expression or serialized array)
    pub items: String,

    /// Loop variable name (default: "item")
    pub as_var: String,

    /// Maximum concurrency (None = unlimited)
    pub parallel: Option<u32>,

    /// Fail fast on first error (default: true)
    pub fail_fast: bool,

    /// Span of the for_each config
    pub span: Span,
}

impl Default for AnalyzedForEach {
    fn default() -> Self {
        Self {
            items: String::new(),
            as_var: "item".to_string(),
            parallel: Some(1), // Default to sequential
            fail_fast: true,
            span: Span::dummy(),
        }
    }
}

impl AnalyzedForEach {
    /// Check if this is a binding expression.
    pub fn is_binding(&self) -> bool {
        self.items.starts_with("{{") || self.items.starts_with("$")
    }

    /// Check if items is a literal array.
    pub fn is_array(&self) -> bool {
        self.items.starts_with('[')
    }

    /// Parse items as a JSON array if it's a literal.
    pub fn parse_items(&self) -> Option<Vec<serde_json::Value>> {
        if self.is_array() {
            serde_json::from_str(&self.items).ok()
        } else {
            None
        }
    }
}

/// Analyzed retry configuration.
#[derive(Debug, Clone)]
pub struct AnalyzedRetry {
    /// Maximum retry attempts (validated: 1-10)
    pub max_attempts: u32,

    /// Delay between retries in milliseconds (validated: 0-60000)
    pub delay_ms: u64,

    /// Exponential backoff multiplier (validated: 1.0-5.0)
    pub backoff: Option<f64>,

    /// Span of the retry config
    pub span: Span,
}

impl Default for AnalyzedRetry {
    fn default() -> Self {
        Self {
            max_attempts: 3,
            delay_ms: 1000,
            backoff: None,
            span: Span::dummy(),
        }
    }
}

impl AnalyzedRetry {
    /// Calculate delay for a given attempt (0-indexed).
    pub fn delay_for_attempt(&self, attempt: u32) -> u64 {
        if attempt == 0 {
            return 0; // No delay for first attempt
        }
        match self.backoff {
            Some(multiplier) => {
                let factor = multiplier.powi(attempt as i32 - 1);
                (self.delay_ms as f64 * factor) as u64
            }
            None => self.delay_ms,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::source::FileId;

    fn make_span(start: u32, end: u32) -> Span {
        Span::new(FileId(0), start, end)
    }

    #[test]
    fn test_http_method_parse() {
        assert_eq!(HttpMethod::parse("GET"), Some(HttpMethod::Get));
        assert_eq!(HttpMethod::parse("get"), Some(HttpMethod::Get));
        assert_eq!(HttpMethod::parse("POST"), Some(HttpMethod::Post));
        assert_eq!(HttpMethod::parse("UNKNOWN"), None);
    }

    #[test]
    fn test_output_format_parse() {
        assert_eq!(OutputFormat::parse("text"), Some(OutputFormat::Text));
        assert_eq!(OutputFormat::parse("JSON"), Some(OutputFormat::Json));
        assert_eq!(OutputFormat::parse("yaml"), Some(OutputFormat::Yaml));
        assert_eq!(OutputFormat::parse("unknown"), None);
    }

    #[test]
    fn test_analyzed_task_action_verb() {
        let infer = AnalyzedTaskAction::Infer(AnalyzedInferAction::default());
        assert_eq!(infer.verb_name(), "infer");

        let exec = AnalyzedTaskAction::Exec(AnalyzedExecAction::default());
        assert_eq!(exec.verb_name(), "exec");
    }

    #[test]
    fn test_analyzed_task_with_spec() {
        use crate::binding::types::{BindingPath, BindingSource, PathSegment};
        use crate::binding::{WithEntry, WithSpec};

        let mut with_spec = WithSpec::default();
        with_spec.insert(
            "data".to_string(),
            WithEntry::simple(BindingPath {
                source: BindingSource::Task("step1".into()),
                segments: vec![PathSegment::Field("result".into())],
            }),
        );

        assert_eq!(with_spec.len(), 1);
        let entry = with_spec.get("data").unwrap();
        assert_eq!(entry.task_id(), Some("step1"));
    }

    #[test]
    fn test_analyzed_for_each_default() {
        let for_each = AnalyzedForEach::default();
        assert_eq!(for_each.as_var, "item");
        assert_eq!(for_each.parallel, Some(1)); // Sequential by default
        assert!(for_each.fail_fast);
    }

    #[test]
    fn test_analyzed_for_each_is_binding() {
        let for_each = AnalyzedForEach {
            items: "{{with.items}}".to_string(),
            ..Default::default()
        };
        assert!(for_each.is_binding());

        let for_each = AnalyzedForEach {
            items: "$items".to_string(),
            ..Default::default()
        };
        assert!(for_each.is_binding());

        let for_each = AnalyzedForEach {
            items: r#"["a", "b", "c"]"#.to_string(),
            ..Default::default()
        };
        assert!(!for_each.is_binding());
    }

    #[test]
    fn test_analyzed_for_each_is_array() {
        let for_each = AnalyzedForEach {
            items: r#"["a", "b", "c"]"#.to_string(),
            ..Default::default()
        };
        assert!(for_each.is_array());

        let for_each = AnalyzedForEach {
            items: "{{with.items}}".to_string(),
            ..Default::default()
        };
        assert!(!for_each.is_array());
    }

    #[test]
    fn test_analyzed_for_each_parse_items() {
        let for_each = AnalyzedForEach {
            items: r#"["a", "b", "c"]"#.to_string(),
            ..Default::default()
        };
        let items = for_each.parse_items().unwrap();
        assert_eq!(items.len(), 3);
        assert_eq!(items[0], serde_json::Value::String("a".to_string()));

        let for_each = AnalyzedForEach {
            items: "{{with.items}}".to_string(),
            ..Default::default()
        };
        assert!(for_each.parse_items().is_none());
    }

    #[test]
    fn test_analyzed_retry_default() {
        let retry = AnalyzedRetry::default();
        assert_eq!(retry.max_attempts, 3);
        assert_eq!(retry.delay_ms, 1000);
        assert!(retry.backoff.is_none());
    }

    #[test]
    fn test_analyzed_retry_delay_for_attempt() {
        // Without backoff
        let retry = AnalyzedRetry {
            max_attempts: 3,
            delay_ms: 1000,
            backoff: None,
            span: make_span(0, 10),
        };
        assert_eq!(retry.delay_for_attempt(0), 0); // First attempt, no delay
        assert_eq!(retry.delay_for_attempt(1), 1000); // Retry 1
        assert_eq!(retry.delay_for_attempt(2), 1000); // Retry 2

        // With exponential backoff
        let retry = AnalyzedRetry {
            max_attempts: 5,
            delay_ms: 1000,
            backoff: Some(2.0),
            span: make_span(0, 10),
        };
        assert_eq!(retry.delay_for_attempt(0), 0); // No delay
        assert_eq!(retry.delay_for_attempt(1), 1000); // 1000 * 2^0
        assert_eq!(retry.delay_for_attempt(2), 2000); // 1000 * 2^1
        assert_eq!(retry.delay_for_attempt(3), 4000); // 1000 * 2^2
    }
}