nika 0.20.0

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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
//! Grep Tool - Search file contents with regex
//!
//! Fast content search with:
//! - Full regex support
//! - Multiple output modes (content, files, count)
//! - Context lines (-B/-A/-C)
//! - File type filtering

use std::sync::Arc;

use async_trait::async_trait;
use ignore::WalkBuilder;
use regex::RegexBuilder;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use tokio::fs;

use super::context::{ToolContext, ToolEvent};
use super::{FileTool, ToolErrorCode, ToolOutput};
use crate::error::NikaError;

// ═══════════════════════════════════════════════════════════════════════════
// PARAMETERS & RESULT
// ═══════════════════════════════════════════════════════════════════════════

/// Output mode for grep results
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum GrepOutputMode {
    /// Show matching lines with content
    Content,
    /// Show only file paths (default)
    #[default]
    FilesWithMatches,
    /// Show match count per file
    Count,
}

/// Parameters for the Grep tool
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GrepParams {
    /// Regex pattern to search for
    pub pattern: String,

    /// Base path to search in (default: working directory)
    #[serde(default)]
    pub path: Option<String>,

    /// Glob pattern to filter files (e.g., "*.rs")
    #[serde(default)]
    pub glob: Option<String>,

    /// Output mode
    #[serde(default)]
    pub output_mode: GrepOutputMode,

    /// Case-insensitive search
    #[serde(default)]
    pub case_insensitive: bool,

    /// Lines of context before match
    #[serde(default, rename = "context_before")]
    pub context_before: Option<usize>,

    /// Lines of context after match
    #[serde(default, rename = "context_after")]
    pub context_after: Option<usize>,

    /// Lines of context before and after
    #[serde(default, rename = "context")]
    pub context: Option<usize>,

    /// Limit results
    #[serde(default)]
    pub limit: Option<usize>,
}

/// A single match in a file
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GrepMatch {
    /// File path
    pub file: String,
    /// Line number (1-indexed)
    pub line_number: usize,
    /// Line content
    pub content: String,
    /// Context lines before (if requested)
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub context_before: Vec<String>,
    /// Context lines after (if requested)
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub context_after: Vec<String>,
}

/// Result from grep search
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GrepResult {
    /// Total matches found
    pub total_matches: usize,

    /// Files searched
    pub files_searched: usize,

    /// Files with matches
    pub files_with_matches: usize,

    /// Matches (for Content mode) or file paths (for FilesWithMatches mode)
    pub matches: Vec<GrepMatch>,

    /// Match counts per file (for Count mode)
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub counts: Vec<(String, usize)>,
}

// ═══════════════════════════════════════════════════════════════════════════
// GREP TOOL
// ═══════════════════════════════════════════════════════════════════════════

/// Grep tool for searching file contents
///
/// # Features
///
/// - Full regex support via `regex` crate
/// - Multiple output modes
/// - Context lines for surrounding content
/// - File type filtering via glob
/// - Respects .gitignore
pub struct GrepTool {
    ctx: Arc<ToolContext>,
}

impl GrepTool {
    /// Maximum files to search
    pub const MAX_FILES: usize = 10000;

    /// Maximum matches to return
    pub const MAX_MATCHES: usize = 1000;

    /// Create a new Grep tool
    pub fn new(ctx: Arc<ToolContext>) -> Self {
        Self { ctx }
    }

