janito 0.4.0__py3-none-any.whl → 0.6.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- janito/__init__.py +1 -1
- janito/__main__.py +102 -326
- janito/agents/__init__.py +16 -0
- janito/agents/agent.py +21 -0
- janito/{claude.py → agents/claudeai.py} +13 -17
- janito/agents/openai.py +53 -0
- janito/agents/test.py +34 -0
- janito/change/__init__.py +32 -0
- janito/change/__main__.py +0 -0
- janito/change/analysis/__init__.py +23 -0
- janito/change/analysis/__main__.py +7 -0
- janito/change/analysis/analyze.py +61 -0
- janito/change/analysis/formatting.py +78 -0
- janito/change/analysis/options.py +81 -0
- janito/change/analysis/prompts.py +98 -0
- janito/change/analysis/view/__init__.py +9 -0
- janito/change/analysis/view/terminal.py +171 -0
- janito/change/applier/__init__.py +5 -0
- janito/change/applier/file.py +58 -0
- janito/change/applier/main.py +156 -0
- janito/change/applier/text.py +245 -0
- janito/change/applier/workspace_dir.py +58 -0
- janito/change/core.py +131 -0
- janito/change/history.py +44 -0
- janito/change/operations.py +7 -0
- janito/change/parser.py +289 -0
- janito/change/play.py +54 -0
- janito/change/preview.py +82 -0
- janito/change/prompts.py +126 -0
- janito/change/test.py +0 -0
- janito/change/validator.py +251 -0
- janito/change/viewer/__init__.py +11 -0
- janito/change/viewer/content.py +66 -0
- janito/change/viewer/diff.py +43 -0
- janito/change/viewer/pager.py +56 -0
- janito/change/viewer/panels.py +555 -0
- janito/change/viewer/styling.py +103 -0
- janito/change/viewer/themes.py +55 -0
- janito/clear_statement_parser/clear_statement_format.txt +328 -0
- janito/clear_statement_parser/examples.txt +326 -0
- janito/clear_statement_parser/models.py +104 -0
- janito/clear_statement_parser/parser.py +496 -0
- janito/cli/__init__.py +2 -0
- janito/cli/base.py +30 -0
- janito/cli/commands.py +45 -0
- janito/cli/functions.py +111 -0
- janito/cli/handlers/ask.py +22 -0
- janito/cli/handlers/demo.py +22 -0
- janito/cli/handlers/request.py +24 -0
- janito/cli/handlers/scan.py +9 -0
- janito/cli/history.py +61 -0
- janito/cli/registry.py +26 -0
- janito/common.py +41 -10
- janito/config.py +71 -6
- janito/demo/__init__.py +4 -0
- janito/demo/data.py +13 -0
- janito/demo/mock_data.py +20 -0
- janito/demo/operations.py +45 -0
- janito/demo/runner.py +59 -0
- janito/demo/scenarios.py +32 -0
- janito/prompts.py +1 -65
- janito/qa.py +8 -5
- janito/review.py +13 -0
- janito/search_replace/README.md +146 -0
- janito/search_replace/__init__.py +6 -0
- janito/search_replace/__main__.py +21 -0
- janito/search_replace/core.py +119 -0
- janito/search_replace/parser.py +52 -0
- janito/search_replace/play.py +61 -0
- janito/search_replace/replacer.py +36 -0
- janito/search_replace/searcher.py +299 -0
- janito/shell/__init__.py +39 -0
- janito/shell/bus.py +31 -0
- janito/shell/commands.py +195 -0
- janito/shell/handlers.py +122 -0
- janito/shell/history.py +20 -0
- janito/shell/processor.py +52 -0
- janito/tui/__init__.py +21 -0
- janito/tui/base.py +22 -0
- janito/tui/flows/__init__.py +5 -0
- janito/tui/flows/changes.py +65 -0
- janito/tui/flows/content.py +128 -0
- janito/tui/flows/selection.py +117 -0
- janito/tui/screens/__init__.py +3 -0
- janito/tui/screens/app.py +1 -0
- janito/workspace/__init__.py +7 -0
- janito/workspace/analysis.py +121 -0
- janito/workspace/manager.py +48 -0
- janito/workspace/scan.py +232 -0
- janito-0.6.0.dist-info/METADATA +185 -0
- janito-0.6.0.dist-info/RECORD +95 -0
- {janito-0.4.0.dist-info → janito-0.6.0.dist-info}/WHEEL +1 -1
- janito/analysis.py +0 -281
- janito/changeapplier.py +0 -436
- janito/changeviewer.py +0 -350
- janito/console.py +0 -330
- janito/contentchange.py +0 -84
- janito/contextparser.py +0 -113
- janito/fileparser.py +0 -125
- janito/scan.py +0 -137
- janito-0.4.0.dist-info/METADATA +0 -164
- janito-0.4.0.dist-info/RECORD +0 -21
- {janito-0.4.0.dist-info → janito-0.6.0.dist-info}/entry_points.txt +0 -0
- {janito-0.4.0.dist-info → janito-0.6.0.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,156 @@
|
|
1
|
+
"""
|
2
|
+
Applies file changes to preview directory and runs tests
|
3
|
+
|
4
|
+
The following situations should result in error:
|
5
|
+
- Creating a file that already exists
|
6
|
+
- Replace operation on a non-existent file
|
7
|
+
- Rename operation on a non-existent file
|
8
|
+
- Modify operation with search text not found
|
9
|
+
- No changes applied to a file
|
10
|
+
"""
|
11
|
+
|
12
|
+
from pathlib import Path
|
13
|
+
from typing import Tuple, Optional, List, Set
|
14
|
+
from rich.console import Console
|
15
|
+
from rich.panel import Panel
|
16
|
+
from rich import box
|
17
|
+
import subprocess
|
18
|
+
from ..validator import validate_python_syntax
|
19
|
+
from .workspace_dir import apply_changes as apply_to_workspace_dir_impl
|
20
|
+
from janito.config import config
|
21
|
+
from .file import FileChangeApplier
|
22
|
+
from .text import TextChangeApplier
|
23
|
+
from ..parser import FileChange, ChangeOperation
|
24
|
+
from ..validator import validate_all_changes
|
25
|
+
|
26
|
+
|
27
|
+
class ChangeApplier:
|
28
|
+
"""Handles applying changes to files."""
|
29
|
+
|
30
|
+
def __init__(self, preview_dir: Path, debug: bool = False):
|
31
|
+
self.preview_dir = preview_dir
|
32
|
+
self.debug = debug
|
33
|
+
self.console = Console()
|
34
|
+
self.file_applier = FileChangeApplier(preview_dir, self.console)
|
35
|
+
self.text_applier = TextChangeApplier(self.console)
|
36
|
+
|
37
|
+
def run_test_command(self, test_cmd: str) -> Tuple[bool, str, Optional[str]]:
|
38
|
+
"""Run test command in preview directory.
|
39
|
+
Returns (success, output, error)"""
|
40
|
+
try:
|
41
|
+
result = subprocess.run(
|
42
|
+
test_cmd,
|
43
|
+
shell=True,
|
44
|
+
cwd=self.preview_dir,
|
45
|
+
capture_output=True,
|
46
|
+
text=True,
|
47
|
+
timeout=300 # 5 minute timeout
|
48
|
+
)
|
49
|
+
return (
|
50
|
+
result.returncode == 0,
|
51
|
+
result.stdout,
|
52
|
+
result.stderr if result.returncode != 0 else None
|
53
|
+
)
|
54
|
+
except subprocess.TimeoutExpired:
|
55
|
+
return False, "", "Test command timed out after 5 minutes"
|
56
|
+
except Exception as e:
|
57
|
+
return False, "", f"Error running test: {str(e)}"
|
58
|
+
|
59
|
+
def apply_changes(self, changes: List[FileChange], debug: bool = None) -> tuple[bool, Set[Path]]:
|
60
|
+
"""Apply changes in preview directory, runs tests if specified.
|
61
|
+
Returns (success, modified_files)"""
|
62
|
+
debug = debug if debug is not None else self.debug
|
63
|
+
console = Console()
|
64
|
+
|
65
|
+
# Validate all changes using consolidated validator
|
66
|
+
is_valid, error = validate_all_changes(changes, set(Path(c.name) for c in changes))
|
67
|
+
if not is_valid:
|
68
|
+
console.print(f"\n[red]{error}[/red]")
|
69
|
+
return False, set()
|
70
|
+
|
71
|
+
# Track modified files and apply changes
|
72
|
+
modified_files: Set[Path] = set()
|
73
|
+
for change in changes:
|
74
|
+
if config.verbose:
|
75
|
+
console.print(f"[dim]Previewing changes for {change.name}...[/dim]")
|
76
|
+
success, error = self.apply_single_change(change, debug)
|
77
|
+
if not success:
|
78
|
+
console.print(f"\n[red]Error previewing {change.name}: {error}[/red]")
|
79
|
+
return False, modified_files
|
80
|
+
if not change.operation == 'remove_file':
|
81
|
+
modified_files.add(change.name)
|
82
|
+
elif change.operation == 'rename_file':
|
83
|
+
modified_files.add(change.target)
|
84
|
+
|
85
|
+
# Validate Python syntax (skip deleted and moved files)
|
86
|
+
python_files = {f for f in modified_files if f.suffix == '.py'}
|
87
|
+
|
88
|
+
for change in changes:
|
89
|
+
if change.operation == ChangeOperation.REMOVE_FILE:
|
90
|
+
python_files.discard(change.name) # Skip validation for deleted files
|
91
|
+
elif change.operation in (ChangeOperation.RENAME_FILE, ChangeOperation.MOVE_FILE):
|
92
|
+
python_files.discard(change.source) # Skip validation for moved/renamed sources
|
93
|
+
|
94
|
+
for path in python_files:
|
95
|
+
preview_path = self.preview_dir / path
|
96
|
+
is_valid, error_msg = validate_python_syntax(preview_path.read_text(), preview_path)
|
97
|
+
if not is_valid:
|
98
|
+
console.print(f"\n[red]Python syntax validation failed for {path}:[/red]")
|
99
|
+
console.print(f"[red]{error_msg}[/red]")
|
100
|
+
return False, modified_files
|
101
|
+
|
102
|
+
# Show success message with syntax validation status
|
103
|
+
console.print("\n[cyan]Changes applied successfully.[/cyan]")
|
104
|
+
if python_files:
|
105
|
+
console.print(f"[green]✓ Python syntax validated for {len(python_files)} file(s)[/green]")
|
106
|
+
|
107
|
+
# Run tests if specified
|
108
|
+
if config.test_cmd:
|
109
|
+
console.print(f"\n[cyan]Testing changes in preview directory:[/cyan] {config.test_cmd}")
|
110
|
+
success, output, error = self.run_test_command(config.test_cmd)
|
111
|
+
if output:
|
112
|
+
console.print("\n[bold]Test Output:[/bold]")
|
113
|
+
console.print(Panel(output, box=box.ROUNDED))
|
114
|
+
if not success:
|
115
|
+
console.print("\n[red bold]Tests failed in preview.[/red bold]")
|
116
|
+
if error:
|
117
|
+
console.print(Panel(error, title="Error", border_style="red"))
|
118
|
+
return False, modified_files
|
119
|
+
|
120
|
+
return True, modified_files
|
121
|
+
|
122
|
+
def apply_single_change(self, change: FileChange, debug: bool) -> Tuple[bool, Optional[str]]:
|
123
|
+
"""Apply a single file change to preview directory"""
|
124
|
+
path = self.preview_dir / change.name # Changed back from path to name
|
125
|
+
|
126
|
+
# Handle file operations first
|
127
|
+
if change.operation != ChangeOperation.MODIFY_FILE:
|
128
|
+
return self.file_applier.apply_file_operation(change)
|
129
|
+
|
130
|
+
# Handle text modifications
|
131
|
+
if not path.exists():
|
132
|
+
original_path = Path(change.name) # Changed back from path to name
|
133
|
+
if not original_path.exists():
|
134
|
+
return False, f"Original file not found: {original_path}"
|
135
|
+
if self.console:
|
136
|
+
self.console.print(f"[dim]Copying {original_path} to preview directory {path}[/dim]")
|
137
|
+
path.write_text(original_path.read_text())
|
138
|
+
|
139
|
+
current_content = path.read_text()
|
140
|
+
success, modified_content, error = self.text_applier.apply_modifications(
|
141
|
+
current_content,
|
142
|
+
change.text_changes,
|
143
|
+
path,
|
144
|
+
debug
|
145
|
+
)
|
146
|
+
|
147
|
+
if not success:
|
148
|
+
return False, error
|
149
|
+
|
150
|
+
path.write_text(modified_content)
|
151
|
+
return True, None
|
152
|
+
|
153
|
+
def apply_to_workspace_dir(self, changes: List[FileChange], debug: bool = None) -> bool:
|
154
|
+
"""Apply changes from preview to working directory"""
|
155
|
+
debug = debug if debug is not None else self.debug
|
156
|
+
return apply_to_workspace_dir_impl(changes, self.preview_dir, Console())
|
@@ -0,0 +1,245 @@
|
|
1
|
+
from typing import Tuple, List, Optional
|
2
|
+
from rich.console import Console
|
3
|
+
from pathlib import Path
|
4
|
+
from datetime import datetime
|
5
|
+
from ..parser import TextChange
|
6
|
+
from janito.config import config
|
7
|
+
from ...clear_statement_parser.parser import StatementParser
|
8
|
+
from ...search_replace import SearchReplacer, PatternNotFoundException, Searcher
|
9
|
+
|
10
|
+
class TextFindDebugger:
|
11
|
+
def __init__(self, console: Console):
|
12
|
+
self.console = console
|
13
|
+
self.find_count = 0
|
14
|
+
|
15
|
+
def _visualize_whitespace(self, text: str) -> str:
|
16
|
+
"""Convert whitespace characters to visible markers"""
|
17
|
+
return text.replace(' ', '·').replace('\t', '→')
|
18
|
+
|
19
|
+
def debug_find(self, content: str, search: str) -> List[int]:
|
20
|
+
"""Debug find operation by showing numbered matches"""
|
21
|
+
self.find_count += 1
|
22
|
+
matches = []
|
23
|
+
|
24
|
+
# Show search pattern
|
25
|
+
self.console.print(f"\n[cyan]Find #{self.find_count} search pattern:[/cyan]")
|
26
|
+
for i, line in enumerate(search.splitlines()):
|
27
|
+
self.console.print(f"[dim]{i+1:3d} | {self._visualize_whitespace(line)}[/dim]")
|
28
|
+
|
29
|
+
# Process content line by line
|
30
|
+
lines = content.splitlines()
|
31
|
+
for i, line in enumerate(lines):
|
32
|
+
if search.strip() in line.strip():
|
33
|
+
matches.append(i + 1)
|
34
|
+
self.console.print(f"[green]Match at line {i+1}:[/green] {self._visualize_whitespace(line)}")
|
35
|
+
|
36
|
+
if not matches:
|
37
|
+
self.console.print("[yellow]No matches found[/yellow]")
|
38
|
+
|
39
|
+
return matches
|
40
|
+
|
41
|
+
class TextChangeApplier:
|
42
|
+
def __init__(self, console: Optional[Console] = None):
|
43
|
+
self.console = console or Console()
|
44
|
+
self.debugger = TextFindDebugger(self.console)
|
45
|
+
self.parser = StatementParser()
|
46
|
+
self.searcher = Searcher()
|
47
|
+
|
48
|
+
def _get_last_line_indent(self, content: str) -> str:
|
49
|
+
"""Extract indentation from the last non-empty line."""
|
50
|
+
lines = content.splitlines()
|
51
|
+
for line in reversed(lines):
|
52
|
+
if line.strip():
|
53
|
+
return self.searcher.get_indentation(line)
|
54
|
+
return ""
|
55
|
+
|
56
|
+
def _validate_operation(self, mod: TextChange) -> Tuple[bool, Optional[str]]:
|
57
|
+
"""Validate text operation type and parameters
|
58
|
+
Returns (is_valid, error_message)"""
|
59
|
+
if mod.is_append:
|
60
|
+
if not mod.replace_content:
|
61
|
+
return False, "Append operation requires content"
|
62
|
+
return True, None
|
63
|
+
|
64
|
+
# For delete operations
|
65
|
+
if mod.is_delete:
|
66
|
+
if not mod.search_content:
|
67
|
+
return False, "Delete operation requires search content"
|
68
|
+
return True, None
|
69
|
+
|
70
|
+
# For replace operations
|
71
|
+
if not mod.search_content:
|
72
|
+
return False, "Replace operation requires search content"
|
73
|
+
if mod.replace_content is None:
|
74
|
+
return False, "Replace operation requires replacement content"
|
75
|
+
|
76
|
+
return True, None
|
77
|
+
|
78
|
+
def apply_modifications(self, content: str, changes: List[TextChange], target_path: Path, debug: bool) -> Tuple[bool, str, Optional[str]]:
|
79
|
+
"""Apply text modifications to content"""
|
80
|
+
modified = content
|
81
|
+
any_changes = False
|
82
|
+
target_path = target_path.resolve()
|
83
|
+
file_ext = target_path.suffix # Get file extension including the dot
|
84
|
+
|
85
|
+
for mod in changes:
|
86
|
+
|
87
|
+
# Validate operation
|
88
|
+
is_valid, error = self._validate_operation(mod)
|
89
|
+
if not is_valid:
|
90
|
+
return False, content, f"Invalid text operation for {target_path}: {error}"
|
91
|
+
|
92
|
+
try:
|
93
|
+
# Handle append operations
|
94
|
+
if not mod.search_content:
|
95
|
+
if mod.replace_content:
|
96
|
+
modified = self._append_content(modified, mod.replace_content)
|
97
|
+
any_changes = True
|
98
|
+
continue
|
99
|
+
|
100
|
+
# Handle delete operations (either explicit or via empty replacement)
|
101
|
+
if mod.is_delete or (mod.replace_content == "" and mod.search_content):
|
102
|
+
replacer = SearchReplacer(modified, mod.search_content, "", file_ext, debug=debug)
|
103
|
+
|
104
|
+
modified = replacer.replace()
|
105
|
+
any_changes = True
|
106
|
+
continue
|
107
|
+
|
108
|
+
# Handle search and replace
|
109
|
+
if debug:
|
110
|
+
print("************************** before replace")
|
111
|
+
print(modified)
|
112
|
+
print("****************************")
|
113
|
+
replacer = SearchReplacer(modified, mod.search_content, mod.replace_content, file_ext, debug=debug)
|
114
|
+
modified = replacer.replace()
|
115
|
+
if debug:
|
116
|
+
print("************************** after replace")
|
117
|
+
print(modified)
|
118
|
+
print("****************************")
|
119
|
+
any_changes = True
|
120
|
+
|
121
|
+
except PatternNotFoundException:
|
122
|
+
if config.debug:
|
123
|
+
self.debug_failed_finds(mod.search_content, modified, str(target_path))
|
124
|
+
return False, content, self._handle_failed_search(target_path, mod.search_content, modified)
|
125
|
+
|
126
|
+
return (True, modified, None) if any_changes else (False, content, "No changes were applied")
|
127
|
+
|
128
|
+
def _append_content(self, content: str, new_content: str) -> str:
|
129
|
+
"""Append content with proper indentation matching.
|
130
|
+
|
131
|
+
The indentation rules are:
|
132
|
+
1. If new content starts with empty lines, preserve original indentation
|
133
|
+
2. Otherwise, use indentation from the last non-empty line of existing content as base
|
134
|
+
3. Preserves relative indentation between lines in new content
|
135
|
+
4. Adjusts indentation if new content would go into negative space
|
136
|
+
"""
|
137
|
+
if not content.endswith('\n'):
|
138
|
+
content += '\n'
|
139
|
+
|
140
|
+
# Add empty line if the last line is not empty
|
141
|
+
if content.rstrip('\n').splitlines()[-1].strip():
|
142
|
+
content += '\n'
|
143
|
+
|
144
|
+
# If new content starts with empty lines, preserve original indentation
|
145
|
+
lines = new_content.splitlines()
|
146
|
+
if not lines or not lines[0].strip():
|
147
|
+
return content + new_content
|
148
|
+
|
149
|
+
# Get base indentation from last non-empty line
|
150
|
+
base_indent = self._get_last_line_indent(content)
|
151
|
+
|
152
|
+
# Get the first non-empty line from new content
|
153
|
+
first_line, _ = self.searcher.get_first_non_empty_line(new_content)
|
154
|
+
if first_line:
|
155
|
+
# Get the indentation of the first line of new content
|
156
|
+
new_base_indent = self.searcher.get_indentation(first_line)
|
157
|
+
|
158
|
+
# Calculate how much we need to shift if new content would go into negative space
|
159
|
+
indent_delta = len(base_indent) + (len(new_base_indent) - len(new_base_indent))
|
160
|
+
left_shift = abs(min(0, indent_delta))
|
161
|
+
|
162
|
+
result_lines = []
|
163
|
+
for line in new_content.splitlines():
|
164
|
+
if not line.strip():
|
165
|
+
result_lines.append('')
|
166
|
+
continue
|
167
|
+
|
168
|
+
# Calculate final indentation:
|
169
|
+
# 1. Get current line's indentation
|
170
|
+
line_indent = self.searcher.get_indentation(line)
|
171
|
+
# 2. Calculate relative indent compared to new content's first line
|
172
|
+
rel_indent = len(line_indent) - len(new_base_indent)
|
173
|
+
# 3. Apply base indent + relative indent, adjusting for negative space
|
174
|
+
final_indent_len = max(0, len(line_indent) - left_shift + (len(base_indent) - len(new_base_indent)))
|
175
|
+
final_indent = ' ' * final_indent_len
|
176
|
+
result_lines.append(final_indent + line.lstrip())
|
177
|
+
|
178
|
+
new_content = '\n'.join(result_lines)
|
179
|
+
|
180
|
+
return content + new_content
|
181
|
+
|
182
|
+
def _handle_failed_search(self, filepath: Path, search_text: str, content: str) -> str:
|
183
|
+
"""Handle failed search by logging debug info in a test case format"""
|
184
|
+
failed_file = config.workspace_dir / '.janito' / 'change_history' / f"{datetime.now().strftime('%Y%m%d_%H%M%S')}_changes_failed.txt"
|
185
|
+
failed_file.parent.mkdir(parents=True, exist_ok=True)
|
186
|
+
|
187
|
+
# Create test case format debug info
|
188
|
+
debug_info = f"""Test: Failed search in {filepath.name}
|
189
|
+
========================================
|
190
|
+
Original:
|
191
|
+
{content}
|
192
|
+
========================================
|
193
|
+
Search pattern:
|
194
|
+
{search_text}
|
195
|
+
========================================"""
|
196
|
+
|
197
|
+
failed_file.write_text(debug_info)
|
198
|
+
|
199
|
+
self.console.print(f"\n[red]Failed search saved to: {failed_file}[/red]")
|
200
|
+
self.console.print("[yellow]Run with 'python -m janito.search_replace {failed_file}' to debug[/yellow]")
|
201
|
+
|
202
|
+
return f"Could not find search text in {filepath}"
|
203
|
+
|
204
|
+
def debug_failed_finds(self, search_content: str, file_content: str, filepath: str) -> None:
|
205
|
+
"""Debug find operations without applying changes"""
|
206
|
+
if not search_content or not file_content:
|
207
|
+
self.console.print("[yellow]Missing search or file content for debugging[/yellow]")
|
208
|
+
return
|
209
|
+
|
210
|
+
self.console.print(f"\n[cyan]Debugging finds for {filepath}:[/cyan]")
|
211
|
+
self.debugger.debug_find(file_content, search_content)
|
212
|
+
|
213
|
+
def extract_debug_info(self, content: str) -> tuple[Optional[str], Optional[str], Optional[str]]:
|
214
|
+
"""Extract search text and file content from failed change debug info.
|
215
|
+
|
216
|
+
Only matches section markers ("========================================")
|
217
|
+
when they appear alone on a line.
|
218
|
+
"""
|
219
|
+
try:
|
220
|
+
marker = "=" * 40
|
221
|
+
lines = content.splitlines()
|
222
|
+
section_starts = [i for i, line in enumerate(lines) if line.strip() == marker]
|
223
|
+
|
224
|
+
if len(section_starts) < 3:
|
225
|
+
raise ValueError("Missing section markers in debug file")
|
226
|
+
|
227
|
+
# Extract content between markers
|
228
|
+
original_start = section_starts[0] + 2 # +1 for section header, +1 for marker
|
229
|
+
search_start = section_starts[1] + 2
|
230
|
+
original_content = "\n".join(lines[original_start:section_starts[1]])
|
231
|
+
search_content = "\n".join(lines[search_start:section_starts[2]])
|
232
|
+
|
233
|
+
# Extract filename from first line
|
234
|
+
if not lines[0].startswith("Test: Failed search in "):
|
235
|
+
raise ValueError("Invalid debug file format")
|
236
|
+
filepath = lines[0].replace("Test: Failed search in ", "").strip()
|
237
|
+
|
238
|
+
if not all([filepath, search_content, original_content]):
|
239
|
+
raise ValueError("Missing required sections in debug file")
|
240
|
+
|
241
|
+
return filepath, search_content, original_content
|
242
|
+
|
243
|
+
except Exception as e:
|
244
|
+
self.console.print(f"[red]Error parsing debug info: {e}[/red]")
|
245
|
+
return None, None, None
|
@@ -0,0 +1,58 @@
|
|
1
|
+
from pathlib import Path
|
2
|
+
from typing import Set, List
|
3
|
+
import shutil
|
4
|
+
from rich.console import Console
|
5
|
+
from janito.config import config
|
6
|
+
from ..parser import FileChange, ChangeOperation
|
7
|
+
|
8
|
+
def verify_changes(changes: List[FileChange]) -> tuple[bool, str]:
|
9
|
+
"""Verify changes can be safely applied to workspace_dir.
|
10
|
+
Returns (is_safe, error_message)."""
|
11
|
+
for change in changes:
|
12
|
+
source_path = config.workspace_dir / change.name
|
13
|
+
|
14
|
+
if change.operation == ChangeOperation.CREATE_FILE:
|
15
|
+
if source_path.exists():
|
16
|
+
return False, f"Cannot create {change.name} - already exists"
|
17
|
+
|
18
|
+
elif change.operation in (ChangeOperation.MOVE_FILE, ChangeOperation.RENAME_FILE):
|
19
|
+
if not source_path.exists():
|
20
|
+
return False, f"Cannot {change.operation.name.lower()} non-existent file {change.name}"
|
21
|
+
target_path = config.workspace_dir / change.target
|
22
|
+
if target_path.exists():
|
23
|
+
return False, f"Cannot {change.operation.name.lower()} {change.name} to {change.target} - target already exists"
|
24
|
+
|
25
|
+
|
26
|
+
return True, ""
|
27
|
+
|
28
|
+
def apply_changes(changes: List[FileChange], preview_dir: Path, console: Console) -> bool:
|
29
|
+
"""Apply all changes from preview to workspace_dir.
|
30
|
+
Returns success status."""
|
31
|
+
is_safe, error = verify_changes(changes)
|
32
|
+
if not is_safe:
|
33
|
+
console.print(f"[red]Error: {error}[/red]")
|
34
|
+
return False
|
35
|
+
|
36
|
+
console.print("\n[blue]Applying changes to working directory...[/blue]")
|
37
|
+
|
38
|
+
for change in changes:
|
39
|
+
if change.operation == ChangeOperation.REMOVE_FILE:
|
40
|
+
remove_from_workspace_dir(change.name, console)
|
41
|
+
else:
|
42
|
+
filepath = change.target if change.operation == ChangeOperation.RENAME_FILE else change.name
|
43
|
+
target_path = config.workspace_dir / filepath
|
44
|
+
preview_path = preview_dir / filepath
|
45
|
+
|
46
|
+
target_path.parent.mkdir(parents=True, exist_ok=True)
|
47
|
+
if preview_path.exists():
|
48
|
+
shutil.copy2(preview_path, target_path)
|
49
|
+
console.print(f"[dim]Applied changes to {filepath}[/dim]")
|
50
|
+
|
51
|
+
return True
|
52
|
+
|
53
|
+
def remove_from_workspace_dir(filepath: Path, console: Console) -> None:
|
54
|
+
"""Remove file from working directory"""
|
55
|
+
target_path = config.workspace_dir / filepath
|
56
|
+
if target_path.exists():
|
57
|
+
target_path.unlink()
|
58
|
+
console.print(f"[red]Removed {filepath}[/red]")
|
janito/change/core.py
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
from pathlib import Path
|
2
|
+
from typing import Optional, Tuple, Optional, List
|
3
|
+
from rich.console import Console
|
4
|
+
from rich.prompt import Confirm
|
5
|
+
from rich.panel import Panel
|
6
|
+
from rich.columns import Columns
|
7
|
+
from rich import box
|
8
|
+
|
9
|
+
from janito.common import progress_send_message
|
10
|
+
from janito.change.history import save_changes_to_history
|
11
|
+
from janito.config import config
|
12
|
+
from janito.workspace.scan import collect_files_content
|
13
|
+
from .viewer import preview_all_changes
|
14
|
+
from janito.workspace.analysis import analyze_workspace_content as show_content_stats
|
15
|
+
|
16
|
+
from . import (
|
17
|
+
build_change_request_prompt,
|
18
|
+
parse_response,
|
19
|
+
setup_workspace_dir_preview,
|
20
|
+
ChangeApplier
|
21
|
+
)
|
22
|
+
|
23
|
+
from .analysis import analyze_request
|
24
|
+
|
25
|
+
def process_change_request(
|
26
|
+
request: str,
|
27
|
+
preview_only: bool = False,
|
28
|
+
debug: bool = False
|
29
|
+
) -> Tuple[bool, Optional[Path]]:
|
30
|
+
"""
|
31
|
+
Process a change request through the main flow.
|
32
|
+
Return:
|
33
|
+
success: True if the request was processed successfully
|
34
|
+
history_file: Path to the saved history file
|
35
|
+
"""
|
36
|
+
console = Console()
|
37
|
+
paths_to_scan = config.include if config.include else [config.workspace_dir]
|
38
|
+
|
39
|
+
content_xml = collect_files_content(paths_to_scan)
|
40
|
+
|
41
|
+
# Show workspace content preview
|
42
|
+
show_content_stats(content_xml)
|
43
|
+
|
44
|
+
analysis = analyze_request(request, content_xml)
|
45
|
+
if not analysis:
|
46
|
+
console.print("[red]Analysis failed or interrupted[/red]")
|
47
|
+
return False, None
|
48
|
+
|
49
|
+
prompt = build_change_request_prompt(request, analysis.format_option_text(), content_xml)
|
50
|
+
response = progress_send_message(prompt)
|
51
|
+
if not response:
|
52
|
+
console.print("[red]Failed to get response from AI[/red]")
|
53
|
+
return False, None
|
54
|
+
|
55
|
+
history_file = save_changes_to_history(response, request)
|
56
|
+
|
57
|
+
# Parse changes
|
58
|
+
changes = parse_response(response)
|
59
|
+
if not changes:
|
60
|
+
console.print("[yellow]No changes found in response[/yellow]")
|
61
|
+
return False, None
|
62
|
+
|
63
|
+
# Extract response info after END_OF_INSTRUCTIONS
|
64
|
+
response_info = extract_response_info(response)
|
65
|
+
|
66
|
+
# Show request and response info in panels
|
67
|
+
request_panel = Panel(
|
68
|
+
request,
|
69
|
+
title="User Request",
|
70
|
+
border_style="cyan",
|
71
|
+
box=box.ROUNDED
|
72
|
+
)
|
73
|
+
response_panel = Panel(
|
74
|
+
response_info if response_info else "No additional information provided",
|
75
|
+
title="Response Information",
|
76
|
+
border_style="green",
|
77
|
+
box=box.ROUNDED
|
78
|
+
)
|
79
|
+
|
80
|
+
# Display panels side by side
|
81
|
+
columns = Columns([request_panel, response_panel], equal=True, expand=True)
|
82
|
+
console.print("\n")
|
83
|
+
console.print(columns)
|
84
|
+
console.print("\n")
|
85
|
+
|
86
|
+
if preview_only:
|
87
|
+
preview_all_changes(console, changes)
|
88
|
+
return True, history_file
|
89
|
+
|
90
|
+
# Create preview directory and apply changes
|
91
|
+
_, preview_dir = setup_workspace_dir_preview()
|
92
|
+
applier = ChangeApplier(preview_dir, debug=debug)
|
93
|
+
|
94
|
+
success, _ = applier.apply_changes(changes)
|
95
|
+
if success:
|
96
|
+
preview_all_changes(console, changes)
|
97
|
+
|
98
|
+
if not config.auto_apply:
|
99
|
+
apply_changes = Confirm.ask("[cyan]Apply changes to working dir?[/cyan]")
|
100
|
+
else:
|
101
|
+
apply_changes = True
|
102
|
+
console.print("[cyan]Auto-applying changes to working dir...[/cyan]")
|
103
|
+
|
104
|
+
if apply_changes:
|
105
|
+
applier.apply_to_workspace_dir(changes)
|
106
|
+
console.print("[green]Changes applied successfully[/green]")
|
107
|
+
else:
|
108
|
+
console.print("[yellow]Changes were not applied[/yellow]")
|
109
|
+
|
110
|
+
return success, history_file
|
111
|
+
|
112
|
+
|
113
|
+
def extract_response_info(response: str) -> str:
|
114
|
+
"""Extract information after END_OF_INSTRUCTIONS marker"""
|
115
|
+
if not response:
|
116
|
+
return ""
|
117
|
+
|
118
|
+
# Find the marker
|
119
|
+
marker = "END_INSTRUCTIONS"
|
120
|
+
marker_pos = response.find(marker)
|
121
|
+
|
122
|
+
if marker_pos == -1:
|
123
|
+
return ""
|
124
|
+
|
125
|
+
# Get text after marker, skipping the marker itself
|
126
|
+
info = response[marker_pos + len(marker):].strip()
|
127
|
+
|
128
|
+
# Remove any XML-style tags
|
129
|
+
info = info.replace("<Extra info about what was implemented/changed goes here>", "")
|
130
|
+
|
131
|
+
return info.strip()
|
janito/change/history.py
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
from pathlib import Path
|
2
|
+
from datetime import datetime
|
3
|
+
from typing import Optional, Tuple
|
4
|
+
from janito.config import config
|
5
|
+
|
6
|
+
# Set fixed timestamp when module is loaded
|
7
|
+
APP_TIMESTAMP = datetime.now().strftime("%Y%m%d_%H%M%S")
|
8
|
+
|
9
|
+
def get_history_path() -> Path:
|
10
|
+
"""Create and return the history directory path"""
|
11
|
+
history_dir = config.workspace_dir / '.janito' / 'change_history'
|
12
|
+
history_dir.mkdir(parents=True, exist_ok=True)
|
13
|
+
return history_dir
|
14
|
+
|
15
|
+
def determine_history_file_type(filepath: Path) -> str:
|
16
|
+
"""Determine the type of saved file based on its name"""
|
17
|
+
name = filepath.name.lower()
|
18
|
+
if '_changes_failed' in name:
|
19
|
+
return 'changes_failed'
|
20
|
+
elif 'changes' in name:
|
21
|
+
return 'changes'
|
22
|
+
elif 'selected' in name:
|
23
|
+
return 'selected'
|
24
|
+
elif 'analysis' in name:
|
25
|
+
return 'analysis'
|
26
|
+
elif 'response' in name:
|
27
|
+
return 'response'
|
28
|
+
return 'unknown'
|
29
|
+
|
30
|
+
def save_changes_to_history(content: str, request: str) -> Path:
|
31
|
+
"""Save change content to history folder with timestamp and request info"""
|
32
|
+
history_dir = get_history_path()
|
33
|
+
|
34
|
+
# Create history entry with request and changes
|
35
|
+
filename = f"{APP_TIMESTAMP}_changes.txt"
|
36
|
+
history_file = history_dir / filename
|
37
|
+
history_content = f"""Request: {request}
|
38
|
+
Timestamp: {APP_TIMESTAMP}
|
39
|
+
|
40
|
+
Changes:
|
41
|
+
{content}
|
42
|
+
"""
|
43
|
+
history_file.write_text(history_content)
|
44
|
+
return history_file
|