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
//! Read Tool - Read files with line numbers
//!
//! Provides Claude Code-like file reading with:
//! - Line number prefix (cat -n style)
//! - Offset and limit for large files
//! - Automatic read tracking for edit validation

use std::sync::Arc;

use async_trait::async_trait;
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
// ═══════════════════════════════════════════════════════════════════════════

/// Parameters for the Read tool
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReadParams {
    /// Absolute path to the file to read
    pub file_path: String,

    /// Line offset (1-indexed, skip first N-1 lines)
    #[serde(default)]
    pub offset: Option<usize>,

    /// Maximum lines to read (default: 2000)
    #[serde(default)]
    pub limit: Option<usize>,
}

/// Result from reading a file
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReadResult {
    /// File content with line numbers
    pub content: String,

    /// Total lines in file
    pub total_lines: usize,

    /// Lines actually returned
    pub lines_returned: usize,

    /// Whether output was truncated
    pub truncated: bool,
}

// ═══════════════════════════════════════════════════════════════════════════
// READ TOOL
// ═══════════════════════════════════════════════════════════════════════════

/// Read tool for reading files with line numbers
///
/// # Features
///
/// - Line numbers in output (cat -n style)
/// - Offset/limit for large files
/// - Tracks reads for edit validation
/// - Max line length truncation (2000 chars)
pub struct ReadTool {
    ctx: Arc<ToolContext>,
}

impl ReadTool {
    /// Default maximum lines to read
    pub const DEFAULT_LIMIT: usize = 2000;

    /// Maximum characters per line before truncation
    pub const MAX_LINE_LENGTH: usize = 2000;

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

