| 710 | } |
| 711 | |
| 712 | void server_tools::setup(const std::vector<std::string> & enabled_tools) { |
| 713 | if (!enabled_tools.empty()) { |
| 714 | std::unordered_set<std::string> enabled_set(enabled_tools.begin(), enabled_tools.end()); |
| 715 | auto all_tools = build_tools(); |
| 716 | |
| 717 | tools.clear(); |
| 718 | for (auto & t : all_tools) { |
| 719 | if (enabled_set.count(t->name) > 0 || enabled_set.count("all") > 0) { |
| 720 | tools.push_back(std::move(t)); |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | handle_get = [this](const server_http_req &) -> server_http_res_ptr { |
| 726 | auto res = std::make_unique<server_http_res>(); |
| 727 | try { |
| 728 | json result = json::array(); |
| 729 | for (const auto & t : tools) { |
| 730 | result.push_back(t->to_json()); |
| 731 | } |
| 732 | res->data = safe_json_to_str(result); |
| 733 | } catch (const std::exception & e) { |
| 734 | SRV_ERR("got exception: %s\n", e.what()); |
| 735 | res->status = 500; |
| 736 | res->data = safe_json_to_str(format_error_response(e.what(), ERROR_TYPE_SERVER)); |
| 737 | } |
| 738 | return res; |
| 739 | }; |
| 740 | |
| 741 | handle_post = [this](const server_http_req & req) -> server_http_res_ptr { |
| 742 | auto res = std::make_unique<server_http_res>(); |
| 743 | try { |
| 744 | json body = json::parse(req.body); |
| 745 | std::string tool_name = body.at("tool").get<std::string>(); |
| 746 | json params = body.value("params", json::object()); |
| 747 | json result = invoke(tool_name, params); |
| 748 | res->data = safe_json_to_str(result); |
| 749 | } catch (const json::exception & e) { |
| 750 | res->status = 400; |
| 751 | res->data = safe_json_to_str(format_error_response(e.what(), ERROR_TYPE_INVALID_REQUEST)); |
| 752 | } catch (const std::exception & e) { |
| 753 | SRV_ERR("got exception: %s\n", e.what()); |
| 754 | res->status = 500; |
| 755 | res->data = safe_json_to_str(format_error_response(e.what(), ERROR_TYPE_SERVER)); |
| 756 | } |
| 757 | return res; |
| 758 | }; |
| 759 | } |
| 760 | |
| 761 | json server_tools::invoke(const std::string & name, const json & params) { |
| 762 | for (auto & t : tools) { |
no test coverage detected