Skip to content

Commit ff44762

Browse files
committed
Split toolcall params into separate files
1 parent 80e6790 commit ff44762

File tree

5 files changed

+114
-90
lines changed

5 files changed

+114
-90
lines changed

common/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ add_library(${TARGET} STATIC
7575
sampling.h
7676
speculative.cpp
7777
speculative.h
78+
${CMAKE_CURRENT_SOURCE_DIR}/toolcall/params.cpp
79+
${CMAKE_CURRENT_SOURCE_DIR}/toolcall/params.hpp
7880
${CMAKE_CURRENT_SOURCE_DIR}/toolcall/handler.cpp
7981
${CMAKE_CURRENT_SOURCE_DIR}/toolcall/handler.hpp
8082
${CMAKE_CURRENT_SOURCE_DIR}/toolcall/mcp_messages.cpp

common/toolcall/handler.cpp

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11

2+
#include "../json.hpp"
23
#include "handler.hpp"
4+
#include "params.hpp"
35

46
#ifdef LLAMA_USE_CURL
57
# include "mcp_sse_transport.hpp"
@@ -9,16 +11,6 @@
911

1012
using json = toolcall::json;
1113

12-
toolcall::params::params(std::string tools, std::string choice) {
13-
this->tools(tools);
14-
this->choice(choice);
15-
}
16-
17-
static bool starts_with(const std::string & str, const std::string & prefix) {
18-
return str.size() >= prefix.size()
19-
&& str.compare(0, prefix.size(), prefix) == 0;
20-
}
21-
2214
std::shared_ptr<toolcall::handler> toolcall::create_handler(const toolcall::params & params) {
2315
std::shared_ptr<toolcall::handler> result;
2416

@@ -42,62 +34,6 @@ std::shared_ptr<toolcall::handler> toolcall::create_handler(const toolcall::para
4234
return result;
4335
}
4436

45-
void toolcall::params::tools(std::string tools) {
46-
try {
47-
48-
if (tools.empty()) {
49-
tools_ = std::move(tools);
50-
51-
} else if (starts_with(tools, "mcp+http")) {
52-
#ifdef LLAMA_USE_CURL
53-
tools_ = std::move(tools);
54-
#else
55-
throw std::invalid_argument(
56-
"Model Context Protocol (MCP) only works when llama.cpp is compiled with libcurl");
57-
#endif
58-
} else {
59-
tools_ = std::make_shared<json>(json::parse(tools));
60-
auto tools_ptr = std::get<std::shared_ptr<json>>(tools_);
61-
if (! tools_ptr->is_array()) {
62-
throw std::invalid_argument(
63-
"tools must be a URL of the form \"mcp+http(s)://hostname[:port]/\""
64-
", or a valid JSON array containing tool definitions");
65-
}
66-
}
67-
68-
} catch (const json::exception & err) {
69-
throw std::invalid_argument(err.what());
70-
}
71-
}
72-
73-
void toolcall::params::choice(std::string choice) {
74-
try {
75-
if (choice == "auto" || choice == "required" || choice == "none") {
76-
tool_choice_ = std::move(choice);
77-
78-
} else {
79-
auto choice_ptr = std::make_shared<json>(json::parse(choice));
80-
tool_choice_ = choice_ptr;
81-
if (! choice_ptr->is_object()) {
82-
throw std::invalid_argument(
83-
"tool choice must be a valid JSON object, \"auto\", \"required\", or \"none\"");
84-
}
85-
}
86-
87-
} catch (const json::exception & err) {
88-
throw std::invalid_argument(err.what());
89-
}
90-
}
91-
92-
toolcall::params::operator bool() const {
93-
if (std::holds_alternative<std::string>(tools_)) {
94-
return ! std::get<std::string>(tools_).empty();
95-
96-
} else {
97-
return std::get<toolcall::json_ptr>(tools_) != nullptr;
98-
}
99-
}
100-
10137
json toolcall::handler::tool_list() {
10238
return impl_->tool_list();
10339
}

common/toolcall/handler.hpp

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#pragma once
22

3+
#include "../json.hpp"
4+
#include "params.hpp" // TODO: make foreward decl.
35
#include <string>
46
#include <variant>
57
#include <memory>
68

7-
#include "../json.hpp"
8-
99
namespace toolcall
1010
{
1111
using json = nlohmann::ordered_json;
@@ -36,28 +36,6 @@ namespace toolcall
3636
action last_action_;
3737
};
3838

39-
class params {
40-
public:
41-
params(std::string tools = "", std::string choice = "auto");
42-
43-
params(const params & other) = default;
44-
params(params && other) noexcept = default;
45-
params & operator=(const params & other) = default;
46-
params & operator=(params && other) noexcept = default;
47-
48-
operator bool() const;
49-
50-
void tools(std::string tools);
51-
const tools_t tools() const { return tools_; }
52-
53-
void choice(std::string choice);
54-
const tool_choice_t & choice() const { return tool_choice_; }
55-
56-
private:
57-
tools_t tools_;
58-
tool_choice_t tool_choice_;
59-
};
60-
6139
std::shared_ptr<toolcall::handler> create_handler(const toolcall::params & params);
6240

6341
class handler_impl {

common/toolcall/params.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
#include "../json.hpp" // Must come before params due to forward decl.
3+
#include "params.hpp"
4+
#include <stdexcept>
5+
6+
using json = nlohmann::ordered_json;
7+
8+
static bool starts_with(const std::string & str, const std::string & prefix) {
9+
return str.size() >= prefix.size()
10+
&& str.compare(0, prefix.size(), prefix) == 0;
11+
}
12+
13+
toolcall::params::params(std::string tools, std::string choice) {
14+
this->tools(tools);
15+
this->choice(choice);
16+
}
17+
18+
void toolcall::params::tools(std::string tools) {
19+
try {
20+
if (tools.empty()) {
21+
tools_ = std::move(tools);
22+
23+
} else if (starts_with(tools, "mcp+http")) {
24+
#ifdef LLAMA_USE_CURL
25+
tools_ = std::move(tools);
26+
#else
27+
throw std::invalid_argument(
28+
"Model Context Protocol (MCP) only works when llama.cpp is compiled with libcurl");
29+
#endif
30+
} else {
31+
tools_ = std::make_shared<json>(json::parse(tools));
32+
auto tools_ptr = std::get<std::shared_ptr<json>>(tools_);
33+
if (! tools_ptr->is_array()) {
34+
throw std::invalid_argument(
35+
"tools must be a URL of the form \"mcp+http(s)://hostname[:port]/\""
36+
", or a valid JSON array containing tool definitions");
37+
}
38+
}
39+
40+
} catch (const json::exception & err) {
41+
throw std::invalid_argument(err.what());
42+
}
43+
}
44+
45+
void toolcall::params::choice(std::string choice) {
46+
try {
47+
if (choice == "auto" || choice == "required" || choice == "none") {
48+
tool_choice_ = std::move(choice);
49+
50+
} else {
51+
auto choice_ptr = std::make_shared<json>(json::parse(choice));
52+
tool_choice_ = choice_ptr;
53+
if (! choice_ptr->is_object()) {
54+
throw std::invalid_argument(
55+
"tool choice must be a valid JSON object, \"auto\", \"required\", or \"none\"");
56+
}
57+
}
58+
59+
} catch (const json::exception & err) {
60+
throw std::invalid_argument(err.what());
61+
}
62+
}
63+
64+
toolcall::params::operator bool() const {
65+
if (std::holds_alternative<std::string>(tools_)) {
66+
return ! std::get<std::string>(tools_).empty();
67+
68+
} else {
69+
return std::get<json_ptr>(tools_) != nullptr;
70+
}
71+
}

common/toolcall/params.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <variant>
5+
#include <memory>
6+
7+
#include "../json.hpp" // TODO: switch to foreward decl.
8+
// namespace nlohmann { class ordered_json; }
9+
10+
namespace toolcall
11+
{
12+
class params {
13+
public:
14+
using json_ptr = std::shared_ptr<nlohmann::ordered_json>;
15+
using tools_t = std::variant<std::string, json_ptr>;
16+
using tool_choice_t = std::variant<std::string, json_ptr>;
17+
18+
params(std::string tools = "", std::string choice = "auto");
19+
20+
params(const params & other) = default;
21+
params(params && other) noexcept = default;
22+
params & operator=(const params & other) = default;
23+
params & operator=(params && other) noexcept = default;
24+
25+
operator bool() const;
26+
27+
void tools(std::string tools);
28+
const tools_t tools() const { return tools_; }
29+
30+
void choice(std::string choice);
31+
const tool_choice_t & choice() const { return tool_choice_; }
32+
33+
private:
34+
tools_t tools_;
35+
tool_choice_t tool_choice_;
36+
};
37+
}

0 commit comments

Comments
 (0)