Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nclient.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ echo ""
fi
# Garder la connexion ouverte pour interaction
cat
} | nc localhost 9999
} | nc -C localhost 9999

10 changes: 7 additions & 3 deletions srcs/commands/Bot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void remove_invalid_prompt_char(char& c)
c = ' ';
}

static void send_ollama_request(const std::string& prompt, std::string& response)
static bool send_ollama_request(const std::string& prompt, std::string& response)
{
std::string command = "curl -X POST -H \"Content-Type: application/json\" -v localhost:11434/api/generate -d '";
command += "{\"model\": \"gemma3:1b\",\"prompt\":\"";
Expand All @@ -100,13 +100,16 @@ static void send_ollama_request(const std::string& prompt, std::string& response
LOG_d_CMD(command);

int code = ::system(command.c_str());
if (code == -1) {
LOG_w_CMD(code);
if (code != 0) {
LOG_E_SERVER("error sending API LLama request", command);
return false;
}

::usleep(BOT_PROCESS_TIME_MS);
std::ifstream file("llama_response.txt");
response.assign((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return true;
}

bool Bot::_connect_to_server(Server& s, TcpSocket& so)
Expand Down Expand Up @@ -194,7 +197,8 @@ void Bot::execute(Server& s, Client& c)
LOG_DV_CMD(prompt);

std::string response = "\"\"";
send_ollama_request(prompt, response);
if (!send_ollama_request(prompt, response))
response = "\"Bot is under maintenance\"";

if (!_connect_to_server(s, _socket))
return;
Expand Down
7 changes: 3 additions & 4 deletions srcs/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ int main(int ac, char** av)
{
int port = DEFAULT_PORT;

LOG_ERR.set_min_level(ERROR); // Seulement les erreurs dans ce log
LOG_ERR.set_min_level(ERROR);
if (!Utils::check_args(ac, av, &port))
return 1;
Server newServer(port, av[2]);
setup_signal_handlers();
try {
Server newServer(port, av[2]);
setup_signal_handlers();
newServer.start();
} catch (std::exception& e) {
std::cout << e.what() << "\n";
LOG_ERR.error("Server error: " + std::string(e.what()));
}
return 0;
Expand Down
1 change: 0 additions & 1 deletion srcs/server/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ bool Config::_parse_config_file(const std::string& fileName)
else if (currentSection == "irc.motd")
_parse_motd(line);
}

file.close();
return true;
}
Expand Down
16 changes: 6 additions & 10 deletions srcs/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <arpa/inet.h> // hton*, ntoh*, inet_addr
#include <csignal>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <fcntl.h>
Expand All @@ -35,13 +36,8 @@
Server::Server(const unsigned short port, const std::string& password) :
_psswd(password), _name(ircConfig.get_name()), _port(port)
{
try {
_serverSocket.tcp_bind(port);
_serverSocket.tcp_listen();
} catch (std::exception& e) {
std::cout << e.what();
exit(1);
}
_serverSocket.tcp_bind(port);
_serverSocket.tcp_listen();
LOG_SERVER.info("Server " + _name + " start at port :" + Utils::to_string(port));
std::cout << "\n";

Expand Down Expand Up @@ -317,9 +313,9 @@ bool Server::_handle_commands(int pfdIndex)
while ((pos = client->get_read_buffer().find("\r\n")) != std::string::npos) {
std::string line = client->get_read_buffer().substr(0, pos);
client->get_read_buffer().erase(0, pos + 2);
if (line.empty()) {
continue;
}
if (line.empty()) {
continue;
}
ICommand* cmd = _parse_command(*client, line);
if (cmd) {
cmd->execute(*this, *client);
Expand Down