Repository navigation
Expand file tree
/
Copy pathinstructions.rs
More file actions
623 lines (545 loc) · 17.8 KB
/
Copy pathinstructions.rs
File metadata and controls
623 lines (545 loc) · 17.8 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
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
//! User instructions management.
//!
//! Provides a system for managing custom instructions that modify
//! agent behavior, including project-specific and global instructions.
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use crate::error::Result;
/// Instruction source.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[derive(Default)]
pub enum InstructionSource {
/// Built-in default.
#[default]
Default,
/// Global user configuration.
Global,
/// Project-specific.
Project,
/// Session-specific.
Session,
/// Command-line override.
CommandLine,
/// Runtime override.
Runtime,
}
impl InstructionSource {
/// Get priority (higher = more important).
pub fn priority(&self) -> u8 {
match self {
Self::Default => 0,
Self::Global => 1,
Self::Project => 2,
Self::Session => 3,
Self::CommandLine => 4,
Self::Runtime => 5,
}
}
}
/// Instruction category.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[derive(Default)]
pub enum InstructionCategory {
/// General behavior.
#[default]
General,
/// Code style and formatting.
CodeStyle,
/// Testing requirements.
Testing,
/// Documentation requirements.
Documentation,
/// Security requirements.
Security,
/// Performance considerations.
Performance,
/// Tool usage.
Tools,
/// Communication style.
Communication,
/// Error handling.
ErrorHandling,
/// Git/version control.
Git,
}
/// A single instruction.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Instruction {
/// Instruction ID.
pub id: String,
/// The instruction text.
pub content: String,
/// Category.
pub category: InstructionCategory,
/// Source.
pub source: InstructionSource,
/// Is enabled.
pub enabled: bool,
/// Priority within category.
pub priority: u8,
/// Conditions for when to apply.
pub conditions: Vec<InstructionCondition>,
/// Tags.
pub tags: Vec<String>,
}
impl Instruction {
/// Create a new instruction.
pub fn new(id: impl Into<String>, content: impl Into<String>) -> Self {
Self {
id: id.into(),
content: content.into(),
category: InstructionCategory::General,
source: InstructionSource::Default,
enabled: true,
priority: 50,
conditions: Vec::new(),
tags: Vec::new(),
}
}
/// Set category.
pub fn with_category(mut self, category: InstructionCategory) -> Self {
self.category = category;
self
}
/// Set source.
pub fn with_source(mut self, source: InstructionSource) -> Self {
self.source = source;
self
}
/// Set priority.
pub fn with_priority(mut self, priority: u8) -> Self {
self.priority = priority;
self
}
/// Add condition.
pub fn when(mut self, condition: InstructionCondition) -> Self {
self.conditions.push(condition);
self
}
/// Add tag.
pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
self.tags.push(tag.into());
self
}
/// Disable instruction.
pub fn disabled(mut self) -> Self {
self.enabled = false;
self
}
/// Check if instruction applies to context.
pub fn applies_to(&self, context: &InstructionContext) -> bool {
if !self.enabled {
return false;
}
if self.conditions.is_empty() {
return true;
}
self.conditions.iter().all(|c| c.matches(context))
}
}
/// Condition for instruction application.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "value")]
pub enum InstructionCondition {
/// File extension match.
FileExtension(String),
/// File path pattern.
FilePath(String),
/// Project type.
ProjectType(String),
/// Framework.
Framework(String),
/// Has file.
HasFile(String),
/// Task type.
TaskType(String),
/// Language.
Language(String),
}
impl InstructionCondition {
/// Check if condition matches context.
pub fn matches(&self, context: &InstructionContext) -> bool {
match self {
Self::FileExtension(ext) => context.file_extensions.contains(ext),
Self::FilePath(pattern) => context
.current_file
.as_ref()
.map(|f| f.to_string_lossy().contains(pattern))
.unwrap_or(false),
Self::ProjectType(pt) => context.project_type.as_deref() == Some(pt),
Self::Framework(fw) => context.framework.as_deref() == Some(fw),
Self::HasFile(file) => context.root.join(file).exists(),
Self::TaskType(task) => context.task_type.as_deref() == Some(task),
Self::Language(lang) => context.languages.contains(lang),
}
}
}
/// Context for instruction evaluation.
#[derive(Debug, Clone, Default)]
pub struct InstructionContext {
/// Project root.
pub root: PathBuf,
/// Current file being worked on.
pub current_file: Option<PathBuf>,
/// File extensions in project.
pub file_extensions: Vec<String>,
/// Project type.
pub project_type: Option<String>,
/// Framework.
pub framework: Option<String>,
/// Languages used.
pub languages: Vec<String>,
/// Task type.
pub task_type: Option<String>,
}
impl InstructionContext {
/// Create from project path.
pub fn from_path(path: impl AsRef<Path>) -> Self {
let root = path.as_ref().to_path_buf();
let project_type = detect_project_type(&root);
Self {
root,
current_file: None,
file_extensions: Vec::new(),
project_type,
framework: None,
languages: Vec::new(),
task_type: None,
}
}
/// Set current file.
pub fn with_file(mut self, file: impl Into<PathBuf>) -> Self {
let file = file.into();
if let Some(ext) = file.extension() {
self.file_extensions.push(ext.to_string_lossy().to_string());
}
self.current_file = Some(file);
self
}
/// Set task type.
pub fn with_task(mut self, task_type: impl Into<String>) -> Self {
self.task_type = Some(task_type.into());
self
}
}
/// Instruction manager.
pub struct InstructionManager {
/// Instructions indexed by ID.
instructions: HashMap<String, Instruction>,
/// Instructions grouped by category.
by_category: HashMap<InstructionCategory, Vec<String>>,
/// Instructions grouped by source.
by_source: HashMap<InstructionSource, Vec<String>>,
}
impl InstructionManager {
/// Create a new instruction manager.
pub fn new() -> Self {
Self {
instructions: HashMap::new(),
by_category: HashMap::new(),
by_source: HashMap::new(),
}
}
/// Add an instruction.
pub fn add(&mut self, instruction: Instruction) {
let id = instruction.id.clone();
let category = instruction.category;
let source = instruction.source;
self.instructions.insert(id.clone(), instruction);
self.by_category
.entry(category)
.or_default()
.push(id.clone());
self.by_source.entry(source).or_default().push(id);
}
/// Remove an instruction.
pub fn remove(&mut self, id: &str) -> Option<Instruction> {
if let Some(instruction) = self.instructions.remove(id) {
// Clean up indexes
if let Some(ids) = self.by_category.get_mut(&instruction.category) {
ids.retain(|i| i != id);
}
if let Some(ids) = self.by_source.get_mut(&instruction.source) {
ids.retain(|i| i != id);
}
Some(instruction)
} else {
None
}
}
/// Get an instruction.
pub fn get(&self, id: &str) -> Option<&Instruction> {
self.instructions.get(id)
}
/// Enable an instruction.
pub fn enable(&mut self, id: &str) -> bool {
if let Some(inst) = self.instructions.get_mut(id) {
inst.enabled = true;
true
} else {
false
}
}
/// Disable an instruction.
pub fn disable(&mut self, id: &str) -> bool {
if let Some(inst) = self.instructions.get_mut(id) {
inst.enabled = false;
true
} else {
false
}
}
/// Get all instructions for a context.
pub fn for_context(&self, context: &InstructionContext) -> Vec<&Instruction> {
let mut instructions: Vec<_> = self
.instructions
.values()
.filter(|i| i.applies_to(context))
.collect();
// Sort by source priority, then instruction priority
instructions.sort_by(|a, b| {
let source_cmp = a.source.priority().cmp(&b.source.priority());
if source_cmp == std::cmp::Ordering::Equal {
b.priority.cmp(&a.priority)
} else {
source_cmp
}
});
instructions
}
/// Get instructions by category.
pub fn by_category(&self, category: InstructionCategory) -> Vec<&Instruction> {
self.by_category
.get(&category)
.map(|ids| {
ids.iter()
.filter_map(|id| self.instructions.get(id))
.collect()
})
.unwrap_or_default()
}
/// Get instructions by source.
pub fn by_source(&self, source: InstructionSource) -> Vec<&Instruction> {
self.by_source
.get(&source)
.map(|ids| {
ids.iter()
.filter_map(|id| self.instructions.get(id))
.collect()
})
.unwrap_or_default()
}
/// Generate prompt text from instructions.
pub fn generate_prompt(&self, context: &InstructionContext) -> String {
let instructions = self.for_context(context);
if instructions.is_empty() {
return String::new();
}
let mut prompt = String::from("# Instructions\n\n");
// Group by category
let mut by_cat: HashMap<InstructionCategory, Vec<&Instruction>> = HashMap::new();
for inst in instructions {
by_cat.entry(inst.category).or_default().push(inst);
}
for (category, insts) in by_cat {
prompt.push_str(&format!("## {category:?}\n\n"));
for inst in insts {
prompt.push_str(&format!("- {}\n", inst.content));
}
prompt.push('\n');
}
prompt
}
/// Load from directory.
pub fn load_from_dir(
&mut self,
dir: impl AsRef<Path>,
source: InstructionSource,
) -> Result<usize> {
let dir = dir.as_ref();
let mut count = 0;
// Look for instruction files
for name in [
"instructions.md",
"INSTRUCTIONS.md",
".cortex-instructions",
"cortex.instructions",
] {
let path = dir.join(name);
if path.exists() {
let content = std::fs::read_to_string(&path)?;
let instructions = parse_instructions(&content, source);
for inst in instructions {
self.add(inst);
count += 1;
}
}
}
Ok(count)
}
/// Get all instructions.
pub fn all(&self) -> Vec<&Instruction> {
self.instructions.values().collect()
}
/// Get count.
pub fn count(&self) -> usize {
self.instructions.len()
}
/// Clear all instructions.
pub fn clear(&mut self) {
self.instructions.clear();
self.by_category.clear();
self.by_source.clear();
}
}
impl Default for InstructionManager {
fn default() -> Self {
Self::new()
}
}
/// Parse instructions from markdown content.
fn parse_instructions(content: &str, source: InstructionSource) -> Vec<Instruction> {
let mut instructions = Vec::new();
let mut current_category = InstructionCategory::General;
let mut id_counter = 0;
for line in content.lines() {
let line = line.trim();
// Category headers
if line.starts_with("## ") || line.starts_with("# ") {
let header = line.trim_start_matches('#').trim().to_lowercase();
current_category = match header.as_str() {
"code style" | "style" => InstructionCategory::CodeStyle,
"testing" | "tests" => InstructionCategory::Testing,
"documentation" | "docs" => InstructionCategory::Documentation,
"security" => InstructionCategory::Security,
"performance" => InstructionCategory::Performance,
"tools" => InstructionCategory::Tools,
"communication" => InstructionCategory::Communication,
"error handling" | "errors" => InstructionCategory::ErrorHandling,
"git" | "version control" => InstructionCategory::Git,
_ => InstructionCategory::General,
};
}
// Instruction items
else if line.starts_with("- ") || line.starts_with("* ") {
let content = line.trim_start_matches('-').trim_start_matches('*').trim();
if !content.is_empty() {
id_counter += 1;
let id = format!("inst_{id_counter}");
let instruction = Instruction::new(id, content)
.with_category(current_category)
.with_source(source);
instructions.push(instruction);
}
}
}
instructions
}
/// Detect project type from path.
fn detect_project_type(path: &Path) -> Option<String> {
if path.join("Cargo.toml").exists() {
Some("rust".to_string())
} else if path.join("package.json").exists() {
Some("node".to_string())
} else if path.join("pyproject.toml").exists() || path.join("setup.py").exists() {
Some("python".to_string())
} else if path.join("go.mod").exists() {
Some("go".to_string())
} else {
None
}
}
/// Default instructions.
pub mod defaults {
use super::*;
/// Code style instructions.
pub fn code_style() -> Vec<Instruction> {
vec![
Instruction::new("style_1", "Follow existing code style and conventions")
.with_category(InstructionCategory::CodeStyle),
Instruction::new("style_2", "Use meaningful variable and function names")
.with_category(InstructionCategory::CodeStyle),
Instruction::new("style_3", "Keep functions small and focused")
.with_category(InstructionCategory::CodeStyle),
]
}
/// Testing instructions.
pub fn testing() -> Vec<Instruction> {
vec![
Instruction::new("test_1", "Write tests for new functionality")
.with_category(InstructionCategory::Testing),
Instruction::new("test_2", "Ensure existing tests pass")
.with_category(InstructionCategory::Testing),
]
}
/// Security instructions.
pub fn security() -> Vec<Instruction> {
vec![
Instruction::new("sec_1", "Never commit secrets or credentials")
.with_category(InstructionCategory::Security),
Instruction::new("sec_2", "Validate all user input")
.with_category(InstructionCategory::Security),
Instruction::new("sec_3", "Use parameterized queries for database operations")
.with_category(InstructionCategory::Security),
]
}
/// All default instructions.
pub fn all() -> Vec<Instruction> {
let mut instructions = Vec::new();
instructions.extend(code_style());
instructions.extend(testing());
instructions.extend(security());
instructions
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_instruction_creation() {
let inst = Instruction::new("test", "Test instruction")
.with_category(InstructionCategory::CodeStyle)
.with_priority(100);
assert_eq!(inst.category, InstructionCategory::CodeStyle);
assert_eq!(inst.priority, 100);
assert!(inst.enabled);
}
#[test]
fn test_instruction_conditions() {
let inst = Instruction::new("test", "Test")
.when(InstructionCondition::FileExtension("rs".to_string()));
let context = InstructionContext {
file_extensions: vec!["rs".to_string()],
..Default::default()
};
assert!(inst.applies_to(&context));
let other_context = InstructionContext {
file_extensions: vec!["py".to_string()],
..Default::default()
};
assert!(!inst.applies_to(&other_context));
}
#[test]
fn test_instruction_manager() {
let mut manager = InstructionManager::new();
manager.add(Instruction::new("a", "First"));
manager.add(Instruction::new("b", "Second").with_category(InstructionCategory::Testing));
assert_eq!(manager.count(), 2);
assert_eq!(manager.by_category(InstructionCategory::Testing).len(), 1);
}
#[test]
fn test_parse_instructions() {
let content = r#"
# Code Style
- Use consistent indentation
- Follow naming conventions
## Testing
- Write unit tests
"#;
let instructions = parse_instructions(content, InstructionSource::Project);
assert_eq!(instructions.len(), 3);
}
}