Remove files from the chat session to free up context space
(self, args="")
| 900 | return all_files |
| 901 | |
| 902 | def cmd_drop(self, args=""): |
| 903 | "Remove files from the chat session to free up context space" |
| 904 | |
| 905 | if not args.strip(): |
| 906 | if self.original_read_only_fnames: |
| 907 | self.io.tool_output( |
| 908 | "Dropping all files from the chat session except originally read-only files." |
| 909 | ) |
| 910 | else: |
| 911 | self.io.tool_output("Dropping all files from the chat session.") |
| 912 | self._drop_all_files() |
| 913 | return |
| 914 | |
| 915 | filenames = parse_quoted_filenames(args) |
| 916 | for word in filenames: |
| 917 | # Expand tilde in the path |
| 918 | expanded_word = os.path.expanduser(word) |
| 919 | |
| 920 | # Handle read-only files with substring matching and samefile check |
| 921 | read_only_matched = [] |
| 922 | for f in self.coder.abs_read_only_fnames: |
| 923 | if expanded_word in f: |
| 924 | read_only_matched.append(f) |
| 925 | continue |
| 926 | |
| 927 | # Try samefile comparison for relative paths |
| 928 | try: |
| 929 | abs_word = os.path.abspath(expanded_word) |
| 930 | if os.path.samefile(abs_word, f): |
| 931 | read_only_matched.append(f) |
| 932 | except (FileNotFoundError, OSError): |
| 933 | continue |
| 934 | |
| 935 | for matched_file in read_only_matched: |
| 936 | self.coder.abs_read_only_fnames.remove(matched_file) |
| 937 | self.io.tool_output(f"Removed read-only file {matched_file} from the chat") |
| 938 | |
| 939 | # For editable files, use glob if word contains glob chars, otherwise use substring |
| 940 | if any(c in expanded_word for c in "*?[]"): |
| 941 | matched_files = self.glob_filtered_to_repo(expanded_word) |
| 942 | else: |
| 943 | # Use substring matching like we do for read-only files |
| 944 | matched_files = [ |
| 945 | self.coder.get_rel_fname(f) for f in self.coder.abs_fnames if expanded_word in f |
| 946 | ] |
| 947 | |
| 948 | if not matched_files: |
| 949 | matched_files.append(expanded_word) |
| 950 | |
| 951 | for matched_file in matched_files: |
| 952 | abs_fname = self.coder.abs_root_path(matched_file) |
| 953 | if abs_fname in self.coder.abs_fnames: |
| 954 | self.coder.abs_fnames.remove(abs_fname) |
| 955 | self.io.tool_output(f"Removed {matched_file} from the chat") |
| 956 | |
| 957 | def cmd_git(self, args): |
| 958 | "Run a git command (output excluded from chat)" |