File size: 13,281 Bytes
0309a5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// pencilclaw.cpp – Command & Control loop (fixed version)
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <memory>
#include <cstring>
#include <curl/curl.h>
#include <filesystem>   // for temporary file cleanup

#include "pencil_utils.hpp"
#include "cJSON.h"

// Global debug flag (can be toggled at runtime)
static bool debug_enabled = false;

// ----------------------------------------------------------------------
// Helper: libcurl write callback
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, std::string *output) {
    size_t total = size * nmemb;
    output->append((char*)contents, total);
    return total;
}

// ----------------------------------------------------------------------
// Send prompt to Ollama, return the generated text
std::string ask_ollama(const std::string &prompt) {
    CURL *curl = curl_easy_init();
    if (!curl) {
        return "[Error] Failed to initialize curl.";
    }

    // Model name – change this to match your installed model (e.g., "llama3", "qwen2.5", "mistral")
    const std::string MODEL_NAME = "qwen2.5:0.5b";

    // Build JSON request
    cJSON *root = cJSON_CreateObject();
    if (!root) {
        curl_easy_cleanup(curl);
        return "[Error] Failed to create JSON object (out of memory).";
    }
    cJSON_AddStringToObject(root, "model", MODEL_NAME.c_str());
    cJSON_AddStringToObject(root, "prompt", prompt.c_str());
    cJSON_AddBoolToObject(root, "stream", false);
    char *json_str = cJSON_PrintUnformatted(root);
    cJSON_Delete(root);

    if (!json_str) {
        curl_easy_cleanup(curl);
        return "[Error] Failed to format JSON request.";
    }

    if (debug_enabled) {
        std::cerr << "\n[DEBUG] Request JSON: " << json_str << std::endl;
    }

    std::string response_string;
    struct curl_slist *headers = nullptr;
    headers = curl_slist_append(headers, "Content-Type: application/json");

    curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:11434/api/generate");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_str);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60L); // increased timeout to 60 seconds

    // Perform request
    CURLcode res = curl_easy_perform(curl);
    if (res != CURLE_OK) {
        std::string err = "[Error] curl failed: ";
        err += curl_easy_strerror(res);
        free(json_str);
        curl_slist_free_all(headers);
        curl_easy_cleanup(curl);
        return err;
    }

    // Check HTTP response code
    long http_code = 0;
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
    if (http_code != 200) {
        std::string err = "[Error] HTTP " + std::to_string(http_code) + " response from Ollama";
        if (http_code == 404) {
            err += ".\n       Make sure Ollama is running and the model '" + MODEL_NAME + "' is installed (try 'ollama pull " + MODEL_NAME + "').";
        }
        free(json_str);
        curl_slist_free_all(headers);
        curl_easy_cleanup(curl);
        return err;
    }

    curl_slist_free_all(headers);
    curl_easy_cleanup(curl);
    free(json_str);

    if (debug_enabled) {
        std::cerr << "[DEBUG] Raw response: " << response_string << std::endl;
    }

    // Parse JSON response
    cJSON *json = cJSON_Parse(response_string.c_str());
    if (!json) {
        return "[Error] Failed to parse Ollama JSON (invalid JSON).";
    }
    cJSON *resp = cJSON_GetObjectItem(json, "response");
    std::string result;
    if (resp && resp->valuestring) {
        result = resp->valuestring;
    } else {
        // Check for error field
        cJSON *error = cJSON_GetObjectItem(json, "error");
        if (error && error->valuestring) {
            result = "[Error from Ollama] " + std::string(error->valuestring);
        } else {
            result = "[Error] No 'response' field in Ollama output.";
        }
    }
    cJSON_Delete(json);
    return result;
}

// ----------------------------------------------------------------------
// Extract code blocks marked with ```lang ... ```
std::vector<std::string> extract_code_blocks(const std::string &text) {
    std::vector<std::string> blocks;
    size_t pos = 0;
    while (true) {
        size_t start = text.find("```", pos);
        if (start == std::string::npos) break;
        size_t end = text.find("```", start + 3);
        if (end == std::string::npos) break;
        
        // Determine content start: skip the language specifier line if present
        size_t content_start = text.find('\n', start) + 1;
        if (content_start == std::string::npos || content_start > end)
            content_start = start + 3;
        else {
            // content_start now points after the newline; but if the line after ``` is empty, we may need to skip more?
            // Simpler: just take everything after the first newline until the closing ```
            // Already correct.
        }
        std::string block = text.substr(content_start, end - content_start);
        blocks.push_back(block);
        pos = end + 3;
    }
    return blocks;
}

// ----------------------------------------------------------------------
// Execute a code block: save to temp file, compile, run
bool execute_code(const std::string &code) {
    // Save to a temporary file
    std::string tmp_cpp = pencil::PENCIL_DIR + "temp_code.cpp";
    std::string tmp_exe = pencil::PENCIL_DIR + "temp_code";
    std::ofstream out(tmp_cpp);
    if (!out) {
        std::cerr << "Failed to create temporary file: " << tmp_cpp << std::endl;
        return false;
    }
    out << code;
    out.close();
    if (out.fail()) {
        std::cerr << "Failed to write code to temporary file." << std::endl;
        return false;
    }

    // Compile with g++
    std::string compile_cmd = "g++ " + tmp_cpp + " -o " + tmp_exe + " 2>&1";
    FILE *pipe = popen(compile_cmd.c_str(), "r");
    if (!pipe) {
        std::cerr << "Failed to run compiler." << std::endl;
        return false;
    }
    char buffer[128];
    std::string compile_out;
    while (fgets(buffer, sizeof buffer, pipe) != nullptr) {
        compile_out += buffer;
    }
    int status = pclose(pipe);
    if (status != 0) {
        std::cerr << "Compilation failed:\n" << compile_out << std::endl;
        // Clean up source file even on failure
        std::filesystem::remove(tmp_cpp);
        return false;
    }

    // Run the compiled program
    std::string run_cmd = tmp_exe;
    int ret = system(run_cmd.c_str());
    std::cout << "\n[Program exited with code " << ret << "]" << std::endl;

    // Clean up temporary files
    std::filesystem::remove(tmp_cpp);
    std::filesystem::remove(tmp_exe);
    return true;
}

