| | |
| | #include <iostream> |
| | #include <string> |
| | #include <vector> |
| | #include <map> |
| | #include <fstream> |
| | #include <sstream> |
| | #include <cstdlib> |
| | #include <memory> |
| | #include <cstring> |
| | #include <curl/curl.h> |
| | #include <filesystem> |
| |
|
| | #include "pencil_utils.hpp" |
| | #include "cJSON.h" |
| |
|
| | |
| | static bool debug_enabled = false; |
| |
|
| | |
| | |
| | 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; |
| | } |
| |
|
| | |
| | |
| | std::string ask_ollama(const std::string &prompt) { |
| | CURL *curl = curl_easy_init(); |
| | if (!curl) { |
| | return "[Error] Failed to initialize curl."; |
| | } |
| |
|
| | |
| | const std::string MODEL_NAME = "qwen2.5:0.5b"; |
| |
|
| | |
| | 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); |
| |
|
| | |
| | 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; |
| | } |
| |
|
| | |
| | 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; |
| | } |
| |
|
| | |
| | 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 { |
| | |
| | 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; |
| | } |
| |
|
| | |
| | |
| | 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; |
| | |
| | |
| | size_t content_start = text.find('\n', start) + 1; |
| | if (content_start == std::string::npos || content_start > end) |
| | content_start = start + 3; |
| | else { |
| | |
| | |
| | |
| | } |
| | std::string block = text.substr(content_start, end - content_start); |
| | blocks.push_back(block); |
| | pos = end + 3; |
| | } |
| | return blocks; |
| | } |
| |
|
| | |
| | |
| | bool execute_code(const std::string &code) { |
| | |
| | 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; |
| | } |
| |
|
| | |
| | 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; |
| | |
| | std::filesystem::remove(tmp_cpp); |
| | return false; |
| | } |
| |
|
| | |
| | std::string run_cmd = tmp_exe; |
| | int ret = system(run_cmd.c_str()); |
| | std::cout << "\n[Program exited with code " << ret << "]" << std::endl; |
| |
|
| | |
| | std::filesystem::remove(tmp_cpp); |
| | std::filesystem::remove(tmp_exe); |
| | return true; |
| | } |
| |
|
| | |
| | |
| | std::string sanitize_filename(const std::string &input) { |
| | std::string safe; |
| | for (char c : input) { |
| | |
| | if (isalnum(c) || c == '.' || c == '-' || c == '_') |
| | safe += c; |
| | else |
| | safe += '_'; |
| | } |
| | |
| | if (safe.empty() || safe == "." || safe == "..") |
| | safe = "unnamed"; |
| | return safe; |
| | } |
| |
|
| | |
| | int main() { |
| | |
| | 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; |
| |
|
| | while (true) { |
| | std::cout << "\n> "; |
| | std::string line; |
| | std::getline(std::cin, line); |
| | if (line.empty()) continue; |
| |
|
| | if (line[0] == '/') { |
| | |
| | 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; |
| | } |
| | |
| | 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"; |
| |
|
| | |
| | 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; |
| | } |
| | |
| | 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"; |
| |
|
| | |
| | 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; |
| | } |
| | |
| | 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; |
| | } |
| | |
| | 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 { |
| | |
| | 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; |
| | } |
| |
|