humanus.cpp/main.cpp

105 lines
3.3 KiB
C++
Raw Normal View History

2025-03-19 18:44:54 +08:00
#include "agent/humanus.h"
2025-03-16 17:17:01 +08:00
#include "logger.h"
2025-03-17 01:58:37 +08:00
#include "prompt.h"
2025-03-17 14:07:41 +08:00
#include "flow/flow_factory.h"
2025-03-16 17:17:01 +08:00
2025-03-16 22:56:03 +08:00
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#include <signal.h>
#include <unistd.h>
#elif defined (_WIN32)
#define WIN32_LEAN_AND_MEAN
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <signal.h>
#endif
2025-03-16 17:17:01 +08:00
using namespace humanus;
2025-03-16 22:56:03 +08:00
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) || defined (_WIN32)
static void sigint_handler(int signo) {
if (signo == SIGINT) {
2025-03-19 18:44:54 +08:00
logger->info("Interrupted by user\n");
2025-03-16 22:56:03 +08:00
exit(0);
}
}
#endif
2025-03-16 17:17:01 +08:00
int main() {
2025-03-16 22:56:03 +08:00
// ctrl+C handling
{
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
struct sigaction sigint_action;
sigint_action.sa_handler = sigint_handler;
sigemptyset (&sigint_action.sa_mask);
sigint_action.sa_flags = 0;
sigaction(SIGINT, &sigint_action, NULL);
#elif defined (_WIN32)
auto console_ctrl_handler = +[](DWORD ctrl_type) -> BOOL {
return (ctrl_type == CTRL_C_EVENT) ? (sigint_handler(SIGINT), true) : false;
};
SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true);
#endif
}
2025-03-17 15:51:14 +08:00
2025-03-19 18:44:54 +08:00
// Humanus agent = Humanus();
// while (true) {
// if (agent.current_step == agent.max_steps) {
// std::cout << "Automatically paused after " << agent.max_steps << " steps." << std::endl;
// std::cout << "Enter your prompt (enter en empty line to resume or 'exit' to quit): ";
// agent.reset(false);
// } else {
// std::cout << "Enter your prompt (or 'exit' to quit): ";
// }
// std::string prompt;
// std::getline(std::cin, prompt);
// if (prompt == "exit") {
// logger->info("Goodbye!");
// break;
// }
// logger->info("Processing your request...");
// agent.run(prompt);
// }
std::shared_ptr<BaseAgent> agent_ptr = std::make_shared<Humanus>();
std::map<std::string, std::shared_ptr<BaseAgent>> agents;
agents["default"] = agent_ptr;
auto flow = FlowFactory::create_flow(
FlowType::PLANNING,
nullptr, // llm
nullptr, // planning_tool
std::vector<std::string>{}, // executor_keys
"", // active_plan_id
agents, // agents
std::vector<std::shared_ptr<BaseTool>>{}, // tools
"default" // primary_agent_key
);
2025-03-16 17:17:01 +08:00
while (true) {
2025-03-19 18:44:54 +08:00
if (agent_ptr->current_step == agent_ptr->max_steps) {
std::cout << "Program automatically paused after " << agent_ptr->current_step << " steps." << std::endl;
2025-03-17 15:51:14 +08:00
std::cout << "Enter your prompt (enter empty line to resume or 'exit' to quit): ";
2025-03-19 18:44:54 +08:00
agent_ptr->reset(false);
2025-03-17 14:07:41 +08:00
} else {
std::cout << "Enter your prompt (or 'exit' to quit): ";
}
2025-03-19 18:44:54 +08:00
if (agent_ptr->state != AgentState::IDLE) {
break;
}
2025-03-16 17:17:01 +08:00
std::string prompt;
std::getline(std::cin, prompt);
if (prompt == "exit") {
logger->info("Goodbye!");
break;
}
2025-03-17 15:51:14 +08:00
2025-03-19 18:44:54 +08:00
std::cout << "Processing your request..." << std::endl;
auto result = flow->execute(prompt);
std::cout << result << std::endl;
}
2025-03-16 17:17:01 +08:00
}