feat: Add file export functionality to export chat context

This commit is contained in:
Lutz Leonhardt 2025-03-09 00:22:04 +01:00
parent 7aca65dd20
commit fa25e96c21

View file

@ -1464,7 +1464,7 @@ Just show me the edits I need to make.
export_dir = os.path.join(tempfile.gettempdir(), f"export_{timestamp}") export_dir = os.path.join(tempfile.gettempdir(), f"export_{timestamp}")
os.makedirs(export_dir, exist_ok=True) os.makedirs(export_dir, exist_ok=True)
return export_dir return export_dir
def _get_context_files(self): def _get_context_files(self):
"""Get all files in the chat context (both editable and read-only)""" """Get all files in the chat context (both editable and read-only)"""
inchat_files = self.coder.get_inchat_relative_files() inchat_files = self.coder.get_inchat_relative_files()
@ -1472,19 +1472,53 @@ Just show me the edits I need to make.
all_files = sorted(set(inchat_files + read_only_files)) all_files = sorted(set(inchat_files + read_only_files))
return all_files return all_files
def _export_flat(self, files, export_dir):
"""Export each file to its own file with flattened path"""
import os
count = 0
for rel_fname in files:
# Get content
abs_fname = self.coder.abs_root_path(rel_fname)
content = self.io.read_text(abs_fname)
if content is None:
self.io.tool_warning(f"Could not read {rel_fname}, skipping")
continue
# Flatten filename
flat_fname = rel_fname.replace('/', '|').replace('\\', '|')
out_path = os.path.join(export_dir, flat_fname)
# Write content
try:
with open(out_path, 'w', encoding=self.io.encoding) as f:
f.write(content)
count += 1
except Exception as e:
self.io.tool_error(f"Error writing {flat_fname}: {str(e)}")
return count
def cmd_export_context(self, args): def cmd_export_context(self, args):
"Export files in chat context to a timestamped directory" "Export files in chat context to a timestamped directory"
# Parse arguments (we'll implement chunking later)
chunk_size = None
# Get files in context
all_files = self._get_context_files() all_files = self._get_context_files()
if not all_files: if not all_files:
self.io.tool_output("No files in context to export.") self.io.tool_output("No files in context to export.")
return return
# Create timestamped directory
export_dir = self._create_export_directory() export_dir = self._create_export_directory()
self.io.tool_output(f"Will export {len(all_files)} files to {export_dir}")
for fname in all_files: # Export files in flat mode
self.io.tool_output(f" {fname}") count = self._export_flat(all_files, export_dir)
return
self.io.tool_output(f"Exported {count} files to {export_dir}/")
def expand_subdir(file_path): def expand_subdir(file_path):