/* * AGI Conversational Interface * Natural language understanding and command execution * Integrates with distributed agent swarm */ #include #include #include #include #include #include #include #ifdef HAVE_CURL #include #endif #define MAX_INPUT 4096 #define MAX_MEMORY 64 #define MAX_KNOWLEDGE 250000 #define DICT_SIZE 150000 #define MAX_RESPONSE 8192 // FNV-1a hash static uint64_t hash_phrase(const char* str) { uint64_t hash = 0xcbf29ce484222325ULL; while (*str) { hash ^= (unsigned char)tolower(*str++); hash *= 0x100000001b3ULL; } return hash; } // Memory entry typedef struct { char input[256]; char response[512]; uint64_t timestamp; } MemoryEntry; // Knowledge entry typedef struct { uint64_t hash; char fact[256]; uint32_t confidence; } KnowledgeEntry; // AGI state typedef struct { MemoryEntry memory[MAX_MEMORY]; KnowledgeEntry knowledge[MAX_KNOWLEDGE]; size_t memory_idx; size_t knowledge_count; uint64_t conversation_count; char gossip_url[128]; } AGIChat; static AGIChat agi_state = {0}; // Common English words and patterns for understanding static const char* common_words[] = { "what", "how", "why", "when", "where", "who", "is", "are", "was", "were", "be", "been", "do", "does", "did", "done", "have", "has", "had", "will", "would", "should", "could", "can", "may", "might", "must", "the", "a", "an", "this", "that", "these", "those", "I", "you", "he", "she", "it", "we", "they", "my", "your", "his", "her", "its", "our", "their", "me", "him", "them", "us", "and", "or", "but", "if", "then", "else", "not", "no", "yes", "maybe", "perhaps", "make", "get", "give", "take", "go", "come", "see", "look", "find", "use", "try", "work", "call", "tell", "ask", "need", "want", "like", "know", "think", "feel", "become", "seem", "run", "move", "live", "die", "stop", "start", "turn", "show", "play", "write", "read", "learn", "build", "create", "destroy", "fix", "break", "agent", "swarm", "migration", "cluster", "gossip", "train", "teach", "understand", "respond", "talk", "status", "stats", "info", "help", "exit", "quit" }; #ifdef HAVE_CURL // curl write callback static size_t curl_write_cb(void* contents, size_t size, size_t nmemb, void* userp) { size_t realsize = size * nmemb; char* response = (char*)userp; size_t len = strlen(response); if (len + realsize < MAX_RESPONSE - 1) { memcpy(response + len, contents, realsize); response[len + realsize] = '\0'; } return realsize; } #endif // Query gossip service for swarm status static int query_swarm_status(char* out_buf, size_t buf_size) { #ifdef HAVE_CURL CURL* curl = curl_easy_init(); if (!curl) return -1; char response[MAX_RESPONSE] = {0}; curl_easy_setopt(curl, CURLOPT_URL, agi_state.gossip_url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_cb); curl_easy_setopt(curl, CURLOPT_WRITEDATA, response); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 2L); CURLcode res = curl_easy_perform(curl); curl_easy_cleanup(curl); if (res == CURLE_OK) { snprintf(out_buf, buf_size, "%s", response); return 0; } return -1; #else // Fallback: use curl command-line tool FILE* fp = popen("curl -s -m 2 http://localhost:8008/stats 2>/dev/null", "r"); if (!fp) return -1; char line[256]; out_buf[0] = '\0'; while (fgets(line, sizeof(line), fp) != NULL) { size_t current_len = strlen(out_buf); size_t line_len = strlen(line); if (current_len + line_len < buf_size - 1) { strcat(out_buf, line); } } int ret = pclose(fp); return (ret == 0 && strlen(out_buf) > 0) ? 0 : -1; #endif } // Add to memory static void remember(const char* input, const char* response) { size_t idx = agi_state.memory_idx % MAX_MEMORY; snprintf(agi_state.memory[idx].input, sizeof(agi_state.memory[idx].input), "%s", input); snprintf(agi_state.memory[idx].response, sizeof(agi_state.memory[idx].response), "%s", response); agi_state.memory[idx].timestamp = (uint64_t)time(NULL); agi_state.memory_idx++; } // Search memory for similar input static const char* recall(const char* input) { uint64_t input_hash = hash_phrase(input); // Search recent memory for similar inputs for (size_t i = 0; i < MAX_MEMORY && i < agi_state.memory_idx; i++) { size_t idx = (agi_state.memory_idx - 1 - i) % MAX_MEMORY; uint64_t mem_hash = hash_phrase(agi_state.memory[idx].input); // Simple similarity: matching hash prefix (32 bits) if ((input_hash >> 32) == (mem_hash >> 32)) { return agi_state.memory[idx].response; } } return NULL; } // Add knowledge static void learn(const char* fact, uint32_t confidence) { if (agi_state.knowledge_count >= MAX_KNOWLEDGE) return; uint64_t hash = hash_phrase(fact); // Check if already known for (size_t i = 0; i < agi_state.knowledge_count; i++) { if (agi_state.knowledge[i].hash == hash) { // Update confidence agi_state.knowledge[i].confidence = (agi_state.knowledge[i].confidence + confidence) / 2; return; } } // Add new knowledge agi_state.knowledge[agi_state.knowledge_count].hash = hash; snprintf(agi_state.knowledge[agi_state.knowledge_count].fact, sizeof(agi_state.knowledge[agi_state.knowledge_count].fact), "%s", fact); agi_state.knowledge[agi_state.knowledge_count].confidence = confidence; agi_state.knowledge_count++; } // Check if input contains word static int contains_word(const char* input, const char* word) { char lower_input[MAX_INPUT]; snprintf(lower_input, sizeof(lower_input), "%s", input); for (char* p = lower_input; *p; p++) *p = tolower(*p); return strstr(lower_input, word) != NULL; } // Pattern matching for command recognition static int is_question(const char* input) { return contains_word(input, "what") || contains_word(input, "how") || contains_word(input, "why") || contains_word(input, "when") || contains_word(input, "where") || contains_word(input, "who") || strchr(input, '?') != NULL; } static int is_status_query(const char* input) { return contains_word(input, "status") || contains_word(input, "stats") || contains_word(input, "how many") || contains_word(input, "agents"); } static int is_help_request(const char* input) { return contains_word(input, "help") || contains_word(input, "what can you"); } static int is_greeting(const char* input) { return contains_word(input, "hello") || contains_word(input, "hi") || contains_word(input, "hey") || contains_word(input, "greetings"); } static int is_farewell(const char* input) { return contains_word(input, "bye") || contains_word(input, "goodbye") || contains_word(input, "exit") || contains_word(input, "quit"); } static int is_training_query(const char* input) { return contains_word(input, "train") || contains_word(input, "learn") || contains_word(input, "teach"); } static int is_migration_query(const char* input) { return contains_word(input, "migrate") || contains_word(input, "migration") || contains_word(input, "cluster") || contains_word(input, "distributed"); } // Generate response based on understanding static void generate_response(const char* input, char* response, size_t response_size) { // Check memory first const char* recalled = recall(input); if (recalled) { snprintf(response, response_size, "%s", recalled); return; } // Pattern-based understanding if (is_greeting(input)) { snprintf(response, response_size, "Hello! I'm your AGI assistant. I can understand natural language and control the distributed agent swarm. How can I help you?"); } else if (is_farewell(input)) { snprintf(response, response_size, "Goodbye! The swarm continues running in the background. Feel free to return anytime."); } else if (is_help_request(input)) { snprintf(response, response_size, "I can help you with:\n" "- Check swarm status: 'what is the swarm status?' or 'how many agents?'\n" "- Training: 'train the agents' or 'start learning'\n" "- Migration: 'show migration status' or 'cluster info'\n" "- General conversation: ask me anything about the system\n" "- Memory: I remember our conversations and learn from them"); } else if (is_status_query(input)) { char status[MAX_RESPONSE] = {0}; if (query_swarm_status(status, sizeof(status)) == 0) { snprintf(response, response_size, "Swarm Status:\n%s\n\nConversations: %lu\nKnowledge entries: %zu", status, agi_state.conversation_count, agi_state.knowledge_count); } else { snprintf(response, response_size, "Unable to connect to gossip service at %s. Is it running?", agi_state.gossip_url); } } else if (is_training_query(input)) { snprintf(response, response_size, "Training capabilities:\n" "- The system learns from our conversations (Memory: %zu/%d)\n" "- Knowledge base: %zu/%d entries\n" "- Distributed learning across %s cluster\n" "- API integration available with 900 credits\n" "What would you like to train?", agi_state.memory_idx < MAX_MEMORY ? agi_state.memory_idx : MAX_MEMORY, MAX_MEMORY, agi_state.knowledge_count, MAX_KNOWLEDGE, agi_state.gossip_url); } else if (is_migration_query(input)) { char status[MAX_RESPONSE] = {0}; if (query_swarm_status(status, sizeof(status)) == 0) { snprintf(response, response_size, "Migration System Status:\n%s\n\n" "The distributed system uses:\n" "- Binary protocol for efficient agent transfer\n" "- MAX consensus for goal convergence\n" "- Load-based predictive migration\n" "- Fibonacci growth control (10%% every 89 ticks)", status); } else { snprintf(response, response_size, "Migration system is integrated but gossip service is not responding."); } } else if (is_question(input)) { // Generic question response snprintf(response, response_size, "That's an interesting question. I'm still learning about '%s'. " "Based on my current knowledge (%zu entries), I can tell you that " "this system is a distributed AGI with conversational capabilities. " "Ask me more specific questions about the swarm, training, or migration.", input, agi_state.knowledge_count); } else { // Default response with learning snprintf(response, response_size, "I understand you said: '%s'. " "I'm learning from this conversation. " "Try asking about 'status', 'training', 'migration', or 'help' for specific actions.", input); // Learn from this interaction char fact[256]; snprintf(fact, sizeof(fact), "user said: %s", input); learn(fact, 50); } } // Main conversational loop int main(int argc, char** argv) { // Initialize snprintf(agi_state.gossip_url, sizeof(agi_state.gossip_url), "http://localhost:8008/stats"); printf("🧠 AGI Conversational Interface\n"); printf("Natural Language Understanding + Distributed Swarm Control\n"); printf("─────────────────────────────────────────────────────────\n"); printf("Type 'exit' or 'quit' to end conversation\n\n"); // Check if gossip service is running char test_status[256] = {0}; if (query_swarm_status(test_status, sizeof(test_status)) == 0) { printf("āœ… Connected to gossip service\n"); } else { printf("āš ļø Gossip service not responding (will continue anyway)\n"); } printf("\n"); char input[MAX_INPUT]; char response[MAX_RESPONSE]; while (1) { printf("You: "); fflush(stdout); if (!fgets(input, sizeof(input), stdin)) break; // Remove newline size_t len = strlen(input); if (len > 0 && input[len-1] == '\n') input[len-1] = '\0'; // Skip empty input if (strlen(input) == 0) continue; // Check for exit if (is_farewell(input)) { generate_response(input, response, sizeof(response)); printf("\nAGI: %s\n", response); break; } // Generate and display response generate_response(input, response, sizeof(response)); printf("\nAGI: %s\n\n", response); // Remember this exchange remember(input, response); agi_state.conversation_count++; // Periodic learning stats if (agi_state.conversation_count % 10 == 0) { printf("šŸ“Š Learning stats: %lu conversations, %zu knowledge entries\n\n", agi_state.conversation_count, agi_state.knowledge_count); } } printf("\nāœ… Conversation ended. Knowledge and memory preserved.\n"); return 0; }