(
session: ChatSession, style: Style, use_color: bool, kb_dir: Path
)
| 268 | |
| 269 | |
| 270 | def _make_prompt_session( |
| 271 | session: ChatSession, style: Style, use_color: bool, kb_dir: Path |
| 272 | ) -> PromptSession: |
| 273 | from prompt_toolkit.filters import has_completions |
| 274 | from prompt_toolkit.history import FileHistory |
| 275 | from prompt_toolkit.key_binding import KeyBindings |
| 276 | |
| 277 | kb = KeyBindings() |
| 278 | |
| 279 | @kb.add("tab", filter=has_completions) |
| 280 | def _accept_completion(event: Any) -> None: |
| 281 | """Tab accepts the current completion (like zsh), not cycle.""" |
| 282 | buf = event.current_buffer |
| 283 | state = buf.complete_state |
| 284 | if not state: |
| 285 | return |
| 286 | # Only one candidate or already selected — accept immediately |
| 287 | if state.current_completion: |
| 288 | buf.apply_completion(state.current_completion) |
| 289 | elif len(state.completions) == 1: |
| 290 | buf.apply_completion(state.completions[0]) |
| 291 | else: |
| 292 | # Multiple candidates, nothing selected — highlight first |
| 293 | buf.go_to_completion(0) |
| 294 | |
| 295 | @kb.add("tab", filter=~has_completions) |
| 296 | def _trigger_completion(event: Any) -> None: |
| 297 | """Tab triggers completion when menu is not open.""" |
| 298 | buf = event.current_buffer |
| 299 | buf.start_completion() |
| 300 | |
| 301 | history_path = kb_dir / ".openkb" / "chat_history" |
| 302 | return PromptSession( |
| 303 | message=FormattedText([("class:prompt", ">>> ")]), |
| 304 | style=style, |
| 305 | completer=_ChatCompleter(), |
| 306 | complete_style=CompleteStyle.MULTI_COLUMN, |
| 307 | complete_while_typing=False, |
| 308 | key_bindings=kb, |
| 309 | history=FileHistory(str(history_path)), |
| 310 | bottom_toolbar=(lambda: _bottom_toolbar(session)) if use_color else None, |
| 311 | ) |
| 312 | |
| 313 | |
| 314 | def _make_rich_console() -> Any: |
no test coverage detected