Run the chat REPL against ``session`` until the user exits.
(
kb_dir: Path,
session: ChatSession,
*,
no_color: bool = False,
raw: bool = False,
)
| 893 | |
| 894 | |
| 895 | async def run_chat( |
| 896 | kb_dir: Path, |
| 897 | session: ChatSession, |
| 898 | *, |
| 899 | no_color: bool = False, |
| 900 | raw: bool = False, |
| 901 | ) -> None: |
| 902 | """Run the chat REPL against ``session`` until the user exits.""" |
| 903 | from openkb.config import load_config |
| 904 | |
| 905 | use_color = _use_color(force_off=no_color) |
| 906 | style = _build_style(use_color) |
| 907 | |
| 908 | config = load_config(kb_dir / ".openkb" / "config.yaml") |
| 909 | language = session.language or config.get("language", "en") |
| 910 | agent = build_chat_agent(kb_dir, session.model, language=language) |
| 911 | |
| 912 | _print_header(session, kb_dir, style) |
| 913 | if session.turn_count > 0: |
| 914 | _print_resume_view(session, style) |
| 915 | |
| 916 | prompt_session = _make_prompt_session(session, style, use_color, kb_dir) |
| 917 | |
| 918 | last_sigint = 0.0 |
| 919 | |
| 920 | while True: |
| 921 | try: |
| 922 | user_input = await prompt_session.prompt_async() |
| 923 | last_sigint = 0.0 |
| 924 | except KeyboardInterrupt: |
| 925 | now = time.monotonic() |
| 926 | if last_sigint and (now - last_sigint) < _SIGINT_EXIT_WINDOW: |
| 927 | _fmt(style, ("class:header", "\nBye. Thanks for using OpenKB.\n\n")) |
| 928 | return |
| 929 | last_sigint = now |
| 930 | _fmt(style, ("class:header", "\n(Press Ctrl-C again to exit)\n")) |
| 931 | continue |
| 932 | except EOFError: |
| 933 | _fmt(style, ("class:header", "Bye. Thanks for using OpenKB.\n\n")) |
| 934 | return |
| 935 | |
| 936 | user_input = (user_input or "").strip() |
| 937 | if not user_input: |
| 938 | continue |
| 939 | |
| 940 | if user_input.startswith("/"): |
| 941 | try: |
| 942 | action = await _handle_slash(user_input, kb_dir, session, style) |
| 943 | except KeyboardInterrupt: |
| 944 | _fmt(style, ("class:error", "\n[aborted]\n")) |
| 945 | continue |
| 946 | if action == "exit": |
| 947 | return |
| 948 | if action == "new_session": |
| 949 | session = ChatSession.new(kb_dir, session.model, session.language) |
| 950 | agent = build_chat_agent(kb_dir, session.model, language=language) |
| 951 | prompt_session = _make_prompt_session(session, style, use_color, kb_dir) |
| 952 | continue |