-
Notifications
You must be signed in to change notification settings - Fork 436
Expand file tree
/
Copy pathcontext.rs
More file actions
1056 lines (881 loc) · 36.8 KB
/
Copy pathcontext.rs
File metadata and controls
1056 lines (881 loc) · 36.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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::collections::HashMap;
use std::io::Write;
use std::path::{
Path,
PathBuf,
};
use std::sync::Arc;
use eyre::{
Result,
eyre,
};
use glob::glob;
use regex::Regex;
use serde::{
Deserialize,
Serialize,
};
use tracing::debug;
use super::consts::CONTEXT_FILES_MAX_SIZE;
use super::hooks::{
Hook,
HookExecutor,
};
use super::util::drop_matched_context_files;
use crate::platform::Context;
use crate::util::directories;
pub const AMAZONQ_FILENAME: &str = "AmazonQ.md";
/// Configuration for context files, containing paths to include in the context.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct ContextConfig {
/// List of file paths or glob patterns to include in the context.
pub paths: Vec<String>,
/// Map of Hook Name to [`Hook`]. The hook name serves as the hook's ID.
pub hooks: HashMap<String, Hook>,
}
#[allow(dead_code)]
/// Manager for context files and profiles.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextManager {
#[serde(skip)]
#[serde(default = "default_context")]
ctx: Arc<Context>,
max_context_files_size: usize,
/// Global context configuration that applies to all profiles.
pub global_config: ContextConfig,
/// Name of the current active profile.
pub current_profile: String,
/// Context configuration for the current profile.
pub profile_config: ContextConfig,
#[serde(skip)]
pub hook_executor: HookExecutor,
}
#[allow(dead_code)]
impl ContextManager {
/// Create a new ContextManager with default settings.
///
/// This will:
/// 1. Create the necessary directories if they don't exist
/// 2. Load the global configuration
/// 3. Load the default profile configuration
///
/// # Arguments
/// * `ctx` - The context to use
/// * `max_context_files_size` - Optional maximum token size for context files. If not provided,
/// defaults to `CONTEXT_FILES_MAX_SIZE`.
///
/// # Returns
/// A Result containing the new ContextManager or an error
pub async fn new(ctx: Arc<Context>, max_context_files_size: Option<usize>) -> Result<Self> {
let max_context_files_size = max_context_files_size.unwrap_or(CONTEXT_FILES_MAX_SIZE);
let profiles_dir = directories::chat_profiles_dir(&ctx)?;
ctx.fs().create_dir_all(&profiles_dir).await?;
let global_config = load_global_config(&ctx).await?;
let current_profile = "default".to_string();
let profile_config = load_profile_config(&ctx, ¤t_profile).await?;
Ok(Self {
ctx,
max_context_files_size,
global_config,
current_profile,
profile_config,
hook_executor: HookExecutor::new(),
})
}
/// Save the current configuration to disk.
///
/// # Arguments
/// * `global` - If true, save the global configuration; otherwise, save the current profile
/// configuration
///
/// # Returns
/// A Result indicating success or an error
async fn save_config(&self, global: bool) -> Result<()> {
if global {
let global_path = directories::chat_global_context_path(&self.ctx)?;
let contents = serde_json::to_string_pretty(&self.global_config)
.map_err(|e| eyre!("Failed to serialize global configuration: {}", e))?;
self.ctx.fs().write(&global_path, contents).await?;
} else {
let profile_path = profile_context_path(&self.ctx, &self.current_profile)?;
if let Some(parent) = profile_path.parent() {
self.ctx.fs().create_dir_all(parent).await?;
}
let contents = serde_json::to_string_pretty(&self.profile_config)
.map_err(|e| eyre!("Failed to serialize profile configuration: {}", e))?;
self.ctx.fs().write(&profile_path, contents).await?;
}
Ok(())
}
/// Reloads the global and profile config from disk.
pub async fn reload_config(&mut self) -> Result<()> {
self.global_config = load_global_config(&self.ctx).await?;
self.profile_config = load_profile_config(&self.ctx, &self.current_profile).await?;
Ok(())
}
/// Add paths to the context configuration.
///
/// # Arguments
/// * `paths` - List of paths to add
/// * `global` - If true, add to global configuration; otherwise, add to current profile
/// configuration
/// * `force` - If true, skip validation that the path exists
///
/// # Returns
/// A Result indicating success or an error
pub async fn add_paths(&mut self, paths: Vec<String>, global: bool, force: bool) -> Result<()> {
let mut all_paths = self.global_config.paths.clone();
all_paths.append(&mut self.profile_config.paths.clone());
// Validate paths exist before adding them
if !force {
let mut context_files = Vec::new();
// Check each path to make sure it exists or matches at least one file
for path in &paths {
// We're using a temporary context_files vector just for validation
// Pass is_validation=true to ensure we error if glob patterns don't match any files
match process_path(&self.ctx, path, &mut context_files, true).await {
Ok(_) => {}, // Path is valid
Err(e) => return Err(eyre!("Invalid path '{}': {}. Use --force to add anyway.", path, e)),
}
}
}
// Add each path, checking for duplicates
for path in paths {
if all_paths.contains(&path) {
return Err(eyre!("Rule '{}' already exists.", path));
}
if global {
self.global_config.paths.push(path);
} else {
self.profile_config.paths.push(path);
}
}
// Save the updated configuration
self.save_config(global).await?;
Ok(())
}
/// Remove paths from the context configuration.
///
/// # Arguments
/// * `paths` - List of paths to remove
/// * `global` - If true, remove from global configuration; otherwise, remove from current
/// profile configuration
///
/// # Returns
/// A Result indicating success or an error
pub async fn remove_paths(&mut self, paths: Vec<String>, global: bool) -> Result<()> {
// Get reference to the appropriate config
let config = self.get_config_mut(global);
// Track if any paths were removed
let mut removed_any = false;
// Remove each path if it exists
for path in paths {
let original_len = config.paths.len();
config.paths.retain(|p| p != &path);
if config.paths.len() < original_len {
removed_any = true;
}
}
if !removed_any {
return Err(eyre!("None of the specified paths were found in the context"));
}
// Save the updated configuration
self.save_config(global).await?;
Ok(())
}
/// List all available profiles.
///
/// # Returns
/// A Result containing a vector of profile names, with "default" always first
pub async fn list_profiles(&self) -> Result<Vec<String>> {
let mut profiles = Vec::new();
// Always include default profile
profiles.push("default".to_string());
// Read profile directory and extract profile names
let profiles_dir = directories::chat_profiles_dir(&self.ctx)?;
if profiles_dir.exists() {
let mut read_dir = self.ctx.fs().read_dir(&profiles_dir).await?;
while let Some(entry) = read_dir.next_entry().await? {
let path = entry.path();
if let (true, Some(name)) = (path.is_dir(), path.file_name()) {
if name != "default" {
profiles.push(name.to_string_lossy().to_string());
}
}
}
}
// Sort non-default profiles alphabetically
if profiles.len() > 1 {
profiles[1..].sort();
}
Ok(profiles)
}
/// List all available profiles using blocking operations.
///
/// Similar to list_profiles but uses synchronous filesystem operations.
///
/// # Returns
/// A Result containing a vector of profile names, with "default" always first
pub fn list_profiles_blocking(&self) -> Result<Vec<String>> {
let mut profiles = Vec::new();
// Always include default profile
profiles.push("default".to_string());
// Read profile directory and extract profile names
let profiles_dir = directories::chat_profiles_dir(&self.ctx)?;
if profiles_dir.exists() {
for entry in std::fs::read_dir(profiles_dir)? {
let entry = entry?;
let path = entry.path();
if let (true, Some(name)) = (path.is_dir(), path.file_name()) {
if name != "default" {
profiles.push(name.to_string_lossy().to_string());
}
}
}
}
// Sort non-default profiles alphabetically
if profiles.len() > 1 {
profiles[1..].sort();
}
Ok(profiles)
}
/// Clear all paths from the context configuration.
///
/// # Arguments
/// * `global` - If true, clear global configuration; otherwise, clear current profile
/// configuration
///
/// # Returns
/// A Result indicating success or an error
pub async fn clear(&mut self, global: bool) -> Result<()> {
// Clear the appropriate config
if global {
self.global_config.paths.clear();
} else {
self.profile_config.paths.clear();
}
// Save the updated configuration
self.save_config(global).await?;
Ok(())
}
/// Create a new profile.
///
/// # Arguments
/// * `name` - Name of the profile to create
///
/// # Returns
/// A Result indicating success or an error
pub async fn create_profile(&self, name: &str) -> Result<()> {
validate_profile_name(name)?;
// Check if profile already exists
let profile_path = profile_context_path(&self.ctx, name)?;
if profile_path.exists() {
return Err(eyre!("Profile '{}' already exists", name));
}
// Create empty profile configuration
let config = ContextConfig::default();
let contents = serde_json::to_string_pretty(&config)
.map_err(|e| eyre!("Failed to serialize profile configuration: {}", e))?;
// Create the file
if let Some(parent) = profile_path.parent() {
self.ctx.fs().create_dir_all(parent).await?;
}
self.ctx.fs().write(&profile_path, contents).await?;
Ok(())
}
/// Delete a profile.
///
/// # Arguments
/// * `name` - Name of the profile to delete
///
/// # Returns
/// A Result indicating success or an error
pub async fn delete_profile(&self, name: &str) -> Result<()> {
if name == "default" {
return Err(eyre!("Cannot delete the default profile"));
} else if name == self.current_profile {
return Err(eyre!(
"Cannot delete the active profile. Switch to another profile first"
));
}
let profile_path = profile_dir_path(&self.ctx, name)?;
if !profile_path.exists() {
return Err(eyre!("Profile '{}' does not exist", name));
}
self.ctx.fs().remove_dir_all(&profile_path).await?;
Ok(())
}
/// Rename a profile.
///
/// # Arguments
/// * `old_name` - Current name of the profile
/// * `new_name` - New name for the profile
///
/// # Returns
/// A Result indicating success or an error
pub async fn rename_profile(&mut self, old_name: &str, new_name: &str) -> Result<()> {
// Validate profile names
if old_name == "default" {
return Err(eyre!("Cannot rename the default profile"));
}
if new_name == "default" {
return Err(eyre!("Cannot rename to 'default' as it's a reserved profile name"));
}
validate_profile_name(new_name)?;
let old_profile_path = profile_dir_path(&self.ctx, old_name)?;
if !old_profile_path.exists() {
return Err(eyre!("Profile '{}' not found", old_name));
}
let new_profile_path = profile_dir_path(&self.ctx, new_name)?;
if new_profile_path.exists() {
return Err(eyre!("Profile '{}' already exists", new_name));
}
self.ctx.fs().rename(&old_profile_path, &new_profile_path).await?;
// If the current profile is being renamed, update the current_profile field
if self.current_profile == old_name {
self.current_profile = new_name.to_string();
self.profile_config = load_profile_config(&self.ctx, new_name).await?;
}
Ok(())
}
/// Switch to a different profile.
///
/// # Arguments
/// * `name` - Name of the profile to switch to
///
/// # Returns
/// A Result indicating success or an error
pub async fn switch_profile(&mut self, name: &str) -> Result<()> {
validate_profile_name(name)?;
self.hook_executor.profile_cache.clear();
// Special handling for default profile - it always exists
if name == "default" {
// Load the default profile configuration
let profile_config = load_profile_config(&self.ctx, name).await?;
// Update the current profile
self.current_profile = name.to_string();
self.profile_config = profile_config;
return Ok(());
}
// Check if profile exists
let profile_path = profile_context_path(&self.ctx, name)?;
if !profile_path.exists() {
return Err(eyre!("Profile '{}' does not exist. Use 'create' to create it", name));
}
// Update the current profile
self.current_profile = name.to_string();
self.profile_config = load_profile_config(&self.ctx, name).await?;
Ok(())
}
/// Get all context files (global + profile-specific).
///
/// This method:
/// 1. Processes all paths in the global and profile configurations
/// 2. Expands glob patterns to include matching files
/// 3. Reads the content of each file
/// 4. Returns a vector of (filename, content) pairs
///
///
/// # Returns
/// A Result containing a vector of (filename, content) pairs or an error
pub async fn get_context_files(&self) -> Result<Vec<(String, String)>> {
let mut context_files = Vec::new();
self.collect_context_files(&self.global_config.paths, &mut context_files)
.await?;
self.collect_context_files(&self.profile_config.paths, &mut context_files)
.await?;
context_files.sort_by(|a, b| a.0.cmp(&b.0));
context_files.dedup_by(|a, b| a.0 == b.0);
Ok(context_files)
}
pub async fn get_context_files_by_path(&self, path: &str) -> Result<Vec<(String, String)>> {
let mut context_files = Vec::new();
process_path(&self.ctx, path, &mut context_files, true).await?;
Ok(context_files)
}
/// Get all context files from the global configuration.
pub async fn get_global_context_files(&self) -> Result<Vec<(String, String)>> {
let mut context_files = Vec::new();
self.collect_context_files(&self.global_config.paths, &mut context_files)
.await?;
Ok(context_files)
}
/// Get all context files from the current profile configuration.
pub async fn get_current_profile_context_files(&self) -> Result<Vec<(String, String)>> {
let mut context_files = Vec::new();
self.collect_context_files(&self.profile_config.paths, &mut context_files)
.await?;
Ok(context_files)
}
/// Collects context files and optionally drops files if the total size exceeds the limit.
/// Returns (files_to_use, dropped_files)
pub async fn collect_context_files_with_limit(&self) -> Result<(Vec<(String, String)>, Vec<(String, String)>)> {
let mut files = self.get_context_files().await?;
let dropped_files = drop_matched_context_files(&mut files, self.max_context_files_size).unwrap_or_default();
// remove dropped files from files
files.retain(|file| !dropped_files.iter().any(|dropped| dropped.0 == file.0));
Ok((files, dropped_files))
}
async fn collect_context_files(&self, paths: &[String], context_files: &mut Vec<(String, String)>) -> Result<()> {
for path in paths {
// Use is_validation=false to handle non-matching globs gracefully
process_path(&self.ctx, path, context_files, false).await?;
}
Ok(())
}
fn get_config_mut(&mut self, global: bool) -> &mut ContextConfig {
if global {
&mut self.global_config
} else {
&mut self.profile_config
}
}
/// Add hooks to the context config. If another hook with the same name already exists, throw an
/// error.
///
/// # Arguments
/// * `hook` - name of the hook to delete
/// * `global` - If true, the add to the global config. If false, add to the current profile
/// config.
/// * `conversation_start` - If true, add the hook to conversation_start. Otherwise, it will be
/// added to per_prompt.
pub async fn add_hook(&mut self, name: String, hook: Hook, global: bool) -> Result<()> {
let config = self.get_config_mut(global);
if config.hooks.contains_key(&name) {
return Err(eyre!("name already exists."));
}
config.hooks.insert(name, hook);
self.save_config(global).await
}
/// Delete hook(s) by name
/// # Arguments
/// * `name` - name of the hook to delete
/// * `global` - If true, the delete from the global config. If false, delete from the current
/// profile config
pub async fn remove_hook(&mut self, name: &str, global: bool) -> Result<()> {
let config = self.get_config_mut(global);
if !config.hooks.contains_key(name) {
return Err(eyre!("does not exist."));
}
config.hooks.remove(name);
self.save_config(global).await
}
/// Sets the "disabled" field on any [`Hook`] with the given name
/// # Arguments
/// * `disable` - Set "disabled" field to this value
pub async fn set_hook_disabled(&mut self, name: &str, global: bool, disable: bool) -> Result<()> {
let config = self.get_config_mut(global);
if !config.hooks.contains_key(name) {
return Err(eyre!("does not exist."));
}
if let Some(hook) = config.hooks.get_mut(name) {
hook.disabled = disable;
}
self.save_config(global).await
}
/// Sets the "disabled" field on all [`Hook`]s
/// # Arguments
/// * `disable` - Set all "disabled" fields to this value
pub async fn set_all_hooks_disabled(&mut self, global: bool, disable: bool) -> Result<()> {
let config = self.get_config_mut(global);
config.hooks.iter_mut().for_each(|(_, h)| h.disabled = disable);
self.save_config(global).await
}
/// Run all the currently enabled hooks from both the global and profile contexts.
/// Skipped hooks (disabled) will not appear in the output.
/// # Arguments
/// * `updates` - output stream to write hook run status to if Some, else do nothing if None
/// # Returns
/// A vector containing pairs of a [`Hook`] definition and its execution output
pub async fn run_hooks(&mut self, updates: Option<&mut impl Write>) -> Vec<(Hook, String)> {
let mut hooks: Vec<&Hook> = Vec::new();
// Set internal hook states
let configs = [
(&mut self.global_config.hooks, true),
(&mut self.profile_config.hooks, false),
];
for (hook_list, is_global) in configs {
hooks.extend(hook_list.iter_mut().map(|(name, h)| {
h.name = name.to_string();
h.is_global = is_global;
&*h
}));
}
self.hook_executor.run_hooks(hooks, updates).await
}
}
fn profile_dir_path(ctx: &Context, profile_name: &str) -> Result<PathBuf> {
Ok(directories::chat_profiles_dir(ctx)?.join(profile_name))
}
/// Path to the context config file for `profile_name`.
pub fn profile_context_path(ctx: &Context, profile_name: &str) -> Result<PathBuf> {
Ok(directories::chat_profiles_dir(ctx)?
.join(profile_name)
.join("context.json"))
}
/// Load the global context configuration.
///
/// If the global configuration file doesn't exist, returns a default configuration.
async fn load_global_config(ctx: &Context) -> Result<ContextConfig> {
let global_path = directories::chat_global_context_path(ctx)?;
debug!(?global_path, "loading profile config");
if ctx.fs().exists(&global_path) {
let contents = ctx.fs().read_to_string(&global_path).await?;
let config: ContextConfig =
serde_json::from_str(&contents).map_err(|e| eyre!("Failed to parse global configuration: {}", e))?;
Ok(config)
} else {
// Return default global configuration with predefined paths
Ok(ContextConfig {
paths: vec![
".amazonq/rules/**/*.md".to_string(),
"README.md".to_string(),
AMAZONQ_FILENAME.to_string(),
],
hooks: HashMap::new(),
})
}
}
/// Load a profile's context configuration.
///
/// If the profile configuration file doesn't exist, creates a default configuration.
async fn load_profile_config(ctx: &Context, profile_name: &str) -> Result<ContextConfig> {
let profile_path = profile_context_path(ctx, profile_name)?;
debug!(?profile_path, "loading profile config");
if ctx.fs().exists(&profile_path) {
let contents = ctx.fs().read_to_string(&profile_path).await?;
let config: ContextConfig =
serde_json::from_str(&contents).map_err(|e| eyre!("Failed to parse profile configuration: {}", e))?;
Ok(config)
} else {
// Return empty configuration for new profiles
Ok(ContextConfig::default())
}
}
/// Process a path, handling glob patterns and file types.
///
/// This method:
/// 1. Expands the path (handling ~ for home directory)
/// 2. If the path contains glob patterns, expands them
/// 3. For each resulting path, adds the file to the context collection
/// 4. Handles directories by including all files in the directory (non-recursive)
/// 5. With force=true, includes paths that don't exist yet
///
/// # Arguments
/// * `path` - The path to process
/// * `context_files` - The collection to add files to
/// * `is_validation` - If true, error when glob patterns don't match; if false, silently skip
///
/// # Returns
/// A Result indicating success or an error
async fn process_path(
ctx: &Context,
path: &str,
context_files: &mut Vec<(String, String)>,
is_validation: bool,
) -> Result<()> {
// Expand ~ to home directory
let expanded_path = if path.starts_with('~') {
if let Some(home_dir) = ctx.env().home() {
home_dir.join(&path[2..]).to_string_lossy().to_string()
} else {
return Err(eyre!("Could not determine home directory"));
}
} else {
path.to_string()
};
// Handle absolute, relative paths, and glob patterns
let full_path = if expanded_path.starts_with('/') {
expanded_path
} else {
ctx.env()
.current_dir()?
.join(&expanded_path)
.to_string_lossy()
.to_string()
};
// Required in chroot testing scenarios so that we can use `Path::exists`.
let full_path = ctx.fs().chroot_path_str(full_path);
// Check if the path contains glob patterns
if full_path.contains('*') || full_path.contains('?') || full_path.contains('[') {
// Expand glob pattern
match glob(&full_path) {
Ok(entries) => {
let mut found_any = false;
for entry in entries {
match entry {
Ok(path) => {
if path.is_file() {
add_file_to_context(ctx, &path, context_files).await?;
found_any = true;
}
},
Err(e) => return Err(eyre!("Glob error: {}", e)),
}
}
if !found_any && is_validation {
// When validating paths (e.g., for /context add), error if no files match
return Err(eyre!("No files found matching glob pattern '{}'", full_path));
}
// When just showing expanded files (e.g., for /context show --expand),
// silently skip non-matching patterns (don't add anything to context_files)
},
Err(e) => return Err(eyre!("Invalid glob pattern '{}': {}", full_path, e)),
}
} else {
// Regular path
let path = Path::new(&full_path);
if path.exists() {
if path.is_file() {
add_file_to_context(ctx, path, context_files).await?;
} else if path.is_dir() {
// For directories, add all files in the directory (non-recursive)
let mut read_dir = ctx.fs().read_dir(path).await?;
while let Some(entry) = read_dir.next_entry().await? {
let path = entry.path();
if path.is_file() {
add_file_to_context(ctx, &path, context_files).await?;
}
}
}
} else if is_validation {
// When validating paths (e.g., for /context add), error if the path doesn't exist
return Err(eyre!("Path '{}' does not exist", full_path));
}
}
Ok(())
}
/// Add a file to the context collection.
///
/// This method:
/// 1. Reads the content of the file
/// 2. Adds the (filename, content) pair to the context collection
///
/// # Arguments
/// * `path` - The path to the file
/// * `context_files` - The collection to add the file to
///
/// # Returns
/// A Result indicating success or an error
async fn add_file_to_context(ctx: &Context, path: &Path, context_files: &mut Vec<(String, String)>) -> Result<()> {
let filename = path.to_string_lossy().to_string();
let content = ctx.fs().read_to_string(path).await?;
context_files.push((filename, content));
Ok(())
}
/// Validate a profile name.
///
/// Profile names can only contain alphanumeric characters, hyphens, and underscores.
///
/// # Arguments
/// * `name` - Name to validate
///
/// # Returns
/// A Result indicating if the name is valid
fn validate_profile_name(name: &str) -> Result<()> {
// Check if name is empty
if name.is_empty() {
return Err(eyre!("Profile name cannot be empty"));
}
// Check if name contains only allowed characters and starts with an alphanumeric character
let re = Regex::new(r"^[a-zA-Z0-9][a-zA-Z0-9_-]*$").unwrap();
if !re.is_match(name) {
return Err(eyre!(
"Profile name must start with an alphanumeric character and can only contain alphanumeric characters, hyphens, and underscores"
));
}
Ok(())
}
fn default_context() -> Arc<Context> {
Context::new()
}
#[cfg(test)]
mod tests {
use std::io::Stdout;
use super::super::hooks::HookTrigger;
use super::*;
// Helper function to create a test ContextManager with Context
pub async fn create_test_context_manager(context_file_size: Option<usize>) -> Result<ContextManager> {
let context_file_size = context_file_size.unwrap_or(CONTEXT_FILES_MAX_SIZE);
let ctx = Context::builder().with_test_home().await.unwrap().build_fake();
let manager = ContextManager::new(ctx, Some(context_file_size)).await?;
Ok(manager)
}
#[tokio::test]
async fn test_validate_profile_name() {
// Test valid names
assert!(validate_profile_name("valid").is_ok());
assert!(validate_profile_name("valid-name").is_ok());
assert!(validate_profile_name("valid_name").is_ok());
assert!(validate_profile_name("valid123").is_ok());
assert!(validate_profile_name("1valid").is_ok());
assert!(validate_profile_name("9test").is_ok());
// Test invalid names
assert!(validate_profile_name("").is_err());
assert!(validate_profile_name("invalid/name").is_err());
assert!(validate_profile_name("invalid.name").is_err());
assert!(validate_profile_name("invalid name").is_err());
assert!(validate_profile_name("_invalid").is_err());
assert!(validate_profile_name("-invalid").is_err());
}
#[tokio::test]
async fn test_profile_ops() -> Result<()> {
let mut manager = create_test_context_manager(None).await?;
let ctx = Arc::clone(&manager.ctx);
assert_eq!(manager.current_profile, "default");
// Create ops
manager.create_profile("test_profile").await?;
assert!(profile_context_path(&ctx, "test_profile")?.exists());
assert!(manager.create_profile("test_profile").await.is_err());
manager.create_profile("alt").await?;
// Listing
let profiles = manager.list_profiles().await?;
assert!(profiles.contains(&"default".to_string()));
assert!(profiles.contains(&"test_profile".to_string()));
assert!(profiles.contains(&"alt".to_string()));
// Switching
manager.switch_profile("test_profile").await?;
assert!(manager.switch_profile("notexists").await.is_err());
// Renaming
manager.rename_profile("alt", "renamed").await?;
assert!(!profile_context_path(&ctx, "alt")?.exists());
assert!(profile_context_path(&ctx, "renamed")?.exists());
// Delete ops
assert!(manager.delete_profile("test_profile").await.is_err());
manager.switch_profile("default").await?;
manager.delete_profile("test_profile").await?;
assert!(!profile_context_path(&ctx, "test_profile")?.exists());
assert!(manager.delete_profile("test_profile").await.is_err());
assert!(manager.delete_profile("default").await.is_err());
Ok(())
}
#[tokio::test]
async fn test_collect_exceeds_limit() -> Result<()> {
let mut manager = create_test_context_manager(Some(2)).await?;
let ctx: Arc<Context> = Arc::clone(&manager.ctx);
ctx.fs().create_dir_all("test").await?;
ctx.fs().write("test/to-include.md", "ha").await?;
ctx.fs()
.write("test/to-drop.md", "long content that exceed limit")
.await?;
manager.add_paths(vec!["test/*.md".to_string()], false, false).await?;
let (used, dropped) = manager.collect_context_files_with_limit().await.unwrap();
assert!(used.len() + dropped.len() == 2);
assert!(used.len() == 1);
assert!(dropped.len() == 1);
Ok(())
}
#[tokio::test]
async fn test_path_ops() -> Result<()> {
let mut manager = create_test_context_manager(None).await?;
let ctx: Arc<Context> = Arc::clone(&manager.ctx);
// Create some test files for matching.
ctx.fs().create_dir_all("test").await?;
ctx.fs().write("test/p1.md", "p1").await?;
ctx.fs().write("test/p2.md", "p2").await?;
assert!(
manager.get_context_files().await?.is_empty(),
"no files should be returned for an empty profile when force is false"
);
manager.add_paths(vec!["test/*.md".to_string()], false, false).await?;
let files = manager.get_context_files().await?;
assert!(files[0].0.ends_with("p1.md"));
assert_eq!(files[0].1, "p1");
assert!(files[1].0.ends_with("p2.md"));
assert_eq!(files[1].1, "p2");
assert!(
manager
.add_paths(vec!["test/*.txt".to_string()], false, false)
.await
.is_err(),
"adding a glob with no matching and without force should fail"
);
Ok(())
}
#[tokio::test]
async fn test_add_hook() -> Result<()> {
let mut manager = create_test_context_manager(None).await?;
let hook = Hook::new_inline_hook(HookTrigger::ConversationStart, "echo test".to_string());
// Test adding hook to profile config
manager.add_hook("test_hook".to_string(), hook.clone(), false).await?;
assert!(manager.profile_config.hooks.contains_key("test_hook"));
// Test adding hook to global config
manager.add_hook("global_hook".to_string(), hook.clone(), true).await?;
assert!(manager.global_config.hooks.contains_key("global_hook"));
// Test adding duplicate hook name
assert!(manager.add_hook("test_hook".to_string(), hook, false).await.is_err());
Ok(())
}
#[tokio::test]
async fn test_remove_hook() -> Result<()> {
let mut manager = create_test_context_manager(None).await?;
let hook = Hook::new_inline_hook(HookTrigger::ConversationStart, "echo test".to_string());
manager.add_hook("test_hook".to_string(), hook, false).await?;
// Test removing existing hook
manager.remove_hook("test_hook", false).await?;
assert!(!manager.profile_config.hooks.contains_key("test_hook"));
// Test removing non-existent hook
assert!(manager.remove_hook("test_hook", false).await.is_err());
Ok(())
}
#[tokio::test]
async fn test_set_hook_disabled() -> Result<()> {
let mut manager = create_test_context_manager(None).await?;
let hook = Hook::new_inline_hook(HookTrigger::ConversationStart, "echo test".to_string());
manager.add_hook("test_hook".to_string(), hook, false).await?;
// Test disabling hook
manager.set_hook_disabled("test_hook", false, true).await?;
assert!(manager.profile_config.hooks.get("test_hook").unwrap().disabled);
// Test enabling hook
manager.set_hook_disabled("test_hook", false, false).await?;
assert!(!manager.profile_config.hooks.get("test_hook").unwrap().disabled);
// Test with non-existent hook
assert!(manager.set_hook_disabled("nonexistent", false, true).await.is_err());
Ok(())
}
#[tokio::test]
async fn test_set_all_hooks_disabled() -> Result<()> {
let mut manager = create_test_context_manager(None).await?;