src/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
0 / 167
0.0%
Branches:
0 / 484
0.0%
Functions:
0 / 15
0.0%

ollama/ollama_client.cpp
Line Branch Exec Source
1 #include "ollama_client.hpp"
2 #include <algorithm>
3 #include <cstdlib>
4 #include <curl/curl.h>
5 #include <format>
6 #include <iostream>
7 #include <nlohmann/json.hpp>
8 #include <sstream>
9 #include <string>
10
11 using nlohmann::json_abi_v3_11_2::json;
12
13 /**
14 * Constructs an OllamaClient connected to a UserInterface.
15 *
16 * Initializes the Ollama API endpoint URLs, verifies the Ollama binary
17 * is installed, and creates a ProgressSpinner for network operations.
18 *
19 * @param config Pointer to the Ollama Client configuration options.
20 * @param ui Pointer to the UserInterface for displaying output.
21 * @param curl Pointer to a CurlUtils instance for HTTP communication with Qdrant.
22 * @param system_utils Pointer to a SustemUtils instance for `system()` call status handling.
23 *
24 * @note Calls check_ollama() during construction. Exits if Ollama is not found.
25 */
26 ✗ OllamaClient::OllamaClient(std::shared_ptr<OllamaClientConfig> config, std::shared_ptr<UserInterface> ui,
27 ✗ std::shared_ptr<CurlUtils> curl, std::shared_ptr<SystemUtils> system_utils)
28 ✗ : config(config), ui(ui), curl(curl), system_utils(system_utils) {
29 this->urls = {
30 generate_url : "http://localhost:11434/api/generate",
31 tags_url : "http://localhost:11434/api/tags",
32 embed_url : "http://localhost:11434/api/embed"
33 ✗ };
34
35 ✗ this->check_ollama();
36
37
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
38
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ UserInterface::print_message_noninteractive("\n");
39 }
40
0/12
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ }
41
42 /**
43 * Destroys the OllamaClient instance and clears the Ollama history file.
44 *
45 * Runs a shell command that overwrites ~/.ollama/history with an empty string, truncating its contents.
46 * Passes the return status of the shell command to system_utils->catch_error() for handling.
47 *
48 * Note: This destructor depends on system_utils remaining valid for the duration of destruction, and it relies on
49 * the shell to expand the ~ path. Because it runs a shell command, it will also fail silently in environments
50 * where system() is unavailable or where the history file does not exist.
51 */
52 ✗ OllamaClient::~OllamaClient() {
53 ✗ int status = system("echo \"\" > ~/.ollama/history");
54 ✗ this->system_utils->catch_error(status);
55 ✗ }
56
57 /**
58 * Retrieves a configuration value by property name as a const reference of the requested type.
59 *
60 * @tparam T: Type of the configuration value to return. Supported types are std::string for "model", "embedding_model",
61 * and "prompt", and bool for "stream".
62 * @param property_name: Name of the configuration property to look up.
63 * @return: Const reference to the matching member of config, valid for as long as config is alive.
64 * @throws std::runtime_error: If property_name is not a supported property, or if T does not match the property's type.
65 *
66 * Compares property_name against each supported property in turn. For a matching name, returns a reference to the
67 * corresponding config member only when T matches that member's type. The type checks are resolved at compile time
68 * with if constexpr, so each instantiation contains only the branch for its type.
69 *
70 * Note: If the name matches but T does not, control falls through to the remaining checks and ends at the exception.
71 * The returned reference refers into config, so it must not outlive the OllamaClient instance.
72 */
73 ✗ template <typename T> const T& OllamaClient::get_config_value(const std::string& property_name) const {
74
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (property_name == "model") {
75 if constexpr (std::is_same_v<T, std::string>) {
76 ✗ return this->config->model;
77 }
78 }
79
80
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (property_name == "embedding_model") {
81 if constexpr (std::is_same_v<T, std::string>) {
82 ✗ return this->config->embedding_model;
83 }
84 }
85
86
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (property_name == "prompt") {
87 if constexpr (std::is_same_v<T, std::string>) {
88 ✗ return this->config->prompt;
89 }
90 }
91
92
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (property_name == "stream") {
93 if constexpr (std::is_same_v<T, bool>) {
94 ✗ return this->config->stream;
95 }
96 }
97
98
0/8
bool const& OllamaClient::get_config_value<bool>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const:
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& OllamaClient::get_config_value<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const:
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ throw std::runtime_error("Property not supported: " + property_name);
99 }
100 template const std::string& OllamaClient::get_config_value<std::string>(const std::string&) const;
101 template const bool& OllamaClient::get_config_value<bool>(const std::string&) const;
102
103 /**
104 * Retrieves a URL value by property name as a const reference to a std::string.
105 *
106 * @param property_name: Name of the URL property to look up; supported values are "generate_url" and "embed_url".
107 * @return: Const reference to the matching URL string stored in the urls member, valid for as long as this OllamaClient
108 * instance is alive.
109 * @throws std::runtime_error: If property_name is not a supported URL property.
110 *
111 * Compares property_name against "generate_url" and then "embed_url", returning a reference to the corresponding field
112 * of urls on a match.
113 *
114 * Note: The returned reference refers into urls, so it must not outlive the OllamaClient instance. Unlike
115 * get_config_value(), this method has no type parameter because every URL property is a std::string.
116 */
117 ✗ const std::string& OllamaClient::get_url_value(const std::string& property_name) const {
118
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (property_name == "generate_url") {
119 ✗ return this->urls.generate_url;
120 }
121
122
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (property_name == "embed_url") {
123 ✗ return this->urls.embed_url;
124 }
125
126
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ throw std::runtime_error("Property not supported: " + property_name);
127 }
128
129 /**
130 * Verifies that the `is available on the system.
131 *
132 * Uses the `which ollama` command to check if Ollama is in the PATH.
133 * - If found: displays a checkmark and continues.
134 * - If not found: displays an error message with installation instructions
135 * and exits or ends the session.
136 *
137 * Output is routed through the UserInterface (if available) or stdout/stderr.
138 *
139 * Displays a checkmark on success.
140 */
141 ✗ void OllamaClient::check_ollama() {
142 ✗ std::string message{};
143
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ if (system("which ollama > /dev/null 2>&1")) {
144 message = "Ollama binary does not exist.\nInstallation instructions: "
145
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ "https://docs.ollama.com/linux\n";
146
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
147
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_error_noninteractive(message);
148 } else {
149
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_error(message);
150 }
151
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->end_session(EXIT_FAILURE);
152 } else {
153
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ message = "Ollama binary exists.\n";
154
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
155
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_success_noninteractive(message);
156 } else {
157
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_success(message);
158 }
159 }
160 ✗ };
161
162 /**
163 * Removes leading and trailing whitespace from a string.
164 *
165 * Uses std::find_if with lambda predicates to efficiently trim both ends
166 * without creating unnecessary intermediate strings.
167 *
168 * @param response The input string.
169 * @return The trimmed string.
170 */
171 ✗ std::string OllamaClient::trim_response(std::string response) {
172
0/2
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ response.erase(response.begin(),
173
0/2
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ std::find_if(response.begin(), response.end(), [](unsigned char ch) { return !std::isspace(ch); }));
174
175
0/2
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ response.erase(
176
0/2
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ std::find_if(response.rbegin(), response.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(),
177 ✗ response.end());
178 ✗ return response;
179 }
180
181 /**
182 * Parses and displays a complete JSON response from Ollama.
183 *
184 * Extracts the "response" field, trims whitespace, and displays it with
185 * a llama emoji header and separator. On JSON parse error, displays an
186 * error message and exits or ends the session.
187 *
188 * Output is routed through the UserInterface or stdout/stderr.
189 *
190 * @param response The complete JSON response from the Ollama API.
191 */
192 ✗ void OllamaClient::process_response(std::string response) {
193 try {
194
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ json response_json = json::parse(response);
195
196
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
197 std::string message = std ::format(
198
0/8
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ "🦙 llama:\n{}\n---\n", this->trim_response(response_json["response"].get<std::string>()).c_str());
199
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_message_noninteractive(message);
200 ✗ } else {
201
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ this->ui->print_message("🦙 llama:");
202
0/8
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ this->ui->print_message(this->trim_response(response_json["response"].get<std::string>()));
203
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ this->ui->print_message("---");
204 }
205
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ } catch (const json::exception& err) {
206
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ std::string message = std::format("JSON parse error: {}\n", err.what());
207
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
208
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_error_noninteractive(message);
209 } else {
210
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_error(message);
211 }
212
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->end_session(EXIT_FAILURE);
213 ✗ }
214 ✗ }
215
216 /**
217 * Parses one line of a streamed Ollama response and displays its text fragment.
218 *
219 * @param line: One JSON-encoded chunk from the streaming response, expected to contain a "response" string and a "done"
220 * boolean.
221 *
222 * Parses line as JSON and extracts the "response" text fragment. A static flag tracks whether this is the first chunk
223 * of the current response. The first chunk starts a new message entry, and later chunks are appended to it. The text is
224 * shown through the UI when it is running, or printed non-interactively otherwise. When the "done" field is true, the
225 * flag is reset so the next response starts a new entry, and the method returns.
226 *
227 * Parse failures and other json::exception errors are caught and reported through print_error_noninteractive(),
228 * including the error ID and the offending line.
229 *
230 * Note: When compiled with DEBUG defined, the first 100 characters of each line are logged if the UI is not running.
231 * The first_chunk flag is static, so it is shared by all OllamaClient instances. If a stream ends or fails to parse
232 * before a chunk with "done" set to true, the flag is not reset and the next response will be appended to the previous
233 * message.
234 */
235 ✗ void OllamaClient::process_response_stream(const std::string& line) {
236 #ifdef DEBUG
237
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (this->ui->backend_running() == false) {
238
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_message_noninteractive(
239
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ std::format("\t[DEBUG] process_response_stream called with line: {}\n", line.substr(0, 100)));
240 }
241 #endif
242
243 try {
244
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ json chunk_json = json::parse(line);
245
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ std::string text = chunk_json["response"].get<std::string>();
246
247 // First chunk: stream=false (new entry), subsequent chunks: stream=true (append)
248 static bool first_chunk = true;
249 ✗ bool is_streaming = !first_chunk;
250 ✗ first_chunk = false;
251
252
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
253
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_message_noninteractive(text);
254 } else {
255
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_message(text, is_streaming);
256 }
257
258
0/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ if (chunk_json["done"].get<bool>()) {
259 ✗ first_chunk = true; // Reset for next response
260 ✗ return;
261 }
262
0/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ } catch (const json::exception& err) {
263 std::string message = std::format("JSON parse error in process_response_stream: {}\nError ID: {}\nLine was: {}",
264
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ err.what(), err.id, line);
265
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_error_noninteractive(message);
266 ✗ }
267 }
268
269 /**
270 * Generates embeddings for a list of text strings using the Ollama embedding model.
271 *
272 * This method sends each text to the Ollama embedding endpoint and retrieves
273 * the corresponding embedding vectors. Embeddings are processed sequentially.
274 *
275 * @param texts A vector of text strings to embed
276 * @param config_opt Configuration object specifying the embedding model to use
277 *
278 * @return A vector of embedding vectors, where each inner vector contains
279 * the float values representing the embedding for the corresponding text.
280 * The order of embeddings matches the order of input texts.
281 *
282 * @throws std::exception if the web request fails or the response cannot be parsed
283 */
284 ✗ std::vector<std::vector<float>> OllamaClient::generate_embeddings(const std::vector<std::string>& texts,
285 std::optional<OllamaClientConfig> config_opt) {
286 ✗ std::vector<std::vector<float>> embeddings{};
287
288 json request_payload = {
289
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ {"model", config_opt != std::nullopt ? config_opt->embedding_model : config->embedding_model}, {"input", ""}};
290
291
0/2
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ for (const auto& text : texts) {
292
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ request_payload["input"] = text;
293
294
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ std::string post_data = request_payload.dump();
295
296
0/4
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ std::string response = this->curl->fetch_web_content(this->urls.embed_url, post_data);
297
298 #ifdef DEBUG
299
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
300
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_message_noninteractive(
301
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ std::format("\t[DEBUG] generate_embeddings response {}\n", response));
302 }
303 #endif
304
305
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ const json data = json::parse(response);
306
307
0/8
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ embeddings.push_back(data["embeddings"][0].get<std::vector<float>>());
308 ✗ }
309
310 ✗ return embeddings;
311
0/36
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ }
312
313 /**
314 * Sends a prompt query to the Ollama model and processes the response.
315 *
316 * Displays the formatted user message to either the console or UI based on
317 * the UI's running state, then constructs a JSON request payload with the
318 * model, prompt, and stream settings. Fetches the response from the Ollama
319 * server and processes it either as a stream or as a complete response.
320 *
321 * @param config_opt The prompt configuration containing the model name, prompt
322 * text, and stream preference
323 */
324 ✗ void OllamaClient::query_model(std::optional<OllamaClientConfig> config_opt) {
325 std::string message =
326
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ std::format("💬 user: {}\n", config_opt != std::nullopt ? config_opt->prompt.c_str() : config->prompt.c_str());
327
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
328
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_message_noninteractive(message);
329 } else {
330
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_message(message);
331 }
332
333 ✗ json request_payload;
334
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (config_opt != std::nullopt) {
335 request_payload = {
336
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ {"model", config_opt->model}, {"prompt", config_opt->prompt}, {"stream", config_opt->stream}};
337 } else {
338
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ request_payload = {{"model", config->model}, {"prompt", config->prompt}, {"stream", config->stream}};
339 }
340
341
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ std::string post_data = request_payload.dump();
342
343
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ bool stream = config_opt != std::nullopt ? config_opt->stream : config->stream;
344
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (stream) {
345 WebContentStream response =
346 ✗ this->curl->fetch_web_content_stream(this->urls.generate_url, post_data, [this](const std::string& chunk) {
347 ✗ this->process_response_stream(chunk);
348
0/4
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ });
349
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ message = std::format("Stream successful: {}, Status: {}\n", response.success, response.status);
350
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
351
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_message_noninteractive(message);
352 } else {
353
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_message(message);
354 }
355 ✗ } else {
356
0/4
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ std::string response = this->curl->fetch_web_content(this->urls.generate_url, post_data);
357
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ this->process_response(response);
358 ✗ }
359
0/92
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✗ Branch 48 not taken.
✗ Branch 49 not taken.
✗ Branch 52 not taken.
✗ Branch 53 not taken.
✗ Branch 55 not taken.
✗ Branch 56 not taken.
✗ Branch 58 not taken.
✗ Branch 59 not taken.
✗ Branch 63 not taken.
✗ Branch 64 not taken.
✗ Branch 66 not taken.
✗ Branch 67 not taken.
✗ Branch 68 not taken.
✗ Branch 69 not taken.
✗ Branch 70 not taken.
✗ Branch 71 not taken.
✗ Branch 72 not taken.
✗ Branch 73 not taken.
✗ Branch 77 not taken.
✗ Branch 78 not taken.
✗ Branch 79 not taken.
✗ Branch 80 not taken.
✗ Branch 81 not taken.
✗ Branch 82 not taken.
✗ Branch 84 not taken.
✗ Branch 85 not taken.
✗ Branch 86 not taken.
✗ Branch 87 not taken.
✗ Branch 88 not taken.
✗ Branch 89 not taken.
✗ Branch 91 not taken.
✗ Branch 92 not taken.
✗ Branch 93 not taken.
✗ Branch 94 not taken.
✗ Branch 95 not taken.
✗ Branch 96 not taken.
✗ Branch 98 not taken.
✗ Branch 99 not taken.
✗ Branch 100 not taken.
✗ Branch 101 not taken.
✗ Branch 103 not taken.
✗ Branch 104 not taken.
✗ Branch 105 not taken.
✗ Branch 106 not taken.
✗ Branch 107 not taken.
✗ Branch 108 not taken.
✗ Branch 110 not taken.
✗ Branch 111 not taken.
✗ Branch 112 not taken.
✗ Branch 113 not taken.
✗ Branch 114 not taken.
✗ Branch 115 not taken.
✗ Branch 117 not taken.
✗ Branch 118 not taken.
✗ Branch 119 not taken.
✗ Branch 120 not taken.
✗ Branch 121 not taken.
✗ Branch 122 not taken.
✗ Branch 124 not taken.
✗ Branch 125 not taken.
✗ Branch 126 not taken.
✗ Branch 127 not taken.
✗ }
360
361 /**
362 * Retrieves a list of available models from the Ollama server.
363 *
364 * Fetches the models list from the Ollama tags endpoint and parses the JSON
365 * response. Validates that the response contains a valid models array. Extracts
366 * and displays model metadata including name, modification timestamp, and size
367 * (converted from bytes to megabytes). Collects valid model names into a vector
368 * for return. Handles errors (invalid response structure or JSON parsing failures)
369 * by terminating with an error message directed to either stderr or the UI based
370 * on the UI's running state.
371 *
372 * @return A vector of available model names extracted from the server response.
373 * Returns an empty vector if an error occurs during fetch or parsing.
374 */
375 ✗ std::vector<std::string> OllamaClient::query_model_list() {
376
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ UserInterface::print_message_noninteractive("running query_model_list");
377 ✗ std::string message{};
378 ✗ std::vector<std::string> models;
379
380
0/2
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ std::string response = this->curl->fetch_web_content(this->urls.tags_url);
381
382 try {
383
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ const json data = json::parse(response);
384
385
0/10
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ if (!data.contains("models") || !data["models"].is_array()) {
386
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ message = "Response has no models array.\n";
387
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
388
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_error_noninteractive(message);
389 } else {
390
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_error(message);
391 }
392
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->end_session(EXIT_FAILURE);
393 }
394
395
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ message = "📃 available models:\n";
396
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
397
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_message_noninteractive(message);
398 } else {
399
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_message(message);
400 }
401
402
0/10
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ for (const auto& model : data["models"]) {
403
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ const std::string name = model.value("name", "unknown");
404
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ const std::string modified_at = model.value("modified_at", "unknown");
405
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ const std::string size = model.contains("size")
406
0/8
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ ? std::to_string(model["size"].get<long long>() / 1024 / 1024) + " Mb"
407
0/12
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ : "size unknown";
408
409
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ if (name != "unknown") {
410
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ models.push_back(name);
411 }
412
413
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ message = std::format("{}\t ({}) \t{}\n", modified_at, size, name);
414
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
415
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_message_noninteractive(message);
416 } else {
417
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_message(message);
418 }
419 ✗ }
420
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ UserInterface::print_message_noninteractive("\n");
421
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ } catch (const json::exception& err) {
422
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ message = std::format("JSON parse error: {}\n", err.what());
423
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
424
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_error_noninteractive(message);
425 } else {
426
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_error(message);
427 }
428
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->end_session(EXIT_FAILURE);
429 ✗ }
430
431 ✗ return models;
432 ✗ }
433