pawan-core 0.5.6

Pawan (पवन) — Core library: agent, tools, config, healing
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
//! ast-grep and LSP (rust-analyzer) tool wrappers.

use super::native_search::{ensure_binary, run_cmd};
use super::Tool;
use async_trait::async_trait;
use serde_json::{json, Value};
use std::path::PathBuf;
use std::process::Stdio;

// ─── ast-grep ────────────────────────────────────────────────────────────────

pub struct AstGrepTool {
    workspace_root: PathBuf,
}

impl AstGrepTool {
    pub fn new(workspace_root: PathBuf) -> Self {
        Self { workspace_root }
    }
}

#[async_trait]
impl Tool for AstGrepTool {
    fn name(&self) -> &str {
        "ast_grep"
    }

    fn description(&self) -> &str {
        "ast-grep — structural code search and rewrite using AST patterns. \
         Unlike regex, this matches code by syntax tree structure. Use $NAME for \
         single-node wildcards, $$$ARGS for variadic (multiple nodes). \
         Actions: 'search' finds matches, 'rewrite' transforms them in-place. \
         Examples: pattern='fn $NAME($$$ARGS)' finds all functions. \
         pattern='$EXPR.unwrap()' rewrite='$EXPR?' replaces unwrap with ?. \
         Supports: rust, python, javascript, typescript, go, c, cpp, java."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "enum": ["search", "rewrite"],
                    "description": "search: find matching code. rewrite: transform matching code in-place."
                },
                "pattern": {
                    "type": "string",
                    "description": "AST pattern to match. Use $VAR for wildcards, $$$VAR for variadic. e.g. 'fn $NAME($$$ARGS) -> $RET { $$$ }'"
                },
                "rewrite": {
                    "type": "string",
                    "description": "Replacement pattern (only for action=rewrite). Use captured $VARs. e.g. '$EXPR?'"
                },
                "path": {
                    "type": "string",
                    "description": "File or directory to search/rewrite"
                },
                "lang": {
                    "type": "string",
                    "description": "Language: rust, python, javascript, typescript, go, c, cpp, java (default: auto-detect)"
                }
            },
            "required": ["action", "pattern", "path"]
        })
    }

    fn thulp_definition(&self) -> thulp_core::ToolDefinition {
        use thulp_core::{Parameter, ParameterType};
        thulp_core::ToolDefinition::builder(self.name())
            .description(self.description())
            .parameter(
                Parameter::builder("action")
                    .param_type(ParameterType::String)
                    .required(true)
                    .description("search: find matching code. rewrite: transform matching code in-place.")
                    .build(),
            )
            .parameter(
                Parameter::builder("pattern")
                    .param_type(ParameterType::String)
                    .required(true)
                    .description("AST pattern to match. Use $VAR for wildcards, $$$VAR for variadic. e.g. 'fn $NAME($$$ARGS) -> $RET { $$$ }'")
                    .build(),
            )
            .parameter(
                Parameter::builder("rewrite")
                    .param_type(ParameterType::String)
                    .required(false)
                    .description("Replacement pattern (only for action=rewrite). Use captured $VARs. e.g. '$EXPR?'")
                    .build(),
            )
            .parameter(
                Parameter::builder("path")
                    .param_type(ParameterType::String)
                    .required(true)
                    .description("File or directory to search/rewrite")
                    .build(),
            )
            .parameter(
                Parameter::builder("lang")
                    .param_type(ParameterType::String)
                    .required(false)
                    .description("Language: rust, python, javascript, typescript, go, c, cpp, java (default: auto-detect)")
                    .build(),
            )
            .build()
    }

    async fn execute(&self, args: Value) -> crate::Result<Value> {
        ensure_binary("ast-grep", &self.workspace_root).await?;

        let action = args["action"]
            .as_str()
            .ok_or_else(|| crate::PawanError::Tool("action required (search or rewrite)".into()))?;
        let pattern = args["pattern"]
            .as_str()
            .ok_or_else(|| crate::PawanError::Tool("pattern required".into()))?;
        let path = args["path"]
            .as_str()
            .ok_or_else(|| crate::PawanError::Tool("path required".into()))?;

        let mut cmd_args: Vec<String> = vec!["run".into()];

        if let Some(lang) = args["lang"].as_str() {
            cmd_args.push("--lang".into());
            cmd_args.push(lang.into());
        }

        cmd_args.push("--pattern".into());
        cmd_args.push(pattern.into());

        match action {
            "search" => {
                cmd_args.push(path.into());
            }
            "rewrite" => {
                let rewrite = args["rewrite"].as_str().ok_or_else(|| {
                    crate::PawanError::Tool("rewrite pattern required for action=rewrite".into())
                })?;
                cmd_args.push("--rewrite".into());
                cmd_args.push(rewrite.into());
                cmd_args.push("--update-all".into());
                cmd_args.push(path.into());
            }
            _ => {
                return Err(crate::PawanError::Tool(format!(
                    "Unknown action: {}. Use 'search' or 'rewrite'",
                    action
                )));
            }
        }

        let cmd_refs: Vec<&str> = cmd_args.iter().map(|s| s.as_str()).collect();
        let (stdout, stderr, success) = run_cmd("ast-grep", &cmd_refs, &self.workspace_root)
            .await
            .map_err(crate::PawanError::Tool)?;

        let match_count = stdout
            .lines()
            .filter(|l| l.starts_with('/') || l.contains(""))
            .count();

        Ok(json!({
            "success": success,
            "action": action,
            "matches": match_count,
            "output": stdout,
            "stderr": if stderr.is_empty() { None::<String> } else { Some(stderr) }
        }))
    }
}