    /// Execute the grep search
    pub async fn execute(&self, params: GrepParams) -> Result<GrepResult, NikaError> {
        // Determine base path
        let base_path = match params.path {
            Some(ref p) => self.ctx.validate_path(p)?,
            None => self.ctx.working_dir().to_path_buf(),
        };

        // Build regex
        let regex = RegexBuilder::new(&params.pattern)
            .case_insensitive(params.case_insensitive)
            .multi_line(true)
            .build()
            .map_err(|e| NikaError::ToolError {
                code: ToolErrorCode::InvalidRegex.code(),
                message: format!("Invalid regex pattern '{}': {}", params.pattern, e),
            })?;

        // Build glob filter if provided
        let glob_filter = if let Some(ref glob_pattern) = params.glob {
            Some(
                globset::GlobBuilder::new(glob_pattern)
                    .literal_separator(true)
                    .build()
                    .map_err(|e| NikaError::ToolError {
                        code: ToolErrorCode::InvalidGlobPattern.code(),
                        message: format!("Invalid glob pattern '{}': {}", glob_pattern, e),
                    })?
                    .compile_matcher(),
            )
        } else {
            None
        };

        // Context lines
        let context_before = params.context_before.or(params.context).unwrap_or(0);
        let context_after = params.context_after.or(params.context).unwrap_or(0);

        // Walk files and search
        let mut matches: Vec<GrepMatch> = Vec::new();
        let mut counts: Vec<(String, usize)> = Vec::new();
        let mut files_searched = 0;
        let mut files_with_matches = 0;
        let mut total_matches = 0;

        let limit = params.limit.unwrap_or(Self::MAX_MATCHES);

        let walker = WalkBuilder::new(&base_path)
            .hidden(false)
            .git_ignore(true)
            .git_global(true)
            .git_exclude(true)
            .build();

        for entry in walker.filter_map(Result::ok) {
            let path = entry.path();

            // Skip directories
            if path.is_dir() {
                continue;
            }

            // Apply glob filter
            if let Some(ref glob) = glob_filter {
                let relative = path.strip_prefix(&base_path).unwrap_or(path);
                if !glob.is_match(relative) && !glob.is_match(path) {
                    continue;
                }
            }

            // Read file
            let content = match fs::read_to_string(path).await {
                Ok(c) => c,
                Err(_) => continue, // Skip unreadable files
            };

            files_searched += 1;

            if files_searched > Self::MAX_FILES {
                break;
            }

            // Search for matches
            let lines: Vec<&str> = content.lines().collect();
            let mut file_matches = 0;

            for (line_idx, line) in lines.iter().enumerate() {
                if regex.is_match(line) {
                    file_matches += 1;
                    total_matches += 1;

                    if total_matches > limit {
                        continue; // Still count but don't store
                    }

                    // Build context
                    let ctx_before: Vec<String> = if context_before > 0 {
                        let start = line_idx.saturating_sub(context_before);
                        lines[start..line_idx]
                            .iter()
                            .map(|s| s.to_string())
                            .collect()
                    } else {
                        Vec::new()
                    };

                    let ctx_after: Vec<String> = if context_after > 0 {
                        let end = (line_idx + 1 + context_after).min(lines.len());
                        lines[line_idx + 1..end]
                            .iter()
                            .map(|s| s.to_string())
                            .collect()
                    } else {
                        Vec::new()
                    };

                    matches.push(GrepMatch {
                        file: path.to_string_lossy().to_string(),
                        line_number: line_idx + 1,
                        content: line.to_string(),
                        context_before: ctx_before,
                        context_after: ctx_after,
                    });
                }
            }

            if file_matches > 0 {
                files_with_matches += 1;
                counts.push((path.to_string_lossy().to_string(), file_matches));
            }
        }

        // Emit event
        self.ctx
            .emit(ToolEvent::GrepSearch {
                pattern: params.pattern,
                files_searched,
                matches: total_matches,
            })
            .await;

        Ok(GrepResult {
            total_matches,
            files_searched,
            files_with_matches,
            matches,
            counts,
        })
    }

    /// Format output based on mode
    fn format_output(&self, result: &GrepResult, mode: GrepOutputMode) -> String {
        match mode {
            GrepOutputMode::Content => {
                if result.matches.is_empty() {
                    return "No matches found".to_string();
                }

                result
                    .matches
                    .iter()
                    .map(|m| {
                        let mut output = String::new();

                        // Context before
                        for (i, ctx) in m.context_before.iter().enumerate() {
                            let line_num = m.line_number - m.context_before.len() + i;
                            output.push_str(&format!("{}:{}: {}\n", m.file, line_num, ctx));
                        }

                        // Matching line
                        output.push_str(&format!("{}:{}> {}\n", m.file, m.line_number, m.content));

                        // Context after
                        for (i, ctx) in m.context_after.iter().enumerate() {
                            let line_num = m.line_number + 1 + i;
                            output.push_str(&format!("{}:{}: {}\n", m.file, line_num, ctx));
                        }

                        output
                    })
                    .collect::<Vec<_>>()
                    .join("--\n")
            }
            GrepOutputMode::FilesWithMatches => {
                if result.files_with_matches == 0 {
                    return "No matching files found".to_string();
                }

                // Deduplicate file paths
                let mut files: Vec<&str> = result.matches.iter().map(|m| m.file.as_str()).collect();
                files.sort();
                files.dedup();

                format!("Found {} files:\n{}", files.len(), files.join("\n"))
            }
            GrepOutputMode::Count => {
                if result.counts.is_empty() {
                    return "No matches found".to_string();
                }

                let counts_str = result
                    .counts
                    .iter()
                    .map(|(file, count)| format!("{}: {}", file, count))
                    .collect::<Vec<_>>()
                    .join("\n");

                format!(
                    "Total: {} matches in {} files\n{}",
                    result.total_matches, result.files_with_matches, counts_str
                )
            }
        }
    }
}

