-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinteractive.rs
More file actions
463 lines (396 loc) · 14.3 KB
/
interactive.rs
File metadata and controls
463 lines (396 loc) · 14.3 KB
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
use crate::adapters::llm::{LLMAdapter, LLMRequest};
use anyhow::Result;
use regex::Regex;
use std::collections::HashSet;
pub struct InteractiveCommand {
pub command: CommandType,
pub args: Vec<String>,
#[allow(dead_code)] // Set by webhook handler when PR context is available
pub context: Option<String>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum CommandType {
Review,
Ignore,
Explain,
Generate,
Help,
Config,
}
impl InteractiveCommand {
pub fn parse(comment: &str) -> Option<Self> {
let command_regex = Regex::new(r"@diffscope\s+(\w+)(?:\s+(.*))?").ok()?;
if let Some(captures) = command_regex.captures(comment) {
let command_str = captures.get(1)?.as_str();
let args_str = captures.get(2).map(|m| m.as_str()).unwrap_or("");
let command_type = match command_str.to_lowercase().as_str() {
"review" => CommandType::Review,
"ignore" => CommandType::Ignore,
"explain" => CommandType::Explain,
"generate" => CommandType::Generate,
"help" => CommandType::Help,
"config" => CommandType::Config,
_ => return None,
};
let args = if args_str.is_empty() {
Vec::new()
} else {
args_str.split_whitespace().map(String::from).collect()
};
Some(InteractiveCommand {
command: command_type,
args,
context: None,
})
} else {
None
}
}
pub async fn execute(
&self,
adapter: &dyn LLMAdapter,
diff_content: Option<&str>,
) -> Result<String> {
match &self.command {
CommandType::Review => self.execute_review(adapter, diff_content).await,
CommandType::Ignore => self.execute_ignore(),
CommandType::Explain => self.execute_explain(adapter, diff_content).await,
CommandType::Generate => self.execute_generate(adapter).await,
CommandType::Help => Ok(Self::get_help_text()),
CommandType::Config => Ok(Self::get_config_info()),
}
}
async fn execute_review(
&self,
adapter: &dyn LLMAdapter,
diff_content: Option<&str>,
) -> Result<String> {
if let Some(diff) = diff_content {
let prompt = if self.args.is_empty() {
format!("Review the following code changes:\n\n{}", diff)
} else {
let focus = self.args.join(" ");
format!(
"Review the following code changes with focus on {}:\n\n{}",
focus, diff
)
};
let request = LLMRequest {
system_prompt: "You are a code reviewer. Provide concise, actionable feedback."
.to_string(),
user_prompt: prompt,
temperature: Some(0.3),
max_tokens: Some(1000),
};
let response = adapter.complete(request).await?;
Ok(format!("## 🔍 Code Review\n\n{}", response.content))
} else {
Ok("No diff content available for review.".to_string())
}
}
fn execute_ignore(&self) -> Result<String> {
if self.args.is_empty() {
Ok(
"Please specify what to ignore (e.g., @diffscope ignore src/generated/)"
.to_string(),
)
} else {
let patterns = self.args.join(", ");
Ok(format!("✅ Will ignore: {}\n\nAdd these patterns to your .diffscope.yml for permanent configuration.", patterns))
}
}
async fn execute_explain(
&self,
adapter: &dyn LLMAdapter,
diff_content: Option<&str>,
) -> Result<String> {
let context = if self.args.is_empty() {
diff_content.unwrap_or("No specific context").to_string()
} else {
// Try to find specific line or section
let target = self.args.join(" ");
format!(
"Explain the following in the context of the code changes: {}",
target
)
};
let request = LLMRequest {
system_prompt:
"You are a helpful code explainer. Provide clear, educational explanations."
.to_string(),
user_prompt: format!("Explain this code or change:\n\n{}", context),
temperature: Some(0.5),
max_tokens: Some(800),
};
let response = adapter.complete(request).await?;
Ok(format!("## 💡 Explanation\n\n{}", response.content))
}
async fn execute_generate(&self, adapter: &dyn LLMAdapter) -> Result<String> {
if self.args.is_empty() {
return Ok(
"Please specify what to generate (e.g., @diffscope generate tests)".to_string(),
);
}
let target = self.args[0].as_str();
let context = self.args[1..].join(" ");
let (system_prompt, user_prompt) = match target {
"tests" => (
"You are a test generation expert. Generate comprehensive tests.",
format!("Generate unit tests for the following context: {}", context),
),
"docs" => (
"You are a documentation expert. Generate clear, comprehensive documentation.",
format!("Generate documentation for: {}", context),
),
"types" => (
"You are a TypeScript/type system expert. Generate proper type definitions.",
format!("Generate type definitions for: {}", context),
),
_ => (
"You are a helpful code generator.",
format!("Generate {} for: {}", target, context),
),
};
let request = LLMRequest {
system_prompt: system_prompt.to_string(),
user_prompt,
temperature: Some(0.7),
max_tokens: Some(1500),
};
let response = adapter.complete(request).await?;
Ok(format!(
"## 🔨 Generated {}\n\n```\n{}\n```",
target, response.content
))
}
pub fn help_text() -> String {
Self::get_help_text()
}
fn get_help_text() -> String {
r#"## 🤖 DiffScope Interactive Commands
Available commands:
### Review
- `@diffscope review` - Review the current changes
- `@diffscope review security` - Focus review on security aspects
- `@diffscope review performance` - Focus on performance
### Ignore
- `@diffscope ignore src/generated/` - Ignore files matching pattern
- `@diffscope ignore *.test.js` - Ignore test files
### Explain
- `@diffscope explain` - Explain the overall changes
- `@diffscope explain line 42` - Explain specific line
- `@diffscope explain function_name` - Explain specific function
### Generate
- `@diffscope generate tests` - Generate unit tests
- `@diffscope generate docs` - Generate documentation
- `@diffscope generate types` - Generate type definitions
### Other
- `@diffscope help` - Show this help message
- `@diffscope config` - Show current configuration"#
.to_string()
}
fn get_config_info() -> String {
r#"## ⚙️ Current Configuration
To configure DiffScope behavior, create a `.diffscope.yml` file:
```yaml
model: gpt-4o
temperature: 0.2
max_tokens: 4000
# Ignore patterns
exclude_patterns:
- "**/*.generated.*"
- "**/node_modules/**"
# Path-specific rules
paths:
"src/api/**":
focus: ["security", "validation"]
"tests/**":
focus: ["coverage", "assertions"]
```
Interactive commands respect these configurations."#
.to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
// === InteractiveCommand::parse tests ===
#[test]
fn test_parse_review_command() {
let cmd = InteractiveCommand::parse("@diffscope review").unwrap();
assert_eq!(cmd.command, CommandType::Review);
assert!(cmd.args.is_empty());
assert!(cmd.context.is_none());
}
#[test]
fn test_parse_review_with_focus() {
let cmd = InteractiveCommand::parse("@diffscope review security performance").unwrap();
assert_eq!(cmd.command, CommandType::Review);
assert_eq!(cmd.args, vec!["security", "performance"]);
}
#[test]
fn test_parse_ignore_command() {
let cmd = InteractiveCommand::parse("@diffscope ignore src/generated/").unwrap();
assert_eq!(cmd.command, CommandType::Ignore);
assert_eq!(cmd.args, vec!["src/generated/"]);
}
#[test]
fn test_parse_explain_command() {
let cmd = InteractiveCommand::parse("@diffscope explain").unwrap();
assert_eq!(cmd.command, CommandType::Explain);
}
#[test]
fn test_parse_generate_tests() {
let cmd = InteractiveCommand::parse("@diffscope generate tests").unwrap();
assert_eq!(cmd.command, CommandType::Generate);
assert_eq!(cmd.args, vec!["tests"]);
}
#[test]
fn test_parse_help() {
let cmd = InteractiveCommand::parse("@diffscope help").unwrap();
assert_eq!(cmd.command, CommandType::Help);
}
#[test]
fn test_parse_config() {
let cmd = InteractiveCommand::parse("@diffscope config").unwrap();
assert_eq!(cmd.command, CommandType::Config);
}
#[test]
fn test_parse_case_insensitive() {
let cmd = InteractiveCommand::parse("@diffscope REVIEW").unwrap();
assert_eq!(cmd.command, CommandType::Review);
let cmd = InteractiveCommand::parse("@diffscope Help").unwrap();
assert_eq!(cmd.command, CommandType::Help);
}
#[test]
fn test_parse_unknown_command_returns_none() {
assert!(InteractiveCommand::parse("@diffscope foobar").is_none());
}
#[test]
fn test_parse_no_at_mention_returns_none() {
assert!(InteractiveCommand::parse("just a regular comment").is_none());
}
#[test]
fn test_parse_empty_string_returns_none() {
assert!(InteractiveCommand::parse("").is_none());
}
#[test]
fn test_parse_at_mention_only_returns_none() {
assert!(InteractiveCommand::parse("@diffscope").is_none());
}
#[test]
fn test_parse_embedded_in_text() {
// Command embedded in longer comment
let cmd =
InteractiveCommand::parse("Hey team, can you @diffscope review this PR?").unwrap();
assert_eq!(cmd.command, CommandType::Review);
}
#[test]
fn test_parse_extra_whitespace() {
let cmd = InteractiveCommand::parse("@diffscope review security").unwrap();
assert_eq!(cmd.command, CommandType::Review);
assert_eq!(cmd.args, vec!["security"]);
}
#[test]
fn test_parse_different_at_mention_ignored() {
assert!(InteractiveCommand::parse("@someone review").is_none());
}
// === execute_ignore tests ===
#[test]
fn test_ignore_no_args() {
let cmd = InteractiveCommand {
command: CommandType::Ignore,
args: vec![],
context: None,
};
let result = cmd.execute_ignore().unwrap();
assert!(result.contains("specify what to ignore"));
}
#[test]
fn test_ignore_with_patterns() {
let cmd = InteractiveCommand {
command: CommandType::Ignore,
args: vec!["src/generated/".to_string(), "*.test.js".to_string()],
context: None,
};
let result = cmd.execute_ignore().unwrap();
assert!(result.contains("src/generated/"));
assert!(result.contains("*.test.js"));
}
// === help_text / config tests ===
#[test]
fn test_help_text_contains_commands() {
let help = InteractiveCommand::help_text();
assert!(help.contains("@diffscope review"));
assert!(help.contains("@diffscope ignore"));
assert!(help.contains("@diffscope explain"));
assert!(help.contains("@diffscope generate"));
assert!(help.contains("@diffscope help"));
assert!(help.contains("@diffscope config"));
}
#[test]
fn test_config_info_contains_yaml() {
let info = InteractiveCommand::get_config_info();
assert!(info.contains(".diffscope.yml"));
assert!(info.contains("exclude_patterns"));
}
// === InteractiveProcessor tests ===
#[test]
fn test_processor_new_empty() {
let processor = InteractiveProcessor::new();
assert!(!processor.should_ignore("any/path"));
}
#[test]
fn test_processor_add_and_check_pattern() {
let mut processor = InteractiveProcessor::new();
processor.add_ignore_pattern("src/generated/");
assert!(processor.should_ignore("src/generated/types.rs"));
assert!(!processor.should_ignore("src/main.rs"));
}
#[test]
fn test_processor_glob_pattern() {
let mut processor = InteractiveProcessor::new();
processor.add_ignore_pattern("*.test.js");
assert!(processor.should_ignore("foo.test.js"));
assert!(!processor.should_ignore("foo.js"));
}
#[test]
fn test_processor_multiple_patterns() {
let mut processor = InteractiveProcessor::new();
processor.add_ignore_pattern("vendor/");
processor.add_ignore_pattern("*.generated.*");
assert!(processor.should_ignore("vendor/lib.js"));
assert!(processor.should_ignore("types.generated.ts"));
assert!(!processor.should_ignore("src/main.rs"));
}
}
/// Manages per-session ignore patterns from @diffscope ignore commands.
/// Will be wired into the review pipeline's triage filter.
#[allow(dead_code)]
pub struct InteractiveProcessor {
ignored_patterns: HashSet<String>,
}
#[allow(dead_code)]
impl InteractiveProcessor {
pub fn new() -> Self {
Self {
ignored_patterns: HashSet::new(),
}
}
pub fn add_ignore_pattern(&mut self, pattern: &str) {
self.ignored_patterns.insert(pattern.to_string());
}
pub fn should_ignore(&self, path: &str) -> bool {
self.ignored_patterns.iter().any(|pattern| {
// Simple glob matching
if pattern.contains('*') {
let regex_pattern = pattern.replace("*", ".*");
regex::Regex::new(®ex_pattern)
.map(|re| re.is_match(path))
.unwrap_or(false)
} else {
path.contains(pattern)
}
})
}
}