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
//! AST Index for position-aware LSP lookups.
//!
//! This module provides the foundation for Phase 2 LSP support by caching
//! parsed ASTs and providing efficient position-based lookups.
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────────┐
//! │  DocumentStore                                                      │
//! │  ├── documents: HashMap<Uri, String>  (raw text)                   │
//! │                                                                     │
//! │  AstIndex                                                           │
//! │  ├── cache: DashMap<Uri, CachedAst>                                │
//! │  │   ├── raw: Option<RawWorkflow>       (Phase 1 parse)            │
//! │  │   ├── analyzed: Option<AnalyzedWorkflow>  (Phase 2 analyze)     │
//! │  │   ├── errors: Vec<AnalyzeError>       (for diagnostics)         │
//! │  │   └── version: i32                    (document version)        │
//! │  │                                                                  │
//! │  └── Methods:                                                       │
//! │      ├── parse_document()  → Updates cache                         │
//! │      ├── get_node_at_position() → AstNode enum                     │
//! │      ├── get_task_at_position() → &AnalyzedTask                    │
//! │      └── invalidate()  → Clears cache entry                        │
//! └─────────────────────────────────────────────────────────────────────┘
//! ```

#[cfg(feature = "lsp")]
use dashmap::DashMap;

#[cfg(feature = "lsp")]
use tower_lsp_server::ls_types::{Position, Uri};

#[cfg(feature = "lsp")]
use crate::ast::analyzed::{AnalyzedTask, AnalyzedTaskAction, AnalyzedWorkflow};
#[cfg(feature = "lsp")]
use crate::ast::analyzer::{analyze, AnalyzeError};
#[cfg(feature = "lsp")]
use crate::ast::raw::{self, ParseError, RawWorkflow};
#[cfg(feature = "lsp")]
use crate::source::{FileId, Span};

#[cfg(feature = "lsp")]
use super::conversion::position_to_offset;

/// Cached AST data for a document.
#[cfg(feature = "lsp")]
#[derive(Debug, Default)]
pub struct CachedAst {
    /// Raw AST from Phase 1 parsing (with spans).
    pub raw: Option<RawWorkflow>,

    /// Analyzed AST from Phase 2 (validated, resolved).
    pub analyzed: Option<AnalyzedWorkflow>,

    /// Parse error (if Phase 1 failed).
    pub parse_error: Option<ParseError>,

    /// Analysis errors (for diagnostics).
    pub errors: Vec<AnalyzeError>,

    /// Document version when parsed.
    pub version: i32,

    /// The source text (needed for offset calculations).
    pub text: String,
}

/// AST index for efficient position-based lookups.
///
/// This is the core structure for Phase 2 LSP support, providing
/// cached AST access and position-to-node resolution.
#[cfg(feature = "lsp")]
pub struct AstIndex {
    /// Cached ASTs per document URI.
    cache: DashMap<Uri, CachedAst>,
}

#[cfg(feature = "lsp")]
impl Default for AstIndex {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "lsp")]
impl AstIndex {
    /// Create a new empty AST index.
    pub fn new() -> Self {
        Self {
            cache: DashMap::new(),
        }
    }

    /// Parse a document and cache the AST.
    ///
    /// This runs both Phase 1 (parse) and Phase 2 (analyze).
    /// Returns the list of analysis errors for diagnostic publishing.
    /// Parse errors are stored in the cache and can be retrieved via `get_parse_error`.
    pub fn parse_document(&self, uri: &Uri, text: &str, version: i32) -> Vec<AnalyzeError> {
        let file_id = FileId(0); // Single-file mode for now

        // Phase 1: Parse to Raw AST
        let (raw, analyzed, parse_error, errors) = match raw::parse(text, file_id) {
            Ok(raw_workflow) => {
                // Phase 2: Analyze
                let result = analyze(raw_workflow.clone());
                let analyzed = if result.is_ok() { result.value } else { None };
                (Some(raw_workflow), analyzed, None, result.errors)
            }
            Err(parse_err) => {
                // Parse failed, no AST available
                (None, None, Some(parse_err), Vec::new())
            }
        };

        self.cache.insert(
            uri.clone(),
            CachedAst {
                raw,
                analyzed,
                parse_error,
                errors: errors.clone(),
                version,
                text: text.to_string(),
            },
        );

        errors
    }