#[async_trait]
impl FileTool for GrepTool {
    fn name(&self) -> &'static str {
        "grep"
    }

    fn description(&self) -> &'static str {
        "Search file contents with regex patterns. Supports multiple output modes: \
         'content' shows matching lines, 'files_with_matches' shows file paths, \
         'count' shows match counts. Use context_before/context_after for surrounding lines. \
         Use glob parameter to filter by file pattern."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "pattern": {
                    "type": "string",
                    "description": "Regex pattern to search for"
                },
                "path": {
                    "type": "string",
                    "description": "Base path to search in (default: working directory)"
                },
                "glob": {
                    "type": "string",
                    "description": "Glob pattern to filter files (e.g., '*.rs', '**/*.ts')"
                },
                "output_mode": {
                    "type": "string",
                    "enum": ["content", "files_with_matches", "count"],
                    "description": "Output format (default: files_with_matches)"
                },
                "case_insensitive": {
                    "type": "boolean",
                    "description": "Case-insensitive search (default: false)"
                },
                "context_before": {
                    "type": "integer",
                    "description": "Lines of context before match"
                },
                "context_after": {
                    "type": "integer",
                    "description": "Lines of context after match"
                },
                "context": {
                    "type": "integer",
                    "description": "Lines of context before and after (shorthand)"
                },
                "limit": {
                    "type": "integer",
                    "description": "Maximum matches to return"
                }
            },
            "required": ["pattern"]
        })
    }

    async fn call(&self, params: Value) -> Result<ToolOutput, NikaError> {
        let params: GrepParams =
            serde_json::from_value(params.clone()).map_err(|e| NikaError::ToolError {
                code: ToolErrorCode::InvalidRegex.code(),
                message: format!("Invalid parameters: {}", e),
            })?;

        let output_mode = params.output_mode;
        let result = self.execute(params).await?;
        let content = self.format_output(&result, output_mode);

        Ok(ToolOutput::success_with_data(
            content,
            serde_json::to_value(&result).unwrap_or_default(),
        ))
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// TESTS
// ═══════════════════════════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    async fn setup_test() -> (TempDir, Arc<ToolContext>) {
        let temp_dir = TempDir::new().unwrap();
        let ctx = Arc::new(ToolContext::new(
            temp_dir.path().to_path_buf(),
            super::super::context::PermissionMode::YoloMode,
        ));
        (temp_dir, ctx)
    }

    async fn create_test_files(temp_dir: &TempDir) {
        fs::create_dir_all(temp_dir.path().join("src"))
            .await
            .unwrap();

        fs::write(
            temp_dir.path().join("src/main.rs"),
            "fn main() {\n    println!(\"Hello\");\n    println!(\"World\");\n}",
        )
        .await
        .unwrap();

        fs::write(
            temp_dir.path().join("src/lib.rs"),
            "pub fn hello() {\n    // Hello function\n}",
        )
        .await
        .unwrap();

        fs::write(
            temp_dir.path().join("README.md"),
            "# Hello World\n\nThis is a test.",
        )
        .await
        .unwrap();
    }

    #[tokio::test]
    async fn test_grep_simple_pattern() {
        let (temp_dir, ctx) = setup_test().await;
        create_test_files(&temp_dir).await;

        let tool = GrepTool::new(ctx);
        let result = tool
            .execute(GrepParams {
                pattern: "Hello".to_string(),
                path: None,
                glob: None,
                output_mode: GrepOutputMode::Content,
                case_insensitive: false,
                context_before: None,
                context_after: None,
                context: None,
                limit: None,
            })
            .await
            .unwrap();

        assert!(result.total_matches >= 2);
        assert!(result.files_with_matches >= 2);
    }

    #[tokio::test]
    async fn test_grep_with_glob_filter() {
        let (temp_dir, ctx) = setup_test().await;
        create_test_files(&temp_dir).await;

        let tool = GrepTool::new(ctx);
        let result = tool
            .execute(GrepParams {
                pattern: "fn".to_string(),
                path: None,
                glob: Some("**/*.rs".to_string()),
                output_mode: GrepOutputMode::FilesWithMatches,
                case_insensitive: false,
                context_before: None,
                context_after: None,
                context: None,
                limit: None,
            })
            .await
            .unwrap();

        assert_eq!(result.files_with_matches, 2);
        assert!(result.matches.iter().all(|m| m.file.ends_with(".rs")));
    }

    #[tokio::test]
    async fn test_grep_case_insensitive() {
        let (temp_dir, ctx) = setup_test().await;
        create_test_files(&temp_dir).await;

        let tool = GrepTool::new(ctx);
        let result = tool
            .execute(GrepParams {
                pattern: "hello".to_string(),
                path: None,
                glob: None,
                output_mode: GrepOutputMode::Content,
                case_insensitive: true,
                context_before: None,
                context_after: None,
                context: None,
                limit: None,
            })
            .await
            .unwrap();

        // Should match "Hello" in multiple files
        assert!(result.total_matches >= 2);
    }

    #[tokio::test]
    async fn test_grep_with_context() {
        let (temp_dir, ctx) = setup_test().await;
        create_test_files(&temp_dir).await;

        let tool = GrepTool::new(ctx);
        let result = tool
            .execute(GrepParams {
                pattern: "println".to_string(),
                path: None,
                glob: Some("*.rs".to_string()),
                output_mode: GrepOutputMode::Content,
                case_insensitive: false,
                context_before: Some(1),
                context_after: Some(1),
                context: None,
                limit: None,
            })
            .await
            .unwrap();

        // Matches should have context
        for m in &result.matches {
            // Depending on position, there should be context
            assert!(m.context_before.len() <= 1);
            assert!(m.context_after.len() <= 1);
        }
    }

    #[tokio::test]
    async fn test_grep_count_mode() {
        let (temp_dir, ctx) = setup_test().await;
        create_test_files(&temp_dir).await;

        let tool = GrepTool::new(ctx);
        let result = tool
            .execute(GrepParams {
                pattern: "println".to_string(),
                path: None,
                glob: None,
                output_mode: GrepOutputMode::Count,
                case_insensitive: false,
                context_before: None,
                context_after: None,
                context: None,
                limit: None,
            })
            .await
            .unwrap();

        // main.rs has 2 println calls
        assert!(result
            .counts
            .iter()
            .any(|(f, c)| f.contains("main.rs") && *c == 2));
    }

    #[tokio::test]
    async fn test_grep_invalid_regex() {
        let (_temp_dir, ctx) = setup_test().await;

        let tool = GrepTool::new(ctx);
        let result = tool
            .execute(GrepParams {
                pattern: "[invalid".to_string(),
                path: None,
                glob: None,
                output_mode: GrepOutputMode::Content,
                case_insensitive: false,
                context_before: None,
                context_after: None,
                context: None,
                limit: None,
            })
            .await;

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Invalid regex"));
    }

    #[tokio::test]
    async fn test_file_tool_trait() {
        let (temp_dir, ctx) = setup_test().await;
        create_test_files(&temp_dir).await;

        let tool = GrepTool::new(ctx);

        assert_eq!(tool.name(), "grep");
        assert!(tool.description().contains("Search file contents"));

        let result = tool
            .call(json!({
                "pattern": "fn",
                "glob": "**/*.rs"
            }))
            .await
            .unwrap();

        assert!(!result.is_error);
        assert!(result.content.contains("Found"));
    }
}