// ----------------------------------------------------------------------
// Sanitize user input for use as a filename: remove path separators and ".."
std::string sanitize_filename(const std::string &input) {
    std::string safe;
    for (char c : input) {
        // Allow alphanumerics, dot, dash, underscore; replace others with underscore
        if (isalnum(c) || c == '.' || c == '-' || c == '_')
            safe += c;
        else
            safe += '_';
    }
    // Prevent empty or dot-only names
    if (safe.empty() || safe == "." || safe == "..")
        safe = "unnamed";
    return safe;
}

// ----------------------------------------------------------------------
int main() {
    // Prepare workspace
    if (!pencil::init_workspace()) {
        std::cerr << "Fatal error: cannot create workspace directory." << std::endl;
        return 1;
    }

    std::cout << "PENCILCLAW v1.1 – ADA‑style writing agent (local Ollama)\n";
    std::cout << "Type /HELP for commands.\n";

    std::string last_response;   // store last LLM output for /EXECUTE

    while (true) {
        std::cout << "\n> ";
        std::string line;
        std::getline(std::cin, line);
        if (line.empty()) continue;

        if (line[0] == '/') {
            // ADA command
            std::string cmd;
            std::string arg;
            size_t sp = line.find(' ');
            if (sp == std::string::npos) {
                cmd = line;
            } else {
                cmd = line.substr(0, sp);
                arg = line.substr(sp + 1);
            }

            if (cmd == "/EXIT") {
                break;
            }
            else if (cmd == "/HELP") {
                std::cout << "Available commands:\n";
                std::cout << "  /HELP                 – this help\n";
                std::cout << "  /STORY   <title>      – write a story\n";
                std::cout << "  /POEM    <subject>    – compose a poem\n";
                std::cout << "  /BOOK    <chapter>    – write a chapter (appends to book.txt)\n";
                std::cout << "  /EXECUTE              – compile & run code from last response\n";
                std::cout << "  /DEBUG                – toggle debug output\n";
                std::cout << "  /EXIT                 – quit\n";
            }
            else if (cmd == "/DEBUG") {
                debug_enabled = !debug_enabled;
                std::cout << "Debug mode " << (debug_enabled ? "enabled" : "disabled") << ".\n";
            }
            else if (cmd == "/STORY" || cmd == "/POEM") {
                if (arg.empty()) {
                    std::cout << "Please provide a " << (cmd == "/STORY" ? "title" : "subject") << ".\n";
                    continue;
                }
                // Build prompt with template
                std::string prompt = pencil::get_template(cmd) + "\n\n" + arg;
                std::cout << "Asking Ollama...\n";
                last_response = ask_ollama(prompt);
                std::cout << last_response << "\n";

                // Save to a file using sanitized filename
                std::string safe_arg = sanitize_filename(arg);
                std::string filename = pencil::PENCIL_DIR + safe_arg + ".txt";
                if (!pencil::save_text(filename, last_response))
                    std::cerr << "Warning: could not save file " << filename << std::endl;
                pencil::append_to_session("User: " + line);
                pencil::append_to_session("Assistant: " + last_response);
            }
            else if (cmd == "/BOOK") {
                if (arg.empty()) {
                    std::cout << "Please provide a chapter name.\n";
                    continue;
                }
                // Build context: previous book content
                std::string book_content = pencil::read_file(pencil::PENCIL_DIR + pencil::BOOK_FILE);
                std::string prompt = pencil::get_template("/BOOK") + "\n\nExisting book content:\n" +
                                     book_content + "\n\nWrite the next chapter: " + arg;
                std::cout << "Asking Ollama...\n";
                last_response = ask_ollama(prompt);
                std::cout << last_response << "\n";

                // Append chapter to book.txt
                if (!pencil::append_to_book("\n--- " + arg + " ---\n" + last_response))
                    std::cerr << "Warning: could not append to book file." << std::endl;
                pencil::append_to_session("User: " + line);
                pencil::append_to_session("Assistant: " + last_response);
            }
            else if (cmd == "/EXECUTE") {
                if (last_response.empty()) {
                    std::cout << "No previous response to execute from.\n";
                    continue;
                }
                auto blocks = extract_code_blocks(last_response);
                if (blocks.empty()) {
                    std::cout << "No code blocks found in last response.\n";
                    continue;
                }
                // Optional security confirmation
                std::cout << "WARNING: You are about to execute code generated by an AI.\n";
                std::cout << "Only proceed if you trust the source. Continue? (y/n): ";
                std::string confirm;
                std::getline(std::cin, confirm);
                if (confirm != "y" && confirm != "Y") {
                    std::cout << "Execution cancelled.\n";
                    continue;
                }
                // Execute the first block
                std::cout << "Executing code block...\n";
                if (execute_code(blocks[0])) {
                    std::cout << "Execution finished.\n";
                } else {
                    std::cout << "Execution failed.\n";
                }
            }
            else {
                std::cout << "Unknown command. Type /HELP for list.\n";
            }
        } else {
            // Non‑command: treat as free prompt
            std::cout << "Sending to Ollama...\n";
            last_response = ask_ollama(line);
            std::cout << last_response << "\n";
            pencil::append_to_session("User: " + line);
            pencil::append_to_session("Assistant: " + last_response);
        }
    }

    return 0;
}