    /// Get the parse error for a document, if any.
    pub fn get_parse_error(&self, uri: &Uri) -> Option<ParseError> {
        self.cache.get(uri).and_then(|c| c.parse_error.clone())
    }

    /// Invalidate the cache for a document.
    pub fn invalidate(&self, uri: &Uri) {
        self.cache.remove(uri);
    }

    /// Get the cached AST for a document.
    pub fn get(&self, uri: &Uri) -> Option<dashmap::mapref::one::Ref<'_, Uri, CachedAst>> {
        self.cache.get(uri)
    }

    /// Check if a span contains a byte offset.
    fn span_contains_offset(span: &Span, offset: usize) -> bool {
        let start = span.start.as_usize();
        let end = span.end.as_usize();
        offset >= start && offset < end
    }

    /// Get the AST node at a given position.
    ///
    /// Returns the most specific node at the position.
    pub fn get_node_at_position(&self, uri: &Uri, position: Position) -> Option<AstNode> {
        let cached = self.cache.get(uri)?;

        // Convert LSP position to byte offset
        let offset = position_to_offset(position, &cached.text);

        // Check analyzed AST first (more semantic info)
        if let Some(ref analyzed) = cached.analyzed {
            // Check tasks
            for task in &analyzed.tasks {
                if Self::span_contains_offset(&task.span, offset) {
                    // Found the task, now check for more specific elements
                    if let Some(node) = self.get_node_in_task(task, offset) {
                        return Some(node);
                    }
                    return Some(AstNode::Task(task.name.clone(), task.span));
                }
            }

            // Check MCP servers
            for (name, server) in &analyzed.mcp_servers {
                if Self::span_contains_offset(&server.span, offset) {
                    return Some(AstNode::McpServer(name.clone(), server.span));
                }
            }
        }

        // Fall back to raw AST for parse-level elements
        if let Some(ref raw) = cached.raw {
            // Check schema
            if Self::span_contains_offset(&raw.schema.span, offset) {
                return Some(AstNode::Schema(raw.schema.value.clone(), raw.schema.span));
            }

            // Check workflow name
            if let Some(ref workflow) = raw.workflow {
                if Self::span_contains_offset(&workflow.span, offset) {
                    return Some(AstNode::Workflow(workflow.value.clone(), workflow.span));
                }
            }
        }

        None
    }

    /// Get a more specific node within a task.
    fn get_node_in_task(&self, task: &AnalyzedTask, offset: usize) -> Option<AstNode> {
        // Check the action span
        let action_span = match &task.action {
            AnalyzedTaskAction::Infer(a) => a.span,
            AnalyzedTaskAction::Exec(a) => a.span,
            AnalyzedTaskAction::Fetch(a) => a.span,
            AnalyzedTaskAction::Invoke(a) => a.span,
            AnalyzedTaskAction::Agent(a) => a.span,
        };

        if Self::span_contains_offset(&action_span, offset) {
            return Some(AstNode::Verb(
                task.action.verb_name().to_string(),
                action_span,
            ));
        }

        // with: bindings don't carry spans (they are parsed expressions),
        // so we can't do positional lookups into individual bindings.
        // The task span covers the entire with: block.

        // Check for_each
        if let Some(ref for_each) = task.for_each {
            if Self::span_contains_offset(&for_each.span, offset) {
                return Some(AstNode::ForEach(for_each.span));
            }
        }

        None
    }

    /// Get the task at a given position.
    pub fn get_task_at_position(&self, uri: &Uri, position: Position) -> Option<String> {
        match self.get_node_at_position(uri, position)? {
            AstNode::Task(name, _) => Some(name),
            AstNode::Verb(_, _) => {
                // Return the containing task
                let cached = self.cache.get(uri)?;
                let offset = position_to_offset(position, &cached.text);

                if let Some(ref analyzed) = cached.analyzed {
                    for task in &analyzed.tasks {
                        if Self::span_contains_offset(&task.span, offset) {
                            return Some(task.name.clone());
                        }
                    }
                }
                None
            }
            AstNode::Binding(_, _) | AstNode::ForEach(_) => {
                // Return the containing task
                let cached = self.cache.get(uri)?;
                let offset = position_to_offset(position, &cached.text);

                if let Some(ref analyzed) = cached.analyzed {
                    for task in &analyzed.tasks {
                        if Self::span_contains_offset(&task.span, offset) {
                            return Some(task.name.clone());
                        }
                    }
                }
                None
            }
            _ => None,
        }
    }

    /// Get all task names in the document.
    pub fn get_task_names(&self, uri: &Uri) -> Vec<String> {
        if let Some(cached) = self.cache.get(uri) {
            if let Some(ref analyzed) = cached.analyzed {
                return analyzed.tasks.iter().map(|t| t.name.clone()).collect();
            }
        }
        Vec::new()
    }

    /// Get all MCP server names in the document.
    pub fn get_mcp_server_names(&self, uri: &Uri) -> Vec<String> {
        if let Some(cached) = self.cache.get(uri) {
            if let Some(ref analyzed) = cached.analyzed {
                return analyzed.mcp_servers.keys().cloned().collect();
            }
        }
        Vec::new()
    }

    /// Get all context file names (aliases) in the document.
    pub fn get_context_file_names(&self, uri: &Uri) -> Vec<String> {
        if let Some(cached) = self.cache.get(uri) {
            if let Some(ref analyzed) = cached.analyzed {
                return analyzed
                    .context_files
                    .iter()
                    .filter_map(|cf| cf.alias.clone())
                    .collect();
            }
        }
        Vec::new()
    }
}

