Skip to content

Commit f364b3e

Browse files
committed
Add tab completion for slash commands and file paths
- Tab on / prefix completes slash commands (/status, /list, etc.) - Tab after /add completes file paths with expanduser support - Handles quoted paths (strips leading quote before path completion)
1 parent b85e3fb commit f364b3e

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

openkb/agent/chat.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from typing import Any
1717

1818
from prompt_toolkit import PromptSession
19+
from prompt_toolkit.completion import Completer, Completion, PathCompleter
20+
from prompt_toolkit.document import Document
1921
from prompt_toolkit.formatted_text import FormattedText
2022
from prompt_toolkit.shortcuts import print_formatted_text
2123
from prompt_toolkit.styles import Style
@@ -186,10 +188,46 @@ def _bottom_toolbar(session: ChatSession) -> FormattedText:
186188
)
187189

188190

191+
_SLASH_COMMANDS = [
192+
"/exit", "/quit", "/help", "/clear", "/save",
193+
"/status", "/list", "/lint", "/add",
194+
]
195+
196+
197+
class _ChatCompleter(Completer):
198+
"""Complete slash commands and file paths after /add."""
199+
200+
def __init__(self) -> None:
201+
self._path_completer = PathCompleter(expanduser=True)
202+
203+
def get_completions(self, document: Document, complete_event: Any) -> Any:
204+
text = document.text_before_cursor
205+
206+
# After "/add ", complete file paths
207+
if text.lstrip().lower().startswith("/add "):
208+
# Extract the path portion after "/add "
209+
prefix = text.lstrip()
210+
add_pos = prefix.lower().index("/add ") + 5
211+
path_text = prefix[add_pos:]
212+
# Strip surrounding quotes if the user started one
213+
if path_text and path_text[0] in ("'", '"'):
214+
path_text = path_text[1:]
215+
path_doc = Document(path_text, len(path_text))
216+
yield from self._path_completer.get_completions(path_doc, complete_event)
217+
return
218+
219+
# Complete slash commands
220+
if text.startswith("/"):
221+
for cmd in _SLASH_COMMANDS:
222+
if cmd.startswith(text.lower()):
223+
yield Completion(cmd, start_position=-len(text))
224+
225+
189226
def _make_prompt_session(session: ChatSession, style: Style, use_color: bool) -> PromptSession:
190227
return PromptSession(
191228
message=FormattedText([("class:prompt", ">>> ")]),
192229
style=style,
230+
completer=_ChatCompleter(),
193231
bottom_toolbar=(lambda: _bottom_toolbar(session)) if use_color else None,
194232
)
195233

0 commit comments

Comments
 (0)