chatmcp-cli 0.1.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.
- aider/__init__.py +20 -0
- aider/__main__.py +4 -0
- aider/_version.py +21 -0
- aider/analytics.py +250 -0
- aider/args.py +926 -0
- aider/args_formatter.py +228 -0
- aider/coders/__init__.py +34 -0
- aider/coders/architect_coder.py +48 -0
- aider/coders/architect_prompts.py +40 -0
- aider/coders/ask_coder.py +9 -0
- aider/coders/ask_prompts.py +35 -0
- aider/coders/base_coder.py +2483 -0
- aider/coders/base_prompts.py +60 -0
- aider/coders/chat_chunks.py +64 -0
- aider/coders/context_coder.py +53 -0
- aider/coders/context_prompts.py +75 -0
- aider/coders/editblock_coder.py +657 -0
- aider/coders/editblock_fenced_coder.py +10 -0
- aider/coders/editblock_fenced_prompts.py +143 -0
- aider/coders/editblock_func_coder.py +141 -0
- aider/coders/editblock_func_prompts.py +27 -0
- aider/coders/editblock_prompts.py +174 -0
- aider/coders/editor_diff_fenced_coder.py +9 -0
- aider/coders/editor_diff_fenced_prompts.py +11 -0
- aider/coders/editor_editblock_coder.py +8 -0
- aider/coders/editor_editblock_prompts.py +18 -0
- aider/coders/editor_whole_coder.py +8 -0
- aider/coders/editor_whole_prompts.py +10 -0
- aider/coders/help_coder.py +16 -0
- aider/coders/help_prompts.py +46 -0
- aider/coders/patch_coder.py +706 -0
- aider/coders/patch_prompts.py +161 -0
- aider/coders/search_replace.py +757 -0
- aider/coders/shell.py +37 -0
- aider/coders/single_wholefile_func_coder.py +102 -0
- aider/coders/single_wholefile_func_prompts.py +27 -0
- aider/coders/udiff_coder.py +429 -0
- aider/coders/udiff_prompts.py +115 -0
- aider/coders/udiff_simple.py +14 -0
- aider/coders/udiff_simple_prompts.py +25 -0
- aider/coders/wholefile_coder.py +144 -0
- aider/coders/wholefile_func_coder.py +134 -0
- aider/coders/wholefile_func_prompts.py +27 -0
- aider/coders/wholefile_prompts.py +67 -0
- aider/commands.py +1665 -0
- aider/copypaste.py +72 -0
- aider/deprecated.py +126 -0
- aider/diffs.py +128 -0
- aider/dump.py +29 -0
- aider/editor.py +147 -0
- aider/exceptions.py +107 -0
- aider/format_settings.py +26 -0
- aider/gui.py +545 -0
- aider/help.py +163 -0
- aider/help_pats.py +19 -0
- aider/history.py +143 -0
- aider/io.py +1175 -0
- aider/linter.py +304 -0
- aider/llm.py +47 -0
- aider/main.py +1267 -0
- aider/mdstream.py +243 -0
- aider/models.py +1286 -0
- aider/onboarding.py +428 -0
- aider/openrouter.py +128 -0
- aider/prompts.py +64 -0
- aider/queries/tree-sitter-language-pack/README.md +7 -0
- aider/queries/tree-sitter-language-pack/arduino-tags.scm +5 -0
- aider/queries/tree-sitter-language-pack/c-tags.scm +9 -0
- aider/queries/tree-sitter-language-pack/chatito-tags.scm +16 -0
- aider/queries/tree-sitter-language-pack/commonlisp-tags.scm +122 -0
- aider/queries/tree-sitter-language-pack/cpp-tags.scm +15 -0
- aider/queries/tree-sitter-language-pack/csharp-tags.scm +26 -0
- aider/queries/tree-sitter-language-pack/d-tags.scm +26 -0
- aider/queries/tree-sitter-language-pack/dart-tags.scm +92 -0
- aider/queries/tree-sitter-language-pack/elisp-tags.scm +5 -0
- aider/queries/tree-sitter-language-pack/elixir-tags.scm +54 -0
- aider/queries/tree-sitter-language-pack/elm-tags.scm +19 -0
- aider/queries/tree-sitter-language-pack/gleam-tags.scm +41 -0
- aider/queries/tree-sitter-language-pack/go-tags.scm +42 -0
- aider/queries/tree-sitter-language-pack/java-tags.scm +20 -0
- aider/queries/tree-sitter-language-pack/javascript-tags.scm +88 -0
- aider/queries/tree-sitter-language-pack/lua-tags.scm +34 -0
- aider/queries/tree-sitter-language-pack/ocaml-tags.scm +115 -0
- aider/queries/tree-sitter-language-pack/ocaml_interface-tags.scm +98 -0
- aider/queries/tree-sitter-language-pack/pony-tags.scm +39 -0
- aider/queries/tree-sitter-language-pack/properties-tags.scm +5 -0
- aider/queries/tree-sitter-language-pack/python-tags.scm +14 -0
- aider/queries/tree-sitter-language-pack/r-tags.scm +21 -0
- aider/queries/tree-sitter-language-pack/racket-tags.scm +12 -0
- aider/queries/tree-sitter-language-pack/ruby-tags.scm +64 -0
- aider/queries/tree-sitter-language-pack/rust-tags.scm +60 -0
- aider/queries/tree-sitter-language-pack/solidity-tags.scm +43 -0
- aider/queries/tree-sitter-language-pack/swift-tags.scm +51 -0
- aider/queries/tree-sitter-language-pack/udev-tags.scm +20 -0
- aider/queries/tree-sitter-languages/README.md +23 -0
- aider/queries/tree-sitter-languages/c-tags.scm +9 -0
- aider/queries/tree-sitter-languages/c_sharp-tags.scm +46 -0
- aider/queries/tree-sitter-languages/cpp-tags.scm +15 -0
- aider/queries/tree-sitter-languages/dart-tags.scm +91 -0
- aider/queries/tree-sitter-languages/elisp-tags.scm +8 -0
- aider/queries/tree-sitter-languages/elixir-tags.scm +54 -0
- aider/queries/tree-sitter-languages/elm-tags.scm +19 -0
- aider/queries/tree-sitter-languages/go-tags.scm +30 -0
- aider/queries/tree-sitter-languages/hcl-tags.scm +77 -0
- aider/queries/tree-sitter-languages/java-tags.scm +20 -0
- aider/queries/tree-sitter-languages/javascript-tags.scm +88 -0
- aider/queries/tree-sitter-languages/kotlin-tags.scm +27 -0
- aider/queries/tree-sitter-languages/ocaml-tags.scm +115 -0
- aider/queries/tree-sitter-languages/ocaml_interface-tags.scm +98 -0
- aider/queries/tree-sitter-languages/php-tags.scm +26 -0
- aider/queries/tree-sitter-languages/python-tags.scm +12 -0
- aider/queries/tree-sitter-languages/ql-tags.scm +26 -0
- aider/queries/tree-sitter-languages/ruby-tags.scm +64 -0
- aider/queries/tree-sitter-languages/rust-tags.scm +60 -0
- aider/queries/tree-sitter-languages/scala-tags.scm +65 -0
- aider/queries/tree-sitter-languages/typescript-tags.scm +41 -0
- aider/reasoning_tags.py +82 -0
- aider/repo.py +623 -0
- aider/repomap.py +847 -0
- aider/report.py +200 -0
- aider/resources/__init__.py +3 -0
- aider/resources/model-metadata.json +468 -0
- aider/resources/model-settings.yml +1767 -0
- aider/run_cmd.py +132 -0
- aider/scrape.py +284 -0
- aider/sendchat.py +61 -0
- aider/special.py +203 -0
- aider/urls.py +17 -0
- aider/utils.py +338 -0
- aider/versioncheck.py +113 -0
- aider/voice.py +187 -0
- aider/waiting.py +221 -0
- aider/watch.py +318 -0
- aider/watch_prompts.py +12 -0
- aider/website/Gemfile +8 -0
- aider/website/_includes/blame.md +162 -0
- aider/website/_includes/get-started.md +22 -0
- aider/website/_includes/help-tip.md +5 -0
- aider/website/_includes/help.md +24 -0
- aider/website/_includes/install.md +5 -0
- aider/website/_includes/keys.md +4 -0
- aider/website/_includes/model-warnings.md +67 -0
- aider/website/_includes/multi-line.md +22 -0
- aider/website/_includes/python-m-aider.md +5 -0
- aider/website/_includes/recording.css +228 -0
- aider/website/_includes/recording.md +34 -0
- aider/website/_includes/replit-pipx.md +9 -0
- aider/website/_includes/works-best.md +1 -0
- aider/website/_sass/custom/custom.scss +103 -0
- aider/website/docs/config/adv-model-settings.md +1881 -0
- aider/website/docs/config/aider_conf.md +527 -0
- aider/website/docs/config/api-keys.md +90 -0
- aider/website/docs/config/dotenv.md +478 -0
- aider/website/docs/config/editor.md +127 -0
- aider/website/docs/config/model-aliases.md +103 -0
- aider/website/docs/config/options.md +843 -0
- aider/website/docs/config/reasoning.md +209 -0
- aider/website/docs/config.md +44 -0
- aider/website/docs/faq.md +378 -0
- aider/website/docs/git.md +76 -0
- aider/website/docs/index.md +47 -0
- aider/website/docs/install/codespaces.md +39 -0
- aider/website/docs/install/docker.md +57 -0
- aider/website/docs/install/optional.md +100 -0
- aider/website/docs/install/replit.md +8 -0
- aider/website/docs/install.md +115 -0
- aider/website/docs/languages.md +264 -0
- aider/website/docs/legal/contributor-agreement.md +111 -0
- aider/website/docs/legal/privacy.md +104 -0
- aider/website/docs/llms/anthropic.md +77 -0
- aider/website/docs/llms/azure.md +48 -0
- aider/website/docs/llms/bedrock.md +132 -0
- aider/website/docs/llms/cohere.md +34 -0
- aider/website/docs/llms/deepseek.md +32 -0
- aider/website/docs/llms/gemini.md +49 -0
- aider/website/docs/llms/github.md +105 -0
- aider/website/docs/llms/groq.md +36 -0
- aider/website/docs/llms/lm-studio.md +39 -0
- aider/website/docs/llms/ollama.md +75 -0
- aider/website/docs/llms/openai-compat.md +39 -0
- aider/website/docs/llms/openai.md +58 -0
- aider/website/docs/llms/openrouter.md +78 -0
- aider/website/docs/llms/other.md +103 -0
- aider/website/docs/llms/vertex.md +50 -0
- aider/website/docs/llms/warnings.md +10 -0
- aider/website/docs/llms/xai.md +53 -0
- aider/website/docs/llms.md +54 -0
- aider/website/docs/more/analytics.md +122 -0
- aider/website/docs/more/edit-formats.md +116 -0
- aider/website/docs/more/infinite-output.md +137 -0
- aider/website/docs/more-info.md +8 -0
- aider/website/docs/recordings/auto-accept-architect.md +31 -0
- aider/website/docs/recordings/dont-drop-original-read-files.md +35 -0
- aider/website/docs/recordings/index.md +21 -0
- aider/website/docs/recordings/model-accepts-settings.md +69 -0
- aider/website/docs/recordings/tree-sitter-language-pack.md +80 -0
- aider/website/docs/repomap.md +112 -0
- aider/website/docs/scripting.md +100 -0
- aider/website/docs/troubleshooting/aider-not-found.md +24 -0
- aider/website/docs/troubleshooting/edit-errors.md +76 -0
- aider/website/docs/troubleshooting/imports.md +62 -0
- aider/website/docs/troubleshooting/models-and-keys.md +54 -0
- aider/website/docs/troubleshooting/support.md +79 -0
- aider/website/docs/troubleshooting/token-limits.md +96 -0
- aider/website/docs/troubleshooting/warnings.md +12 -0
- aider/website/docs/troubleshooting.md +11 -0
- aider/website/docs/usage/browser.md +57 -0
- aider/website/docs/usage/caching.md +49 -0
- aider/website/docs/usage/commands.md +132 -0
- aider/website/docs/usage/conventions.md +119 -0
- aider/website/docs/usage/copypaste.md +121 -0
- aider/website/docs/usage/images-urls.md +48 -0
- aider/website/docs/usage/lint-test.md +118 -0
- aider/website/docs/usage/modes.md +211 -0
- aider/website/docs/usage/not-code.md +179 -0
- aider/website/docs/usage/notifications.md +87 -0
- aider/website/docs/usage/tips.md +79 -0
- aider/website/docs/usage/tutorials.md +30 -0
- aider/website/docs/usage/voice.md +121 -0
- aider/website/docs/usage/watch.md +294 -0
- aider/website/docs/usage.md +92 -0
- aider/website/share/index.md +101 -0
- chatmcp_cli-0.1.0.dist-info/METADATA +502 -0
- chatmcp_cli-0.1.0.dist-info/RECORD +228 -0
- chatmcp_cli-0.1.0.dist-info/WHEEL +5 -0
- chatmcp_cli-0.1.0.dist-info/entry_points.txt +3 -0
- chatmcp_cli-0.1.0.dist-info/licenses/LICENSE.txt +202 -0
- chatmcp_cli-0.1.0.dist-info/top_level.txt +1 -0
aider/io.py
ADDED
@@ -0,0 +1,1175 @@
|
|
1
|
+
import base64
|
2
|
+
import functools
|
3
|
+
import os
|
4
|
+
import shutil
|
5
|
+
import signal
|
6
|
+
import subprocess
|
7
|
+
import time
|
8
|
+
import webbrowser
|
9
|
+
from collections import defaultdict
|
10
|
+
from dataclasses import dataclass
|
11
|
+
from datetime import datetime
|
12
|
+
from io import StringIO
|
13
|
+
from pathlib import Path
|
14
|
+
|
15
|
+
from prompt_toolkit.completion import Completer, Completion, ThreadedCompleter
|
16
|
+
from prompt_toolkit.cursor_shapes import ModalCursorShapeConfig
|
17
|
+
from prompt_toolkit.enums import EditingMode
|
18
|
+
from prompt_toolkit.filters import Condition, is_searching
|
19
|
+
from prompt_toolkit.history import FileHistory
|
20
|
+
from prompt_toolkit.key_binding import KeyBindings
|
21
|
+
from prompt_toolkit.key_binding.vi_state import InputMode
|
22
|
+
from prompt_toolkit.keys import Keys
|
23
|
+
from prompt_toolkit.lexers import PygmentsLexer
|
24
|
+
from prompt_toolkit.output.vt100 import is_dumb_terminal
|
25
|
+
from prompt_toolkit.shortcuts import CompleteStyle, PromptSession
|
26
|
+
from prompt_toolkit.styles import Style
|
27
|
+
from pygments.lexers import MarkdownLexer, guess_lexer_for_filename
|
28
|
+
from pygments.token import Token
|
29
|
+
from rich.color import ColorParseError
|
30
|
+
from rich.columns import Columns
|
31
|
+
from rich.console import Console
|
32
|
+
from rich.markdown import Markdown
|
33
|
+
from rich.style import Style as RichStyle
|
34
|
+
from rich.text import Text
|
35
|
+
|
36
|
+
from aider.mdstream import MarkdownStream
|
37
|
+
|
38
|
+
from .dump import dump # noqa: F401
|
39
|
+
from .editor import pipe_editor
|
40
|
+
from .utils import is_image_file
|
41
|
+
|
42
|
+
# Constants
|
43
|
+
NOTIFICATION_MESSAGE = "Aider is waiting for your input"
|
44
|
+
|
45
|
+
|
46
|
+
def ensure_hash_prefix(color):
|
47
|
+
"""Ensure hex color values have a # prefix."""
|
48
|
+
if not color:
|
49
|
+
return color
|
50
|
+
if isinstance(color, str) and color.strip() and not color.startswith("#"):
|
51
|
+
# Check if it's a valid hex color (3 or 6 hex digits)
|
52
|
+
if all(c in "0123456789ABCDEFabcdef" for c in color) and len(color) in (3, 6):
|
53
|
+
return f"#{color}"
|
54
|
+
return color
|
55
|
+
|
56
|
+
|
57
|
+
def restore_multiline(func):
|
58
|
+
"""Decorator to restore multiline mode after function execution"""
|
59
|
+
|
60
|
+
@functools.wraps(func)
|
61
|
+
def wrapper(self, *args, **kwargs):
|
62
|
+
orig_multiline = self.multiline_mode
|
63
|
+
self.multiline_mode = False
|
64
|
+
try:
|
65
|
+
return func(self, *args, **kwargs)
|
66
|
+
except Exception:
|
67
|
+
raise
|
68
|
+
finally:
|
69
|
+
self.multiline_mode = orig_multiline
|
70
|
+
|
71
|
+
return wrapper
|
72
|
+
|
73
|
+
|
74
|
+
class CommandCompletionException(Exception):
|
75
|
+
"""Raised when a command should use the normal autocompleter instead of
|
76
|
+
command-specific completion."""
|
77
|
+
|
78
|
+
pass
|
79
|
+
|
80
|
+
|
81
|
+
@dataclass
|
82
|
+
class ConfirmGroup:
|
83
|
+
preference: str = None
|
84
|
+
show_group: bool = True
|
85
|
+
|
86
|
+
def __init__(self, items=None):
|
87
|
+
if items is not None:
|
88
|
+
self.show_group = len(items) > 1
|
89
|
+
|
90
|
+
|
91
|
+
class AutoCompleter(Completer):
|
92
|
+
def __init__(
|
93
|
+
self, root, rel_fnames, addable_rel_fnames, commands, encoding, abs_read_only_fnames=None
|
94
|
+
):
|
95
|
+
self.addable_rel_fnames = addable_rel_fnames
|
96
|
+
self.rel_fnames = rel_fnames
|
97
|
+
self.encoding = encoding
|
98
|
+
self.abs_read_only_fnames = abs_read_only_fnames or []
|
99
|
+
|
100
|
+
fname_to_rel_fnames = defaultdict(list)
|
101
|
+
for rel_fname in addable_rel_fnames:
|
102
|
+
fname = os.path.basename(rel_fname)
|
103
|
+
if fname != rel_fname:
|
104
|
+
fname_to_rel_fnames[fname].append(rel_fname)
|
105
|
+
self.fname_to_rel_fnames = fname_to_rel_fnames
|
106
|
+
|
107
|
+
self.words = set()
|
108
|
+
|
109
|
+
self.commands = commands
|
110
|
+
self.command_completions = dict()
|
111
|
+
if commands:
|
112
|
+
self.command_names = self.commands.get_commands()
|
113
|
+
|
114
|
+
for rel_fname in addable_rel_fnames:
|
115
|
+
self.words.add(rel_fname)
|
116
|
+
|
117
|
+
for rel_fname in rel_fnames:
|
118
|
+
self.words.add(rel_fname)
|
119
|
+
|
120
|
+
all_fnames = [Path(root) / rel_fname for rel_fname in rel_fnames]
|
121
|
+
if abs_read_only_fnames:
|
122
|
+
all_fnames.extend(abs_read_only_fnames)
|
123
|
+
|
124
|
+
self.all_fnames = all_fnames
|
125
|
+
self.tokenized = False
|
126
|
+
|
127
|
+
def tokenize(self):
|
128
|
+
if self.tokenized:
|
129
|
+
return
|
130
|
+
self.tokenized = True
|
131
|
+
|
132
|
+
for fname in self.all_fnames:
|
133
|
+
try:
|
134
|
+
with open(fname, "r", encoding=self.encoding) as f:
|
135
|
+
content = f.read()
|
136
|
+
except (FileNotFoundError, UnicodeDecodeError, IsADirectoryError):
|
137
|
+
continue
|
138
|
+
try:
|
139
|
+
lexer = guess_lexer_for_filename(fname, content)
|
140
|
+
except Exception: # On Windows, bad ref to time.clock which is deprecated
|
141
|
+
continue
|
142
|
+
|
143
|
+
tokens = list(lexer.get_tokens(content))
|
144
|
+
self.words.update(
|
145
|
+
(token[1], f"`{token[1]}`") for token in tokens if token[0] in Token.Name
|
146
|
+
)
|
147
|
+
|
148
|
+
def get_command_completions(self, document, complete_event, text, words):
|
149
|
+
if len(words) == 1 and not text[-1].isspace():
|
150
|
+
partial = words[0].lower()
|
151
|
+
candidates = [cmd for cmd in self.command_names if cmd.startswith(partial)]
|
152
|
+
for candidate in sorted(candidates):
|
153
|
+
yield Completion(candidate, start_position=-len(words[-1]))
|
154
|
+
return
|
155
|
+
|
156
|
+
if len(words) <= 1 or text[-1].isspace():
|
157
|
+
return
|
158
|
+
|
159
|
+
cmd = words[0]
|
160
|
+
partial = words[-1].lower()
|
161
|
+
|
162
|
+
matches, _, _ = self.commands.matching_commands(cmd)
|
163
|
+
if len(matches) == 1:
|
164
|
+
cmd = matches[0]
|
165
|
+
elif cmd not in matches:
|
166
|
+
return
|
167
|
+
|
168
|
+
raw_completer = self.commands.get_raw_completions(cmd)
|
169
|
+
if raw_completer:
|
170
|
+
yield from raw_completer(document, complete_event)
|
171
|
+
return
|
172
|
+
|
173
|
+
if cmd not in self.command_completions:
|
174
|
+
candidates = self.commands.get_completions(cmd)
|
175
|
+
self.command_completions[cmd] = candidates
|
176
|
+
else:
|
177
|
+
candidates = self.command_completions[cmd]
|
178
|
+
|
179
|
+
if candidates is None:
|
180
|
+
return
|
181
|
+
|
182
|
+
candidates = [word for word in candidates if partial in word.lower()]
|
183
|
+
for candidate in sorted(candidates):
|
184
|
+
yield Completion(candidate, start_position=-len(words[-1]))
|
185
|
+
|
186
|
+
def get_completions(self, document, complete_event):
|
187
|
+
self.tokenize()
|
188
|
+
|
189
|
+
text = document.text_before_cursor
|
190
|
+
words = text.split()
|
191
|
+
if not words:
|
192
|
+
return
|
193
|
+
|
194
|
+
if text and text[-1].isspace():
|
195
|
+
# don't keep completing after a space
|
196
|
+
return
|
197
|
+
|
198
|
+
if text[0] == "/":
|
199
|
+
try:
|
200
|
+
yield from self.get_command_completions(document, complete_event, text, words)
|
201
|
+
return
|
202
|
+
except CommandCompletionException:
|
203
|
+
# Fall through to normal completion
|
204
|
+
pass
|
205
|
+
|
206
|
+
candidates = self.words
|
207
|
+
candidates.update(set(self.fname_to_rel_fnames))
|
208
|
+
candidates = [word if type(word) is tuple else (word, word) for word in candidates]
|
209
|
+
|
210
|
+
last_word = words[-1]
|
211
|
+
|
212
|
+
# Only provide completions if the user has typed at least 3 characters
|
213
|
+
if len(last_word) < 3:
|
214
|
+
return
|
215
|
+
|
216
|
+
completions = []
|
217
|
+
for word_match, word_insert in candidates:
|
218
|
+
if word_match.lower().startswith(last_word.lower()):
|
219
|
+
completions.append((word_insert, -len(last_word), word_match))
|
220
|
+
|
221
|
+
rel_fnames = self.fname_to_rel_fnames.get(word_match, [])
|
222
|
+
if rel_fnames:
|
223
|
+
for rel_fname in rel_fnames:
|
224
|
+
completions.append((rel_fname, -len(last_word), rel_fname))
|
225
|
+
|
226
|
+
for ins, pos, match in sorted(completions):
|
227
|
+
yield Completion(ins, start_position=pos, display=match)
|
228
|
+
|
229
|
+
|
230
|
+
class InputOutput:
|
231
|
+
num_error_outputs = 0
|
232
|
+
num_user_asks = 0
|
233
|
+
clipboard_watcher = None
|
234
|
+
bell_on_next_input = False
|
235
|
+
notifications_command = None
|
236
|
+
|
237
|
+
def __init__(
|
238
|
+
self,
|
239
|
+
pretty=True,
|
240
|
+
yes=None,
|
241
|
+
input_history_file=None,
|
242
|
+
chat_history_file=None,
|
243
|
+
input=None,
|
244
|
+
output=None,
|
245
|
+
user_input_color="blue",
|
246
|
+
tool_output_color=None,
|
247
|
+
tool_error_color="red",
|
248
|
+
tool_warning_color="#FFA500",
|
249
|
+
assistant_output_color="blue",
|
250
|
+
completion_menu_color=None,
|
251
|
+
completion_menu_bg_color=None,
|
252
|
+
completion_menu_current_color=None,
|
253
|
+
completion_menu_current_bg_color=None,
|
254
|
+
code_theme="default",
|
255
|
+
encoding="utf-8",
|
256
|
+
line_endings="platform",
|
257
|
+
dry_run=False,
|
258
|
+
llm_history_file=None,
|
259
|
+
editingmode=EditingMode.EMACS,
|
260
|
+
fancy_input=True,
|
261
|
+
file_watcher=None,
|
262
|
+
multiline_mode=False,
|
263
|
+
root=".",
|
264
|
+
notifications=False,
|
265
|
+
notifications_command=None,
|
266
|
+
):
|
267
|
+
self.placeholder = None
|
268
|
+
self.interrupted = False
|
269
|
+
self.never_prompts = set()
|
270
|
+
self.editingmode = editingmode
|
271
|
+
self.multiline_mode = multiline_mode
|
272
|
+
self.bell_on_next_input = False
|
273
|
+
self.notifications = notifications
|
274
|
+
if notifications and notifications_command is None:
|
275
|
+
self.notifications_command = self.get_default_notification_command()
|
276
|
+
else:
|
277
|
+
self.notifications_command = notifications_command
|
278
|
+
|
279
|
+
no_color = os.environ.get("NO_COLOR")
|
280
|
+
if no_color is not None and no_color != "":
|
281
|
+
pretty = False
|
282
|
+
|
283
|
+
self.user_input_color = ensure_hash_prefix(user_input_color) if pretty else None
|
284
|
+
self.tool_output_color = ensure_hash_prefix(tool_output_color) if pretty else None
|
285
|
+
self.tool_error_color = ensure_hash_prefix(tool_error_color) if pretty else None
|
286
|
+
self.tool_warning_color = ensure_hash_prefix(tool_warning_color) if pretty else None
|
287
|
+
self.assistant_output_color = ensure_hash_prefix(assistant_output_color)
|
288
|
+
self.completion_menu_color = ensure_hash_prefix(completion_menu_color) if pretty else None
|
289
|
+
self.completion_menu_bg_color = (
|
290
|
+
ensure_hash_prefix(completion_menu_bg_color) if pretty else None
|
291
|
+
)
|
292
|
+
self.completion_menu_current_color = (
|
293
|
+
ensure_hash_prefix(completion_menu_current_color) if pretty else None
|
294
|
+
)
|
295
|
+
self.completion_menu_current_bg_color = (
|
296
|
+
ensure_hash_prefix(completion_menu_current_bg_color) if pretty else None
|
297
|
+
)
|
298
|
+
|
299
|
+
self.code_theme = code_theme
|
300
|
+
|
301
|
+
self.input = input
|
302
|
+
self.output = output
|
303
|
+
|
304
|
+
self.pretty = pretty
|
305
|
+
if self.output:
|
306
|
+
self.pretty = False
|
307
|
+
|
308
|
+
self.yes = yes
|
309
|
+
|
310
|
+
self.input_history_file = input_history_file
|
311
|
+
self.llm_history_file = llm_history_file
|
312
|
+
if chat_history_file is not None:
|
313
|
+
self.chat_history_file = Path(chat_history_file)
|
314
|
+
else:
|
315
|
+
self.chat_history_file = None
|
316
|
+
|
317
|
+
self.encoding = encoding
|
318
|
+
valid_line_endings = {"platform", "lf", "crlf"}
|
319
|
+
if line_endings not in valid_line_endings:
|
320
|
+
raise ValueError(
|
321
|
+
f"Invalid line_endings value: {line_endings}. "
|
322
|
+
f"Must be one of: {', '.join(valid_line_endings)}"
|
323
|
+
)
|
324
|
+
self.newline = (
|
325
|
+
None if line_endings == "platform" else "\n" if line_endings == "lf" else "\r\n"
|
326
|
+
)
|
327
|
+
self.dry_run = dry_run
|
328
|
+
|
329
|
+
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
330
|
+
self.append_chat_history(f"\n# aider chat started at {current_time}\n\n")
|
331
|
+
|
332
|
+
self.prompt_session = None
|
333
|
+
self.is_dumb_terminal = is_dumb_terminal()
|
334
|
+
|
335
|
+
if self.is_dumb_terminal:
|
336
|
+
self.pretty = False
|
337
|
+
fancy_input = False
|
338
|
+
|
339
|
+
if fancy_input:
|
340
|
+
# Initialize PromptSession only if we have a capable terminal
|
341
|
+
session_kwargs = {
|
342
|
+
"input": self.input,
|
343
|
+
"output": self.output,
|
344
|
+
"lexer": PygmentsLexer(MarkdownLexer),
|
345
|
+
"editing_mode": self.editingmode,
|
346
|
+
}
|
347
|
+
if self.editingmode == EditingMode.VI:
|
348
|
+
session_kwargs["cursor"] = ModalCursorShapeConfig()
|
349
|
+
if self.input_history_file is not None:
|
350
|
+
session_kwargs["history"] = FileHistory(self.input_history_file)
|
351
|
+
try:
|
352
|
+
self.prompt_session = PromptSession(**session_kwargs)
|
353
|
+
self.console = Console() # pretty console
|
354
|
+
except Exception as err:
|
355
|
+
self.console = Console(force_terminal=False, no_color=True)
|
356
|
+
self.tool_error(f"Can't initialize prompt toolkit: {err}") # non-pretty
|
357
|
+
else:
|
358
|
+
self.console = Console(force_terminal=False, no_color=True) # non-pretty
|
359
|
+
if self.is_dumb_terminal:
|
360
|
+
self.tool_output("Detected dumb terminal, disabling fancy input and pretty output.")
|
361
|
+
|
362
|
+
self.file_watcher = file_watcher
|
363
|
+
self.root = root
|
364
|
+
|
365
|
+
# Validate color settings after console is initialized
|
366
|
+
self._validate_color_settings()
|
367
|
+
|
368
|
+
def _validate_color_settings(self):
|
369
|
+
"""Validate configured color strings and reset invalid ones."""
|
370
|
+
color_attributes = [
|
371
|
+
"user_input_color",
|
372
|
+
"tool_output_color",
|
373
|
+
"tool_error_color",
|
374
|
+
"tool_warning_color",
|
375
|
+
"assistant_output_color",
|
376
|
+
"completion_menu_color",
|
377
|
+
"completion_menu_bg_color",
|
378
|
+
"completion_menu_current_color",
|
379
|
+
"completion_menu_current_bg_color",
|
380
|
+
]
|
381
|
+
for attr_name in color_attributes:
|
382
|
+
color_value = getattr(self, attr_name, None)
|
383
|
+
if color_value:
|
384
|
+
try:
|
385
|
+
# Try creating a style to validate the color
|
386
|
+
RichStyle(color=color_value)
|
387
|
+
except ColorParseError as e:
|
388
|
+
self.console.print(
|
389
|
+
"[bold red]Warning:[/bold red] Invalid configuration for"
|
390
|
+
f" {attr_name}: '{color_value}'. {e}. Disabling this color."
|
391
|
+
)
|
392
|
+
setattr(self, attr_name, None) # Reset invalid color to None
|
393
|
+
|
394
|
+
def _get_style(self):
|
395
|
+
style_dict = {}
|
396
|
+
if not self.pretty:
|
397
|
+
return Style.from_dict(style_dict)
|
398
|
+
|
399
|
+
if self.user_input_color:
|
400
|
+
style_dict.setdefault("", self.user_input_color)
|
401
|
+
style_dict.update(
|
402
|
+
{
|
403
|
+
"pygments.literal.string": f"bold italic {self.user_input_color}",
|
404
|
+
}
|
405
|
+
)
|
406
|
+
|
407
|
+
# Conditionally add 'completion-menu' style
|
408
|
+
completion_menu_style = []
|
409
|
+
if self.completion_menu_bg_color:
|
410
|
+
completion_menu_style.append(f"bg:{self.completion_menu_bg_color}")
|
411
|
+
if self.completion_menu_color:
|
412
|
+
completion_menu_style.append(self.completion_menu_color)
|
413
|
+
if completion_menu_style:
|
414
|
+
style_dict["completion-menu"] = " ".join(completion_menu_style)
|
415
|
+
|
416
|
+
# Conditionally add 'completion-menu.completion.current' style
|
417
|
+
completion_menu_current_style = []
|
418
|
+
if self.completion_menu_current_bg_color:
|
419
|
+
completion_menu_current_style.append(self.completion_menu_current_bg_color)
|
420
|
+
if self.completion_menu_current_color:
|
421
|
+
completion_menu_current_style.append(f"bg:{self.completion_menu_current_color}")
|
422
|
+
if completion_menu_current_style:
|
423
|
+
style_dict["completion-menu.completion.current"] = " ".join(
|
424
|
+
completion_menu_current_style
|
425
|
+
)
|
426
|
+
|
427
|
+
return Style.from_dict(style_dict)
|
428
|
+
|
429
|
+
def read_image(self, filename):
|
430
|
+
try:
|
431
|
+
with open(str(filename), "rb") as image_file:
|
432
|
+
encoded_string = base64.b64encode(image_file.read())
|
433
|
+
return encoded_string.decode("utf-8")
|
434
|
+
except OSError as err:
|
435
|
+
self.tool_error(f"{filename}: unable to read: {err}")
|
436
|
+
return
|
437
|
+
except FileNotFoundError:
|
438
|
+
self.tool_error(f"{filename}: file not found error")
|
439
|
+
return
|
440
|
+
except IsADirectoryError:
|
441
|
+
self.tool_error(f"{filename}: is a directory")
|
442
|
+
return
|
443
|
+
except Exception as e:
|
444
|
+
self.tool_error(f"{filename}: {e}")
|
445
|
+
return
|
446
|
+
|
447
|
+
def read_text(self, filename, silent=False):
|
448
|
+
if is_image_file(filename):
|
449
|
+
return self.read_image(filename)
|
450
|
+
|
451
|
+
try:
|
452
|
+
with open(str(filename), "r", encoding=self.encoding) as f:
|
453
|
+
return f.read()
|
454
|
+
except FileNotFoundError:
|
455
|
+
if not silent:
|
456
|
+
self.tool_error(f"{filename}: file not found error")
|
457
|
+
return
|
458
|
+
except IsADirectoryError:
|
459
|
+
if not silent:
|
460
|
+
self.tool_error(f"{filename}: is a directory")
|
461
|
+
return
|
462
|
+
except OSError as err:
|
463
|
+
if not silent:
|
464
|
+
self.tool_error(f"{filename}: unable to read: {err}")
|
465
|
+
return
|
466
|
+
except UnicodeError as e:
|
467
|
+
if not silent:
|
468
|
+
self.tool_error(f"{filename}: {e}")
|
469
|
+
self.tool_error("Use --encoding to set the unicode encoding.")
|
470
|
+
return
|
471
|
+
|
472
|
+
def write_text(self, filename, content, max_retries=5, initial_delay=0.1):
|
473
|
+
"""
|
474
|
+
Writes content to a file, retrying with progressive backoff if the file is locked.
|
475
|
+
|
476
|
+
:param filename: Path to the file to write.
|
477
|
+
:param content: Content to write to the file.
|
478
|
+
:param max_retries: Maximum number of retries if a file lock is encountered.
|
479
|
+
:param initial_delay: Initial delay (in seconds) before the first retry.
|
480
|
+
"""
|
481
|
+
if self.dry_run:
|
482
|
+
return
|
483
|
+
|
484
|
+
delay = initial_delay
|
485
|
+
for attempt in range(max_retries):
|
486
|
+
try:
|
487
|
+
with open(str(filename), "w", encoding=self.encoding, newline=self.newline) as f:
|
488
|
+
f.write(content)
|
489
|
+
return # Successfully wrote the file
|
490
|
+
except PermissionError as err:
|
491
|
+
if attempt < max_retries - 1:
|
492
|
+
time.sleep(delay)
|
493
|
+
delay *= 2 # Exponential backoff
|
494
|
+
else:
|
495
|
+
self.tool_error(
|
496
|
+
f"Unable to write file {filename} after {max_retries} attempts: {err}"
|
497
|
+
)
|
498
|
+
raise
|
499
|
+
except OSError as err:
|
500
|
+
self.tool_error(f"Unable to write file {filename}: {err}")
|
501
|
+
raise
|
502
|
+
|
503
|
+
def rule(self):
|
504
|
+
if self.pretty:
|
505
|
+
style = dict(style=self.user_input_color) if self.user_input_color else dict()
|
506
|
+
self.console.rule(**style)
|
507
|
+
else:
|
508
|
+
print()
|
509
|
+
|
510
|
+
def interrupt_input(self):
|
511
|
+
if self.prompt_session and self.prompt_session.app:
|
512
|
+
# Store any partial input before interrupting
|
513
|
+
self.placeholder = self.prompt_session.app.current_buffer.text
|
514
|
+
self.interrupted = True
|
515
|
+
self.prompt_session.app.exit()
|
516
|
+
|
517
|
+
def get_input(
|
518
|
+
self,
|
519
|
+
root,
|
520
|
+
rel_fnames,
|
521
|
+
addable_rel_fnames,
|
522
|
+
commands,
|
523
|
+
abs_read_only_fnames=None,
|
524
|
+
edit_format=None,
|
525
|
+
):
|
526
|
+
self.rule()
|
527
|
+
|
528
|
+
# Ring the bell if needed
|
529
|
+
self.ring_bell()
|
530
|
+
|
531
|
+
rel_fnames = list(rel_fnames)
|
532
|
+
show = ""
|
533
|
+
if rel_fnames:
|
534
|
+
rel_read_only_fnames = [
|
535
|
+
get_rel_fname(fname, root) for fname in (abs_read_only_fnames or [])
|
536
|
+
]
|
537
|
+
show = self.format_files_for_input(rel_fnames, rel_read_only_fnames)
|
538
|
+
|
539
|
+
prompt_prefix = ""
|
540
|
+
if edit_format:
|
541
|
+
prompt_prefix += edit_format
|
542
|
+
if self.multiline_mode:
|
543
|
+
prompt_prefix += (" " if edit_format else "") + "multi"
|
544
|
+
prompt_prefix += "> "
|
545
|
+
|
546
|
+
show += prompt_prefix
|
547
|
+
self.prompt_prefix = prompt_prefix
|
548
|
+
|
549
|
+
inp = ""
|
550
|
+
multiline_input = False
|
551
|
+
|
552
|
+
style = self._get_style()
|
553
|
+
|
554
|
+
completer_instance = ThreadedCompleter(
|
555
|
+
AutoCompleter(
|
556
|
+
root,
|
557
|
+
rel_fnames,
|
558
|
+
addable_rel_fnames,
|
559
|
+
commands,
|
560
|
+
self.encoding,
|
561
|
+
abs_read_only_fnames=abs_read_only_fnames,
|
562
|
+
)
|
563
|
+
)
|
564
|
+
|
565
|
+
def suspend_to_bg(event):
|
566
|
+
"""Suspend currently running application."""
|
567
|
+
event.app.suspend_to_background()
|
568
|
+
|
569
|
+
kb = KeyBindings()
|
570
|
+
|
571
|
+
@kb.add(Keys.ControlZ, filter=Condition(lambda: hasattr(signal, "SIGTSTP")))
|
572
|
+
def _(event):
|
573
|
+
"Suspend to background with ctrl-z"
|
574
|
+
suspend_to_bg(event)
|
575
|
+
|
576
|
+
@kb.add("c-space")
|
577
|
+
def _(event):
|
578
|
+
"Ignore Ctrl when pressing space bar"
|
579
|
+
event.current_buffer.insert_text(" ")
|
580
|
+
|
581
|
+
@kb.add("c-up")
|
582
|
+
def _(event):
|
583
|
+
"Navigate backward through history"
|
584
|
+
event.current_buffer.history_backward()
|
585
|
+
|
586
|
+
@kb.add("c-down")
|
587
|
+
def _(event):
|
588
|
+
"Navigate forward through history"
|
589
|
+
event.current_buffer.history_forward()
|
590
|
+
|
591
|
+
@kb.add("c-x", "c-e")
|
592
|
+
def _(event):
|
593
|
+
"Edit current input in external editor (like Bash)"
|
594
|
+
buffer = event.current_buffer
|
595
|
+
current_text = buffer.text
|
596
|
+
|
597
|
+
# Open the editor with the current text
|
598
|
+
edited_text = pipe_editor(input_data=current_text, suffix="md")
|
599
|
+
|
600
|
+
# Replace the buffer with the edited text, strip any trailing newlines
|
601
|
+
buffer.text = edited_text.rstrip("\n")
|
602
|
+
|
603
|
+
# Move cursor to the end of the text
|
604
|
+
buffer.cursor_position = len(buffer.text)
|
605
|
+
|
606
|
+
@kb.add("enter", eager=True, filter=~is_searching)
|
607
|
+
def _(event):
|
608
|
+
"Handle Enter key press"
|
609
|
+
if self.multiline_mode and not (
|
610
|
+
self.editingmode == EditingMode.VI
|
611
|
+
and event.app.vi_state.input_mode == InputMode.NAVIGATION
|
612
|
+
):
|
613
|
+
# In multiline mode and if not in vi-mode or vi navigation/normal mode,
|
614
|
+
# Enter adds a newline
|
615
|
+
event.current_buffer.insert_text("\n")
|
616
|
+
else:
|
617
|
+
# In normal mode, Enter submits
|
618
|
+
event.current_buffer.validate_and_handle()
|
619
|
+
|
620
|
+
@kb.add("escape", "enter", eager=True, filter=~is_searching) # This is Alt+Enter
|
621
|
+
def _(event):
|
622
|
+
"Handle Alt+Enter key press"
|
623
|
+
if self.multiline_mode:
|
624
|
+
# In multiline mode, Alt+Enter submits
|
625
|
+
event.current_buffer.validate_and_handle()
|
626
|
+
else:
|
627
|
+
# In normal mode, Alt+Enter adds a newline
|
628
|
+
event.current_buffer.insert_text("\n")
|
629
|
+
|
630
|
+
while True:
|
631
|
+
if multiline_input:
|
632
|
+
show = self.prompt_prefix
|
633
|
+
|
634
|
+
try:
|
635
|
+
if self.prompt_session:
|
636
|
+
# Use placeholder if set, then clear it
|
637
|
+
default = self.placeholder or ""
|
638
|
+
self.placeholder = None
|
639
|
+
|
640
|
+
self.interrupted = False
|
641
|
+
if not multiline_input:
|
642
|
+
if self.file_watcher:
|
643
|
+
self.file_watcher.start()
|
644
|
+
if self.clipboard_watcher:
|
645
|
+
self.clipboard_watcher.start()
|
646
|
+
|
647
|
+
def get_continuation(width, line_number, is_soft_wrap):
|
648
|
+
return self.prompt_prefix
|
649
|
+
|
650
|
+
line = self.prompt_session.prompt(
|
651
|
+
show,
|
652
|
+
default=default,
|
653
|
+
completer=completer_instance,
|
654
|
+
reserve_space_for_menu=4,
|
655
|
+
complete_style=CompleteStyle.MULTI_COLUMN,
|
656
|
+
style=style,
|
657
|
+
key_bindings=kb,
|
658
|
+
complete_while_typing=True,
|
659
|
+
prompt_continuation=get_continuation,
|
660
|
+
)
|
661
|
+
else:
|
662
|
+
line = input(show)
|
663
|
+
|
664
|
+
# Check if we were interrupted by a file change
|
665
|
+
if self.interrupted:
|
666
|
+
line = line or ""
|
667
|
+
if self.file_watcher:
|
668
|
+
cmd = self.file_watcher.process_changes()
|
669
|
+
return cmd
|
670
|
+
|
671
|
+
except EOFError:
|
672
|
+
raise
|
673
|
+
except Exception as err:
|
674
|
+
import traceback
|
675
|
+
|
676
|
+
self.tool_error(str(err))
|
677
|
+
self.tool_error(traceback.format_exc())
|
678
|
+
return ""
|
679
|
+
except UnicodeEncodeError as err:
|
680
|
+
self.tool_error(str(err))
|
681
|
+
return ""
|
682
|
+
finally:
|
683
|
+
if self.file_watcher:
|
684
|
+
self.file_watcher.stop()
|
685
|
+
if self.clipboard_watcher:
|
686
|
+
self.clipboard_watcher.stop()
|
687
|
+
|
688
|
+
if line.strip("\r\n") and not multiline_input:
|
689
|
+
stripped = line.strip("\r\n")
|
690
|
+
if stripped == "{":
|
691
|
+
multiline_input = True
|
692
|
+
multiline_tag = None
|
693
|
+
inp += ""
|
694
|
+
elif stripped[0] == "{":
|
695
|
+
# Extract tag if it exists (only alphanumeric chars)
|
696
|
+
tag = "".join(c for c in stripped[1:] if c.isalnum())
|
697
|
+
if stripped == "{" + tag:
|
698
|
+
multiline_input = True
|
699
|
+
multiline_tag = tag
|
700
|
+
inp += ""
|
701
|
+
else:
|
702
|
+
inp = line
|
703
|
+
break
|
704
|
+
else:
|
705
|
+
inp = line
|
706
|
+
break
|
707
|
+
continue
|
708
|
+
elif multiline_input and line.strip():
|
709
|
+
if multiline_tag:
|
710
|
+
# Check if line is exactly "tag}"
|
711
|
+
if line.strip("\r\n") == f"{multiline_tag}}}":
|
712
|
+
break
|
713
|
+
else:
|
714
|
+
inp += line + "\n"
|
715
|
+
# Check if line is exactly "}"
|
716
|
+
elif line.strip("\r\n") == "}":
|
717
|
+
break
|
718
|
+
else:
|
719
|
+
inp += line + "\n"
|
720
|
+
elif multiline_input:
|
721
|
+
inp += line + "\n"
|
722
|
+
else:
|
723
|
+
inp = line
|
724
|
+
break
|
725
|
+
|
726
|
+
print()
|
727
|
+
self.user_input(inp)
|
728
|
+
return inp
|
729
|
+
|
730
|
+
def add_to_input_history(self, inp):
|
731
|
+
if not self.input_history_file:
|
732
|
+
return
|
733
|
+
try:
|
734
|
+
FileHistory(self.input_history_file).append_string(inp)
|
735
|
+
# Also add to the in-memory history if it exists
|
736
|
+
if self.prompt_session and self.prompt_session.history:
|
737
|
+
self.prompt_session.history.append_string(inp)
|
738
|
+
except OSError as err:
|
739
|
+
self.tool_warning(f"Unable to write to input history file: {err}")
|
740
|
+
|
741
|
+
def get_input_history(self):
|
742
|
+
if not self.input_history_file:
|
743
|
+
return []
|
744
|
+
|
745
|
+
fh = FileHistory(self.input_history_file)
|
746
|
+
return fh.load_history_strings()
|
747
|
+
|
748
|
+
def log_llm_history(self, role, content):
|
749
|
+
if not self.llm_history_file:
|
750
|
+
return
|
751
|
+
timestamp = datetime.now().isoformat(timespec="seconds")
|
752
|
+
with open(self.llm_history_file, "a", encoding="utf-8") as log_file:
|
753
|
+
log_file.write(f"{role.upper()} {timestamp}\n")
|
754
|
+
log_file.write(content + "\n")
|
755
|
+
|
756
|
+
def display_user_input(self, inp):
|
757
|
+
if self.pretty and self.user_input_color:
|
758
|
+
style = dict(style=self.user_input_color)
|
759
|
+
else:
|
760
|
+
style = dict()
|
761
|
+
|
762
|
+
self.console.print(Text(inp), **style)
|
763
|
+
|
764
|
+
def user_input(self, inp, log_only=True):
|
765
|
+
if not log_only:
|
766
|
+
self.display_user_input(inp)
|
767
|
+
|
768
|
+
prefix = "####"
|
769
|
+
if inp:
|
770
|
+
hist = inp.splitlines()
|
771
|
+
else:
|
772
|
+
hist = ["<blank>"]
|
773
|
+
|
774
|
+
hist = f" \n{prefix} ".join(hist)
|
775
|
+
|
776
|
+
hist = f"""
|
777
|
+
{prefix} {hist}"""
|
778
|
+
self.append_chat_history(hist, linebreak=True)
|
779
|
+
|
780
|
+
# OUTPUT
|
781
|
+
|
782
|
+
def ai_output(self, content):
|
783
|
+
hist = "\n" + content.strip() + "\n\n"
|
784
|
+
self.append_chat_history(hist)
|
785
|
+
|
786
|
+
def offer_url(self, url, prompt="Open URL for more info?", allow_never=True):
|
787
|
+
"""Offer to open a URL in the browser, returns True if opened."""
|
788
|
+
if url in self.never_prompts:
|
789
|
+
return False
|
790
|
+
if self.confirm_ask(prompt, subject=url, allow_never=allow_never):
|
791
|
+
webbrowser.open(url)
|
792
|
+
return True
|
793
|
+
return False
|
794
|
+
|
795
|
+
@restore_multiline
|
796
|
+
def confirm_ask(
|
797
|
+
self,
|
798
|
+
question,
|
799
|
+
default="y",
|
800
|
+
subject=None,
|
801
|
+
explicit_yes_required=False,
|
802
|
+
group=None,
|
803
|
+
allow_never=False,
|
804
|
+
):
|
805
|
+
self.num_user_asks += 1
|
806
|
+
|
807
|
+
# Ring the bell if needed
|
808
|
+
self.ring_bell()
|
809
|
+
|
810
|
+
question_id = (question, subject)
|
811
|
+
|
812
|
+
if question_id in self.never_prompts:
|
813
|
+
return False
|
814
|
+
|
815
|
+
if group and not group.show_group:
|
816
|
+
group = None
|
817
|
+
if group:
|
818
|
+
allow_never = True
|
819
|
+
|
820
|
+
valid_responses = ["yes", "no", "skip", "all"]
|
821
|
+
options = " (Y)es/(N)o"
|
822
|
+
if group:
|
823
|
+
if not explicit_yes_required:
|
824
|
+
options += "/(A)ll"
|
825
|
+
options += "/(S)kip all"
|
826
|
+
if allow_never:
|
827
|
+
options += "/(D)on't ask again"
|
828
|
+
valid_responses.append("don't")
|
829
|
+
|
830
|
+
if default.lower().startswith("y"):
|
831
|
+
question += options + " [Yes]: "
|
832
|
+
elif default.lower().startswith("n"):
|
833
|
+
question += options + " [No]: "
|
834
|
+
else:
|
835
|
+
question += options + f" [{default}]: "
|
836
|
+
|
837
|
+
if subject:
|
838
|
+
self.tool_output()
|
839
|
+
if "\n" in subject:
|
840
|
+
lines = subject.splitlines()
|
841
|
+
max_length = max(len(line) for line in lines)
|
842
|
+
padded_lines = [line.ljust(max_length) for line in lines]
|
843
|
+
padded_subject = "\n".join(padded_lines)
|
844
|
+
self.tool_output(padded_subject, bold=True)
|
845
|
+
else:
|
846
|
+
self.tool_output(subject, bold=True)
|
847
|
+
|
848
|
+
style = self._get_style()
|
849
|
+
|
850
|
+
def is_valid_response(text):
|
851
|
+
if not text:
|
852
|
+
return True
|
853
|
+
return text.lower() in valid_responses
|
854
|
+
|
855
|
+
if self.yes is True:
|
856
|
+
res = "n" if explicit_yes_required else "y"
|
857
|
+
elif self.yes is False:
|
858
|
+
res = "n"
|
859
|
+
elif group and group.preference:
|
860
|
+
res = group.preference
|
861
|
+
self.user_input(f"{question}{res}", log_only=False)
|
862
|
+
else:
|
863
|
+
while True:
|
864
|
+
try:
|
865
|
+
if self.prompt_session:
|
866
|
+
res = self.prompt_session.prompt(
|
867
|
+
question,
|
868
|
+
style=style,
|
869
|
+
complete_while_typing=False,
|
870
|
+
)
|
871
|
+
else:
|
872
|
+
res = input(question)
|
873
|
+
except EOFError:
|
874
|
+
# Treat EOF (Ctrl+D) as if the user pressed Enter
|
875
|
+
res = default
|
876
|
+
break
|
877
|
+
|
878
|
+
if not res:
|
879
|
+
res = default
|
880
|
+
break
|
881
|
+
res = res.lower()
|
882
|
+
good = any(valid_response.startswith(res) for valid_response in valid_responses)
|
883
|
+
if good:
|
884
|
+
break
|
885
|
+
|
886
|
+
error_message = f"Please answer with one of: {', '.join(valid_responses)}"
|
887
|
+
self.tool_error(error_message)
|
888
|
+
|
889
|
+
res = res.lower()[0]
|
890
|
+
|
891
|
+
if res == "d" and allow_never:
|
892
|
+
self.never_prompts.add(question_id)
|
893
|
+
hist = f"{question.strip()} {res}"
|
894
|
+
self.append_chat_history(hist, linebreak=True, blockquote=True)
|
895
|
+
return False
|
896
|
+
|
897
|
+
if explicit_yes_required:
|
898
|
+
is_yes = res == "y"
|
899
|
+
else:
|
900
|
+
is_yes = res in ("y", "a")
|
901
|
+
|
902
|
+
is_all = res == "a" and group is not None and not explicit_yes_required
|
903
|
+
is_skip = res == "s" and group is not None
|
904
|
+
|
905
|
+
if group:
|
906
|
+
if is_all and not explicit_yes_required:
|
907
|
+
group.preference = "all"
|
908
|
+
elif is_skip:
|
909
|
+
group.preference = "skip"
|
910
|
+
|
911
|
+
hist = f"{question.strip()} {res}"
|
912
|
+
self.append_chat_history(hist, linebreak=True, blockquote=True)
|
913
|
+
|
914
|
+
return is_yes
|
915
|
+
|
916
|
+
@restore_multiline
|
917
|
+
def prompt_ask(self, question, default="", subject=None):
|
918
|
+
self.num_user_asks += 1
|
919
|
+
|
920
|
+
# Ring the bell if needed
|
921
|
+
self.ring_bell()
|
922
|
+
|
923
|
+
if subject:
|
924
|
+
self.tool_output()
|
925
|
+
self.tool_output(subject, bold=True)
|
926
|
+
|
927
|
+
style = self._get_style()
|
928
|
+
|
929
|
+
if self.yes is True:
|
930
|
+
res = "yes"
|
931
|
+
elif self.yes is False:
|
932
|
+
res = "no"
|
933
|
+
else:
|
934
|
+
try:
|
935
|
+
if self.prompt_session:
|
936
|
+
res = self.prompt_session.prompt(
|
937
|
+
question + " ",
|
938
|
+
default=default,
|
939
|
+
style=style,
|
940
|
+
complete_while_typing=True,
|
941
|
+
)
|
942
|
+
else:
|
943
|
+
res = input(question + " ")
|
944
|
+
except EOFError:
|
945
|
+
# Treat EOF (Ctrl+D) as if the user pressed Enter
|
946
|
+
res = default
|
947
|
+
|
948
|
+
hist = f"{question.strip()} {res.strip()}"
|
949
|
+
self.append_chat_history(hist, linebreak=True, blockquote=True)
|
950
|
+
if self.yes in (True, False):
|
951
|
+
self.tool_output(hist)
|
952
|
+
|
953
|
+
return res
|
954
|
+
|
955
|
+
def _tool_message(self, message="", strip=True, color=None):
|
956
|
+
if message.strip():
|
957
|
+
if "\n" in message:
|
958
|
+
for line in message.splitlines():
|
959
|
+
self.append_chat_history(line, linebreak=True, blockquote=True, strip=strip)
|
960
|
+
else:
|
961
|
+
hist = message.strip() if strip else message
|
962
|
+
self.append_chat_history(hist, linebreak=True, blockquote=True)
|
963
|
+
|
964
|
+
if not isinstance(message, Text):
|
965
|
+
message = Text(message)
|
966
|
+
color = ensure_hash_prefix(color) if color else None
|
967
|
+
style = dict(style=color) if self.pretty and color else dict()
|
968
|
+
try:
|
969
|
+
self.console.print(message, **style)
|
970
|
+
except UnicodeEncodeError:
|
971
|
+
# Fallback to ASCII-safe output
|
972
|
+
if isinstance(message, Text):
|
973
|
+
message = message.plain
|
974
|
+
message = str(message).encode("ascii", errors="replace").decode("ascii")
|
975
|
+
self.console.print(message, **style)
|
976
|
+
|
977
|
+
def tool_error(self, message="", strip=True):
|
978
|
+
self.num_error_outputs += 1
|
979
|
+
self._tool_message(message, strip, self.tool_error_color)
|
980
|
+
|
981
|
+
def tool_warning(self, message="", strip=True):
|
982
|
+
self._tool_message(message, strip, self.tool_warning_color)
|
983
|
+
|
984
|
+
def tool_output(self, *messages, log_only=False, bold=False):
|
985
|
+
if messages:
|
986
|
+
hist = " ".join(messages)
|
987
|
+
hist = f"{hist.strip()}"
|
988
|
+
self.append_chat_history(hist, linebreak=True, blockquote=True)
|
989
|
+
|
990
|
+
if log_only:
|
991
|
+
return
|
992
|
+
|
993
|
+
messages = list(map(Text, messages))
|
994
|
+
style = dict()
|
995
|
+
if self.pretty:
|
996
|
+
if self.tool_output_color:
|
997
|
+
style["color"] = ensure_hash_prefix(self.tool_output_color)
|
998
|
+
style["reverse"] = bold
|
999
|
+
|
1000
|
+
style = RichStyle(**style)
|
1001
|
+
self.console.print(*messages, style=style)
|
1002
|
+
|
1003
|
+
def get_assistant_mdstream(self):
|
1004
|
+
mdargs = dict(style=self.assistant_output_color, code_theme=self.code_theme)
|
1005
|
+
mdStream = MarkdownStream(mdargs=mdargs)
|
1006
|
+
return mdStream
|
1007
|
+
|
1008
|
+
def assistant_output(self, message, pretty=None):
|
1009
|
+
if not message:
|
1010
|
+
self.tool_warning("Empty response received from LLM. Check your provider account?")
|
1011
|
+
return
|
1012
|
+
|
1013
|
+
show_resp = message
|
1014
|
+
|
1015
|
+
# Coder will force pretty off if fence is not triple-backticks
|
1016
|
+
if pretty is None:
|
1017
|
+
pretty = self.pretty
|
1018
|
+
|
1019
|
+
if pretty:
|
1020
|
+
show_resp = Markdown(
|
1021
|
+
message, style=self.assistant_output_color, code_theme=self.code_theme
|
1022
|
+
)
|
1023
|
+
else:
|
1024
|
+
show_resp = Text(message or "(empty response)")
|
1025
|
+
|
1026
|
+
self.console.print(show_resp)
|
1027
|
+
|
1028
|
+
def set_placeholder(self, placeholder):
|
1029
|
+
"""Set a one-time placeholder text for the next input prompt."""
|
1030
|
+
self.placeholder = placeholder
|
1031
|
+
|
1032
|
+
def print(self, message=""):
|
1033
|
+
print(message)
|
1034
|
+
|
1035
|
+
def llm_started(self):
|
1036
|
+
"""Mark that the LLM has started processing, so we should ring the bell on next input"""
|
1037
|
+
self.bell_on_next_input = True
|
1038
|
+
|
1039
|
+
def get_default_notification_command(self):
|
1040
|
+
"""Return a default notification command based on the operating system."""
|
1041
|
+
import platform
|
1042
|
+
|
1043
|
+
system = platform.system()
|
1044
|
+
|
1045
|
+
if system == "Darwin": # macOS
|
1046
|
+
# Check for terminal-notifier first
|
1047
|
+
if shutil.which("terminal-notifier"):
|
1048
|
+
return f"terminal-notifier -title 'Aider' -message '{NOTIFICATION_MESSAGE}'"
|
1049
|
+
# Fall back to osascript
|
1050
|
+
return (
|
1051
|
+
f'osascript -e \'display notification "{NOTIFICATION_MESSAGE}" with title "Aider"\''
|
1052
|
+
)
|
1053
|
+
elif system == "Linux":
|
1054
|
+
# Check for common Linux notification tools
|
1055
|
+
for cmd in ["notify-send", "zenity"]:
|
1056
|
+
if shutil.which(cmd):
|
1057
|
+
if cmd == "notify-send":
|
1058
|
+
return f"notify-send 'Aider' '{NOTIFICATION_MESSAGE}'"
|
1059
|
+
elif cmd == "zenity":
|
1060
|
+
return f"zenity --notification --text='{NOTIFICATION_MESSAGE}'"
|
1061
|
+
return None # No known notification tool found
|
1062
|
+
elif system == "Windows":
|
1063
|
+
# PowerShell notification
|
1064
|
+
return (
|
1065
|
+
"powershell -command"
|
1066
|
+
" \"[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms');"
|
1067
|
+
f" [System.Windows.Forms.MessageBox]::Show('{NOTIFICATION_MESSAGE}',"
|
1068
|
+
" 'Aider')\""
|
1069
|
+
)
|
1070
|
+
|
1071
|
+
return None # Unknown system
|
1072
|
+
|
1073
|
+
def ring_bell(self):
|
1074
|
+
"""Ring the terminal bell if needed and clear the flag"""
|
1075
|
+
if self.bell_on_next_input and self.notifications:
|
1076
|
+
if self.notifications_command:
|
1077
|
+
try:
|
1078
|
+
result = subprocess.run(
|
1079
|
+
self.notifications_command, shell=True, capture_output=True
|
1080
|
+
)
|
1081
|
+
if result.returncode != 0 and result.stderr:
|
1082
|
+
error_msg = result.stderr.decode("utf-8", errors="replace")
|
1083
|
+
self.tool_warning(f"Failed to run notifications command: {error_msg}")
|
1084
|
+
except Exception as e:
|
1085
|
+
self.tool_warning(f"Failed to run notifications command: {e}")
|
1086
|
+
else:
|
1087
|
+
print("\a", end="", flush=True) # Ring the bell
|
1088
|
+
self.bell_on_next_input = False # Clear the flag
|
1089
|
+
|
1090
|
+
def toggle_multiline_mode(self):
|
1091
|
+
"""Toggle between normal and multiline input modes"""
|
1092
|
+
self.multiline_mode = not self.multiline_mode
|
1093
|
+
if self.multiline_mode:
|
1094
|
+
self.tool_output(
|
1095
|
+
"Multiline mode: Enabled. Enter inserts newline, Alt-Enter submits text"
|
1096
|
+
)
|
1097
|
+
else:
|
1098
|
+
self.tool_output(
|
1099
|
+
"Multiline mode: Disabled. Alt-Enter inserts newline, Enter submits text"
|
1100
|
+
)
|
1101
|
+
|
1102
|
+
def append_chat_history(self, text, linebreak=False, blockquote=False, strip=True):
|
1103
|
+
if blockquote:
|
1104
|
+
if strip:
|
1105
|
+
text = text.strip()
|
1106
|
+
text = "> " + text
|
1107
|
+
if linebreak:
|
1108
|
+
if strip:
|
1109
|
+
text = text.rstrip()
|
1110
|
+
text = text + " \n"
|
1111
|
+
if not text.endswith("\n"):
|
1112
|
+
text += "\n"
|
1113
|
+
if self.chat_history_file is not None:
|
1114
|
+
try:
|
1115
|
+
with self.chat_history_file.open("a", encoding=self.encoding, errors="ignore") as f:
|
1116
|
+
f.write(text)
|
1117
|
+
except (PermissionError, OSError) as err:
|
1118
|
+
print(f"Warning: Unable to write to chat history file {self.chat_history_file}.")
|
1119
|
+
print(err)
|
1120
|
+
self.chat_history_file = None # Disable further attempts to write
|
1121
|
+
|
1122
|
+
def format_files_for_input(self, rel_fnames, rel_read_only_fnames):
|
1123
|
+
if not self.pretty:
|
1124
|
+
read_only_files = []
|
1125
|
+
for full_path in sorted(rel_read_only_fnames or []):
|
1126
|
+
read_only_files.append(f"{full_path} (read only)")
|
1127
|
+
|
1128
|
+
editable_files = []
|
1129
|
+
for full_path in sorted(rel_fnames):
|
1130
|
+
if full_path in rel_read_only_fnames:
|
1131
|
+
continue
|
1132
|
+
editable_files.append(f"{full_path}")
|
1133
|
+
|
1134
|
+
return "\n".join(read_only_files + editable_files) + "\n"
|
1135
|
+
|
1136
|
+
output = StringIO()
|
1137
|
+
console = Console(file=output, force_terminal=False)
|
1138
|
+
|
1139
|
+
read_only_files = sorted(rel_read_only_fnames or [])
|
1140
|
+
editable_files = [f for f in sorted(rel_fnames) if f not in rel_read_only_fnames]
|
1141
|
+
|
1142
|
+
if read_only_files:
|
1143
|
+
# Use shorter of abs/rel paths for readonly files
|
1144
|
+
ro_paths = []
|
1145
|
+
for rel_path in read_only_files:
|
1146
|
+
abs_path = os.path.abspath(os.path.join(self.root, rel_path))
|
1147
|
+
ro_paths.append(Text(abs_path if len(abs_path) < len(rel_path) else rel_path))
|
1148
|
+
|
1149
|
+
files_with_label = [Text("Readonly:")] + ro_paths
|
1150
|
+
read_only_output = StringIO()
|
1151
|
+
Console(file=read_only_output, force_terminal=False).print(Columns(files_with_label))
|
1152
|
+
read_only_lines = read_only_output.getvalue().splitlines()
|
1153
|
+
console.print(Columns(files_with_label))
|
1154
|
+
|
1155
|
+
if editable_files:
|
1156
|
+
text_editable_files = [Text(f) for f in editable_files]
|
1157
|
+
files_with_label = text_editable_files
|
1158
|
+
if read_only_files:
|
1159
|
+
files_with_label = [Text("Editable:")] + text_editable_files
|
1160
|
+
editable_output = StringIO()
|
1161
|
+
Console(file=editable_output, force_terminal=False).print(Columns(files_with_label))
|
1162
|
+
editable_lines = editable_output.getvalue().splitlines()
|
1163
|
+
|
1164
|
+
if len(read_only_lines) > 1 or len(editable_lines) > 1:
|
1165
|
+
console.print()
|
1166
|
+
console.print(Columns(files_with_label))
|
1167
|
+
|
1168
|
+
return output.getvalue()
|
1169
|
+
|
1170
|
+
|
1171
|
+
def get_rel_fname(fname, root):
|
1172
|
+
try:
|
1173
|
+
return os.path.relpath(fname, root)
|
1174
|
+
except ValueError:
|
1175
|
+
return fname
|