/// AST node types for position-based lookup.
///
/// Each variant carries the node's name/identifier and its span.
#[cfg(feature = "lsp")]
#[derive(Debug, Clone)]
pub enum AstNode {
    /// Schema declaration
    Schema(String, Span),

    /// Workflow name
    Workflow(String, Span),

    /// Task (id, span)
    Task(String, Span),

    /// Task verb (verb_name, span)
    Verb(String, Span),

    /// Use binding (alias, span)
    Binding(String, Span),

    /// For-each construct
    ForEach(Span),

    /// MCP server configuration (name, span)
    McpServer(String, Span),

    /// Context file
    ContextFile(String, Span),

    /// Include specification
    Include(String, Span),

    /// Template expression (e.g., {{with.alias}})
    Template(String, Span),

    /// Unknown node
    Unknown,
}

#[cfg(feature = "lsp")]
impl AstNode {
    /// Get the span of the node.
    pub fn span(&self) -> Option<Span> {
        match self {
            AstNode::Schema(_, span) => Some(*span),
            AstNode::Workflow(_, span) => Some(*span),
            AstNode::Task(_, span) => Some(*span),
            AstNode::Verb(_, span) => Some(*span),
            AstNode::Binding(_, span) => Some(*span),
            AstNode::ForEach(span) => Some(*span),
            AstNode::McpServer(_, span) => Some(*span),
            AstNode::ContextFile(_, span) => Some(*span),
            AstNode::Include(_, span) => Some(*span),
            AstNode::Template(_, span) => Some(*span),
            AstNode::Unknown => None,
        }
    }

    /// Get the name/identifier of the node.
    pub fn name(&self) -> Option<&str> {
        match self {
            AstNode::Schema(name, _) => Some(name),
            AstNode::Workflow(name, _) => Some(name),
            AstNode::Task(name, _) => Some(name),
            AstNode::Verb(name, _) => Some(name),
            AstNode::Binding(name, _) => Some(name),
            AstNode::ForEach(_) => Some("for_each"),
            AstNode::McpServer(name, _) => Some(name),
            AstNode::ContextFile(name, _) => Some(name),
            AstNode::Include(name, _) => Some(name),
            AstNode::Template(expr, _) => Some(expr),
            AstNode::Unknown => None,
        }
    }
}

#[cfg(test)]
mod tests {
    #[cfg(feature = "lsp")]
    use super::*;

