Handle /quit and /files
Exit

Handle /quit and /files

Add slash command detection to chat_loop with /quit to exit and /files to list indexed files

💻

Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.

Giving users control over the loop

Right now the only way to exit the assistant is to press Ctrl+C. There is no way to see which files were indexed without looking at the startup output. Slash commands fix both problems.

A slash command is any input that starts with /. Before routing user input to the search pipeline, the loop checks whether the input starts with /. If it does, the command is handled and the loop continues — the AI pipeline is skipped entirely.

You'll add two commands in this chapter:

  • /quit: prints "Goodbye!" and breaks out of the loop, ending the program cleanly
  • /files: lists every file that was indexed, one per line

An else clause catches any unrecognized command and tells the user to type /help.

Instructions

  1. After if not question: continue, add if question.startswith("/"):. All slash command handling goes inside this block.
  2. Inside the slash command block:
    • Add parts = question.split(maxsplit=1) — this splits the input into at most two parts, separating the command from any arguments that follow it.
    • Add command = parts[0] to store just the command name.
  3. Add if command == "/quit":, then print("Goodbye!"), then break.
  4. Add elif command == "/files":. Inside it:
    • Add print("Indexed files:").
    • Add for name in file_list:.
    • Inside the loop, add print(f" {name}").
  5. Add else: with print(f"Unknown command: {command}"), then print("Type /help to see available commands.").
  6. After the entire if/elif/else block (still inside the if question.startswith("/"): block), add continue. This skips the search and prompt-building code below — slash commands don't send anything to the AI.