From 9934518c112ba027d1e443bfa1e93245e63a452d Mon Sep 17 00:00:00 2001 From: rfprod Date: Thu, 8 Oct 2026 13:44:26 +0400 Subject: [PATCH] feat(cli): add support for interactive mode for model querying - [x] implement `next_prompt` helper to handle user input and quit commands; - [x] add `--interactive` flag to enable continuous prompting session; - [x] implement main loop for interactive model interaction; - [x] fix incorrect boolean checks for `starts_with` during argument parsing; --- src/main.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index aa62c42..d35090b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,19 +1,40 @@ #include "ollama_client.hpp" #include +bool next_prompt(std::string* prompt) { + bool done = false; + printf("ā³ Type your prompt (q/Q to quit): "); + if (!std::getline(std::cin, *prompt, '\n')) { + fprintf(stderr, "Error reading user input.\n"); + exit(1); + } + + if (*prompt == "q" || *prompt == "Q") { + done = true; + } + + return done; +} + int main(int argc, const char* argv[]) { std::string model = "gemma3n:latest"; std::string prompt = "Hey! What's up!"; bool stream = false; + bool interactive = false; + + bool done = false; + std::string arg{}; for (int i = 1; i < argc; i += 1) { - std::string arg = argv[i]; - if (arg.starts_with("--model=") == 0) { + arg = argv[i]; + if (arg.starts_with("--model=") == true) { model = arg.substr(8); - } else if (arg.starts_with("--prompt=") == 0) { + } else if (arg.starts_with("--prompt=") == true) { prompt = arg.substr(9); - } else if (arg.starts_with("--stream=") == 0) { + } else if (arg.starts_with("--stream=") == true) { stream = arg.substr(9) == "true" ? true : false; + } else if (arg.starts_with("--interactive=") == true) { + interactive = arg.substr(14) == "true" ? true : false; } } @@ -21,11 +42,32 @@ int main(int argc, const char* argv[]) { printf("šŸ›ˆ model = %s\n", model.c_str()); printf("šŸ›ˆ prompt = %s\n", prompt.c_str()); printf("šŸ›ˆ stream = %s\n", stream ? "true" : "false"); + printf("šŸ›ˆ interactive = %s\n", interactive ? "true" : "false"); printf("\n"); - if (prompt.length() == 0) { - printf("Enter your prompt: "); - getline(std::cin, prompt); + if (interactive == true) { + while (prompt.length() == 0) { + done = next_prompt(&prompt); + + if (done == true) { + printf("\nšŸ The session has ended.\n"); + exit(0); + } + } + } else { + if (prompt.length() == 0) { + done = next_prompt(&prompt); + + if (prompt.length() == 0) { + printf("\nšŸ Empty prompt. The session has ended.\n"); + exit(0); + } + + if (done == true) { + printf("\nšŸ The session has ended.\n"); + exit(0); + } + } } OllamaClient client = OllamaClient(); @@ -39,5 +81,19 @@ int main(int argc, const char* argv[]) { client.query_model(config); + if (interactive == true) { + while (done == false) { + done = next_prompt(&prompt); + if (done == true) { + continue; + } + + config.prompt = prompt; + client.query_model(config); + } + } + + printf("\nšŸ The session has ended.\n"); + return 0; }