// ─── LSP (rust-analyzer powered code intelligence) ──────────────────────────

pub struct LspTool {
    workspace_root: PathBuf,
}

impl LspTool {
    pub fn new(workspace_root: PathBuf) -> Self {
        Self { workspace_root }
    }
}

#[async_trait]
impl Tool for LspTool {
    fn name(&self) -> &str {
        "lsp"
    }

    fn description(&self) -> &str {
        "LSP code intelligence via rust-analyzer. Provides type-aware code understanding \
         that grep/ast-grep can't: diagnostics without cargo check, structural search with \
         type info, symbol extraction, and analysis stats. Actions: diagnostics (find errors), \
         search (structural pattern search), ssr (structural search+replace with types), \
         symbols (parse file symbols), analyze (project-wide type stats)."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "enum": ["diagnostics", "search", "ssr", "symbols", "analyze"],
                    "description": "diagnostics: find errors/warnings in project. \
                                    search: structural pattern search (e.g. '$a.foo($b)'). \
                                    ssr: structural search+replace (e.g. '$a.unwrap() ==>> $a?'). \
                                    symbols: parse file and list symbols. \
                                    analyze: project-wide type analysis stats."
                },
                "pattern": {
                    "type": "string",
                    "description": "For search: pattern like '$a.foo($b)'. For ssr: rule like '$a.unwrap() ==>> $a?'"
                },
                "path": {
                    "type": "string",
                    "description": "Project path (directory with Cargo.toml) for diagnostics/analyze. File path for symbols."
                },
                "severity": {
                    "type": "string",
                    "enum": ["error", "warning", "info", "hint"],
                    "description": "Minimum severity for diagnostics (default: warning)"
                }
            },
            "required": ["action"]
        })
    }

    fn thulp_definition(&self) -> thulp_core::ToolDefinition {
        use thulp_core::{Parameter, ParameterType};
        thulp_core::ToolDefinition::builder(self.name())
            .description(self.description())
            .parameter(
                Parameter::builder("action")
                    .param_type(ParameterType::String)
                    .required(true)
                    .description("diagnostics: find errors/warnings in project. \
                                  search: structural pattern search (e.g. '$a.foo($b)'). \
                                  ssr: structural search+replace (e.g. '$a.unwrap() ==>> $a?'). \
                                  symbols: parse file and list symbols. \
                                  analyze: project-wide type analysis stats.")
                    .build(),
            )
            .parameter(
                Parameter::builder("pattern")
                    .param_type(ParameterType::String)
                    .required(false)
                    .description("For search: pattern like '$a.foo($b)'. For ssr: rule like '$a.unwrap() ==>> $a?'")
                    .build(),
            )
            .parameter(
                Parameter::builder("path")
                    .param_type(ParameterType::String)
                    .required(false)
                    .description("Project path (directory with Cargo.toml) for diagnostics/analyze. File path for symbols.")
                    .build(),
            )
            .parameter(
                Parameter::builder("severity")
                    .param_type(ParameterType::String)
                    .required(false)
                    .description("Minimum severity for diagnostics (default: warning)")
                    .build(),
            )
            .build()
    }

    async fn execute(&self, args: Value) -> crate::Result<Value> {
        ensure_binary("rust-analyzer", &self.workspace_root).await?;

        let action = args["action"]
            .as_str()
            .ok_or_else(|| crate::PawanError::Tool("action required".into()))?;

        let timeout_dur = std::time::Duration::from_secs(60);

        match action {
            "diagnostics" => {
                let path = args["path"]
                    .as_str()
                    .unwrap_or(self.workspace_root.to_str().unwrap_or("."));
                let mut cmd_args = vec!["diagnostics", path];
                if let Some(sev) = args["severity"].as_str() {
                    cmd_args.extend(["--severity", sev]);
                }
                let result = tokio::time::timeout(
                    timeout_dur,
                    run_cmd("rust-analyzer", &cmd_args, &self.workspace_root),
                )
                .await;
                match result {
                    Ok(Ok((stdout, stderr, success))) => Ok(json!({
                        "success": success,
                        "diagnostics": stdout,
                        "count": stdout.lines().filter(|l| !l.is_empty()).count(),
                        "stderr": if stderr.is_empty() { None::<String> } else { Some(stderr) }
                    })),
                    Ok(Err(e)) => Err(crate::PawanError::Tool(e)),
                    Err(_) => Err(crate::PawanError::Tool(
                        "rust-analyzer diagnostics timed out (60s)".into(),
                    )),
                }
            }
            "search" => {
                let pattern = args["pattern"]
                    .as_str()
                    .ok_or_else(|| crate::PawanError::Tool("pattern required for search".into()))?;
                let result = tokio::time::timeout(
                    timeout_dur,
                    run_cmd("rust-analyzer", &["search", pattern], &self.workspace_root),
                )
                .await;
                match result {
                    Ok(Ok((stdout, stderr, success))) => Ok(json!({
                        "success": success,
                        "matches": stdout,
                        "count": stdout.lines().filter(|l| !l.is_empty()).count(),
                        "stderr": if stderr.is_empty() { None::<String> } else { Some(stderr) }
                    })),
                    Ok(Err(e)) => Err(crate::PawanError::Tool(e)),
                    Err(_) => Err(crate::PawanError::Tool(
                        "rust-analyzer search timed out (60s)".into(),
                    )),
                }
            }
            "ssr" => {
                let pattern = args["pattern"].as_str().ok_or_else(|| {
                    crate::PawanError::Tool(
                        "pattern required for ssr (format: '$a.unwrap() ==>> $a?')".into(),
                    )
                })?;
                let result = tokio::time::timeout(
                    timeout_dur,
                    run_cmd("rust-analyzer", &["ssr", pattern], &self.workspace_root),
                )
                .await;
                match result {
                    Ok(Ok((stdout, stderr, success))) => Ok(json!({
                        "success": success,
                        "output": stdout,
                        "stderr": if stderr.is_empty() { None::<String> } else { Some(stderr) }
                    })),
                    Ok(Err(e)) => Err(crate::PawanError::Tool(e)),
                    Err(_) => Err(crate::PawanError::Tool(
                        "rust-analyzer ssr timed out (60s)".into(),
                    )),
                }
            }
            "symbols" => {
                let path = args["path"]
                    .as_str()
                    .ok_or_else(|| crate::PawanError::Tool("path required for symbols".into()))?;
                let full_path = if std::path::Path::new(path).is_absolute() {
                    PathBuf::from(path)
                } else {
                    self.workspace_root.join(path)
                };
                let content = tokio::fs::read_to_string(&full_path).await.map_err(|e| {
                    crate::PawanError::Tool(format!("Failed to read {}: {}", path, e))
                })?;

                let mut child = tokio::process::Command::new("rust-analyzer")
                    .arg("symbols")
                    .stdin(Stdio::piped())
                    .stdout(Stdio::piped())
                    .stderr(Stdio::piped())
                    .spawn()
                    .map_err(|e| {
                        crate::PawanError::Tool(format!("Failed to spawn rust-analyzer: {}", e))
                    })?;

                if let Some(mut stdin) = child.stdin.take() {
                    use tokio::io::AsyncWriteExt;
                    let _ = stdin.write_all(content.as_bytes()).await;
                    drop(stdin);
                }

                let output = child.wait_with_output().await.map_err(|e| {
                    crate::PawanError::Tool(format!("rust-analyzer symbols failed: {}", e))
                })?;

                let stdout = String::from_utf8_lossy(&output.stdout).to_string();
                Ok(json!({
                    "success": output.status.success(),
                    "symbols": stdout,
                    "count": stdout.lines().filter(|l| !l.is_empty()).count()
                }))
            }
            "analyze" => {
                let path = args["path"]
                    .as_str()
                    .unwrap_or(self.workspace_root.to_str().unwrap_or("."));
                let result = tokio::time::timeout(
                    timeout_dur,
                    run_cmd(
                        "rust-analyzer",
                        &["analysis-stats", "--skip-inference", path],
                        &self.workspace_root,
                    ),
                )
                .await;
                match result {
                    Ok(Ok((stdout, stderr, success))) => Ok(json!({
                        "success": success,
                        "stats": stdout,
                        "stderr": if stderr.is_empty() { None::<String> } else { Some(stderr) }
                    })),
                    Ok(Err(e)) => Err(crate::PawanError::Tool(e)),
                    Err(_) => Err(crate::PawanError::Tool(
                        "rust-analyzer analysis-stats timed out (60s)".into(),
                    )),
                }
            }
            _ => Err(crate::PawanError::Tool(format!(
                "Unknown action: {action}. Use diagnostics/search/ssr/symbols/analyze"
            ))),
        }
    }
}

// ─── tests ──────────────────────────────────────────────────────────────────

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

    #[tokio::test]
    async fn test_ast_grep_tool_schema() {
        let tmp = TempDir::new().unwrap();
        let tool = AstGrepTool::new(tmp.path().to_path_buf());
        assert_eq!(tool.name(), "ast_grep");
        let schema = tool.parameters_schema();
        assert!(schema["properties"]["action"].is_object());
        assert!(schema["properties"]["pattern"].is_object());
    }

    #[tokio::test]
    async fn test_lsp_tool_schema() {
        let tmp = TempDir::new().unwrap();
        let tool = LspTool::new(tmp.path().to_path_buf());
        assert_eq!(tool.name(), "lsp");
        let schema = tool.parameters_schema();
        assert!(schema["properties"]["action"].is_object());
    }
}