Repository navigation
Expand file tree
/
Copy pathtool.cpp
More file actions
76 lines (63 loc) · 2.7 KB
/
Copy pathtool.cpp
File metadata and controls
76 lines (63 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// SPDX-FileCopyrightText: 2026 Paolo Anzani
// SPDX-License-Identifier: Apache-2.0
#include "tool.h"
#include <charconv>
#include <cstddef>
#include <expected>
#include <optional>
#include <string>
#include <string_view>
#include <system_error>
#include "json.h"
namespace microcodex {
std::expected<std::string, std::string> ToolArguments::string(const std::string_view name) const {
auto value = json::scalarMember(json_, name);
if (!value) return std::unexpected(value.error());
return json::string(*value);
}
std::expected<std::string, std::string> ToolArguments::optionalString(const std::string_view name) const {
auto value = json::findJsonMember(json_, name);
if (!value) return std::unexpected(value.error());
const std::optional<std::string_view>& member = *value;
if (!member) return std::string();
return json::string(*member);
}
std::expected<std::size_t, std::string> ToolArguments::size(const std::string_view name) const {
auto value = json::scalarMember(json_, name);
if (!value) return std::unexpected(value.error());
std::size_t result = 0;
const auto [end, error] = std::from_chars(value->data(), value->data() + value->size(), result);
if (error != std::errc{} || end != value->data() + value->size()) {
return std::unexpected("JSON member '" + std::string(name) + "' is not a non-negative integer");
}
return result;
}
std::expected<bool, std::string> ToolArguments::boolean(const std::string_view name) const {
auto value = json::scalarMember(json_, name);
if (!value) return std::unexpected(value.error());
if (*value == "true") return true;
if (*value == "false") return false;
return std::unexpected("JSON member '" + std::string(name) + "' is not a boolean");
}
std::expected<bool, std::string> ToolArguments::boolean(const std::string_view name, const bool default_value) const {
auto value = json::findJsonMember(json_, name);
if (!value) return std::unexpected(value.error());
const std::optional<std::string_view>& member = *value;
if (!member) return default_value;
if (*member == "true") return true;
if (*member == "false") return false;
return std::unexpected("JSON member '" + std::string(name) + "' is not a boolean");
}
} // namespace microcodex
namespace microcodex::detail {
std::string toolJsonString(const ToolJsonFields& fields) {
std::string json = R"({"type":"function","name":)";
microcodex::json::appendString(json, fields.name);
json += R"(,"description":)";
microcodex::json::appendString(json, fields.description);
json += R"(,"strict":false,"parameters":)";
json += fields.parameters;
json += '}';
return json;
}
} // namespace microcodex::detail