-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinteractive.rs
More file actions
281 lines (243 loc) · 8.65 KB
/
interactive.rs
File metadata and controls
281 lines (243 loc) · 8.65 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
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()
}
}
/// 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)
}
})
}
}