    /// Execute the read operation
    pub async fn execute(&self, params: ReadParams) -> Result<ReadResult, NikaError> {
        // Validate path
        let path = self.ctx.validate_path(&params.file_path)?;

        // Check permission (reads are usually allowed, but respect Deny mode)
        if self.ctx.permission_mode() == super::context::PermissionMode::Deny {
            return Err(NikaError::ToolError {
                code: ToolErrorCode::PermissionDenied.code(),
                message: "Read operations are denied in current permission mode".to_string(),
            });
        }

        // Check file exists
        if !path.exists() {
            return Err(NikaError::ToolError {
                code: ToolErrorCode::FileNotFound.code(),
                message: format!("File not found: {}", params.file_path),
            });
        }

        // Read file content
        let content = fs::read_to_string(&path)
            .await
            .map_err(|e| NikaError::ToolError {
                code: ToolErrorCode::ReadFailed.code(),
                message: format!("Failed to read file: {}", e),
            })?;

        // Process lines
        let all_lines: Vec<&str> = content.lines().collect();
        let total_lines = all_lines.len();

        // Apply offset (1-indexed)
        let offset = params.offset.unwrap_or(1).saturating_sub(1);
        let limit = params.limit.unwrap_or(Self::DEFAULT_LIMIT);

        let selected_lines: Vec<&str> = all_lines.into_iter().skip(offset).take(limit).collect();

        let lines_returned = selected_lines.len();
        let truncated = offset + lines_returned < total_lines;

        // Format with line numbers (cat -n style)
        // Format: "    N\tline content"
        let formatted = selected_lines
            .iter()
            .enumerate()
            .map(|(i, line)| {
                let line_num = offset + i + 1;
                let truncated_line = if line.len() > Self::MAX_LINE_LENGTH {
                    format!("{}...", &line[..Self::MAX_LINE_LENGTH])
                } else {
                    line.to_string()
                };
                format!("{:>6}\t{}", line_num, truncated_line)
            })
            .collect::<Vec<_>>()
            .join("\n");

        // Mark as read for edit validation
        self.ctx.mark_as_read(&path);

        // Emit event
        self.ctx
            .emit(ToolEvent::FileRead {
                path: params.file_path,
                lines: lines_returned,
                truncated,
            })
            .await;

        Ok(ReadResult {
            content: formatted,
            total_lines,
            lines_returned,
            truncated,
        })
    }
}

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

    fn description(&self) -> &'static str {
        "Read a file from the filesystem. Returns content with line numbers. \
         Use offset and limit for large files. Must use absolute paths within \
         the working directory."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "file_path": {
                    "type": "string",
                    "description": "Absolute path to the file to read"
                },
                "offset": {
                    "type": "integer",
                    "description": "Line number to start reading from (1-indexed)",
                    "minimum": 1
                },
                "limit": {
                    "type": "integer",
                    "description": "Maximum number of lines to read (default: 2000)",
                    "minimum": 1,
                    "maximum": 10000
                }
            },
            "required": ["file_path"]
        })
    }

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

        let result = self.execute(params).await?;

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

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

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;
    use tokio::fs::File;
    use tokio::io::AsyncWriteExt;

    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_file(dir: &TempDir, name: &str, content: &str) -> String {
        let path = dir.path().join(name);
        let mut file = File::create(&path).await.unwrap();
        file.write_all(content.as_bytes()).await.unwrap();
        file.flush().await.unwrap(); // Ensure data is written before returning
        path.to_string_lossy().to_string()
    }

    #[tokio::test]
    async fn test_read_simple_file() {
        let (temp_dir, ctx) = setup_test().await;
        let file_path = create_test_file(&temp_dir, "test.txt", "line 1\nline 2\nline 3").await;

        let tool = ReadTool::new(ctx);
        let result = tool
            .execute(ReadParams {
                file_path,
                offset: None,
                limit: None,
            })
            .await
            .unwrap();

        assert_eq!(result.total_lines, 3);
        assert_eq!(result.lines_returned, 3);
        assert!(!result.truncated);
        assert!(result.content.contains("line 1"));
        assert!(result.content.contains("line 3"));
    }

    #[tokio::test]
    async fn test_read_with_offset() {
        let (temp_dir, ctx) = setup_test().await;
        let content = (1..=10)
            .map(|i| format!("line {}", i))
            .collect::<Vec<_>>()
            .join("\n");
        let file_path = create_test_file(&temp_dir, "test.txt", &content).await;

        let tool = ReadTool::new(ctx);
        let result = tool
            .execute(ReadParams {
                file_path,
                offset: Some(5),
                limit: Some(3),
            })
            .await
            .unwrap();

        assert_eq!(result.total_lines, 10);
        assert_eq!(result.lines_returned, 3);
        assert!(result.truncated);
        assert!(result.content.contains("line 5"));
        assert!(result.content.contains("line 7"));
        assert!(!result.content.contains("line 4"));
    }

    #[tokio::test]
    async fn test_read_line_numbers_format() {
        let (temp_dir, ctx) = setup_test().await;
        let file_path = create_test_file(&temp_dir, "test.txt", "hello\nworld").await;

        let tool = ReadTool::new(ctx);
        let result = tool
            .execute(ReadParams {
                file_path,
                offset: None,
                limit: None,
            })
            .await
            .unwrap();

        // Check line number format (right-aligned with tab)
        assert!(result.content.contains("     1\thello"));
        assert!(result.content.contains("     2\tworld"));
    }

    #[tokio::test]
    async fn test_read_marks_file_as_read() {
        let (temp_dir, ctx) = setup_test().await;
        let file_path = create_test_file(&temp_dir, "test.txt", "content").await;
        let path = std::path::PathBuf::from(&file_path);

        assert!(!ctx.was_read(&path));

        let tool = ReadTool::new(ctx.clone());
        tool.execute(ReadParams {
            file_path,
            offset: None,
            limit: None,
        })
        .await
        .unwrap();

        assert!(ctx.was_read(&path));
    }

    #[tokio::test]
    async fn test_read_file_not_found() {
        let (temp_dir, ctx) = setup_test().await;
        let file_path = temp_dir
            .path()
            .join("nonexistent.txt")
            .to_string_lossy()
            .to_string();

        let tool = ReadTool::new(ctx);
        let result = tool
            .execute(ReadParams {
                file_path,
                offset: None,
                limit: None,
            })
            .await;

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));
    }

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

        let tool = ReadTool::new(ctx);
        let result = tool
            .execute(ReadParams {
                file_path: "/etc/passwd".to_string(),
                offset: None,
                limit: None,
            })
            .await;

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

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

        let tool = ReadTool::new(ctx);
        let result = tool
            .execute(ReadParams {
                file_path: "relative/path.txt".to_string(),
                offset: None,
                limit: None,
            })
            .await;

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

    #[tokio::test]
    async fn test_file_tool_trait() {
        let (temp_dir, ctx) = setup_test().await;
        let file_path = create_test_file(&temp_dir, "test.txt", "content").await;

        let tool = ReadTool::new(ctx);

        assert_eq!(tool.name(), "read");
        assert!(tool.description().contains("Read a file"));

        let schema = tool.parameters_schema();
        assert!(schema["properties"]["file_path"].is_object());

        let result = tool.call(json!({ "file_path": file_path })).await.unwrap();

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