    #[test]
    #[cfg(feature = "lsp")]
    fn test_ast_index_creation() {
        let index = AstIndex::new();
        assert!(index.cache.is_empty());
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_ast_index_parse_simple_workflow() {
        let index = AstIndex::new();
        let uri = "file:///test.nika.yaml".parse::<Uri>().unwrap();
        let text = r#"schema: nika/workflow@0.12
workflow: test

tasks:
  - id: step1
    infer: "Hello"
"#;

        let errors = index.parse_document(&uri, text, 1);
        assert!(errors.is_empty(), "Parse errors: {:?}", errors);

        let cached = index.get(&uri).expect("Should have cached AST");
        assert!(cached.raw.is_some());
        assert!(cached.analyzed.is_some());
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_ast_index_get_task_names() {
        let index = AstIndex::new();
        let uri = "file:///test.nika.yaml".parse::<Uri>().unwrap();
        let text = r#"schema: nika/workflow@0.12
workflow: test

tasks:
  - id: step1
    infer: "Hello"
  - id: step2
    exec: "echo hello"
"#;

        index.parse_document(&uri, text, 1);
        let names = index.get_task_names(&uri);
        assert_eq!(names.len(), 2);
        assert!(names.contains(&"step1".to_string()));
        assert!(names.contains(&"step2".to_string()));
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_ast_index_invalidate() {
        let index = AstIndex::new();
        let uri = "file:///test.nika.yaml".parse::<Uri>().unwrap();
        let text = "schema: nika/workflow@0.12\n";

        index.parse_document(&uri, text, 1);
        assert!(index.get(&uri).is_some());

        index.invalidate(&uri);
        assert!(index.get(&uri).is_none());
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_ast_node_span() {
        use crate::source::FileId;

        let span = Span::new(FileId(0), 10, 20);
        let node = AstNode::Task("test".to_string(), span);
        assert_eq!(node.span(), Some(span));
        assert_eq!(node.name(), Some("test"));
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_span_contains_offset() {
        use crate::source::FileId;

        let span = Span::new(FileId(0), 10, 20);
        assert!(!AstIndex::span_contains_offset(&span, 5));
        assert!(AstIndex::span_contains_offset(&span, 10));
        assert!(AstIndex::span_contains_offset(&span, 15));
        assert!(!AstIndex::span_contains_offset(&span, 20));
        assert!(!AstIndex::span_contains_offset(&span, 25));
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_get_node_at_position_schema() {
        let index = AstIndex::new();
        let uri = "file:///test.nika.yaml".parse::<Uri>().unwrap();
        let text = r#"schema: nika/workflow@0.12
workflow: test

tasks:
  - id: step1
    infer: "Hello"
"#;

        index.parse_document(&uri, text, 1);

        // Verify parsing succeeded
        let cached = index.get(&uri).expect("Should have cached AST");
        assert!(cached.raw.is_some(), "Should have raw AST");
        assert!(cached.analyzed.is_some(), "Should have analyzed AST");

        // Check schema value was parsed correctly
        let schema_value = &cached.raw.as_ref().unwrap().schema.value;
        assert_eq!(schema_value, "nika/workflow@0.12");

        // Note: Position-based lookup for schema may fail due to degenerate spans
        // in marked_yaml (start == end for scalars). This is a known limitation.
        // The important thing is that the AST is correctly parsed and can be queried.
    }

    #[test]
    #[cfg(feature = "lsp")]
    fn test_get_node_at_position_task() {
        let index = AstIndex::new();
        let uri = "file:///test.nika.yaml".parse::<Uri>().unwrap();
        let text = r#"schema: nika/workflow@0.12
workflow: test

tasks:
  - id: step1
    infer: "Hello"
"#;

        index.parse_document(&uri, text, 1);

        // Position at "id: step1" (line 4, col 5)
        let node = index.get_node_at_position(
            &uri,
            Position {
                line: 4,
                character: 5,
            },
        );
        // Note: This test might need adjustment based on actual span positions
        // The spans come from marked_yaml which tracks exact positions
        if let Some(node) = node {
            match node {
                AstNode::Task(name, _) => assert_eq!(name, "step1"),
                other => {
                    // Task spans might not include "id:" prefix, check for verb instead
                    println!("Got node: {:?}", other);
                }
            }
        }
    }
}