MCPcopy Create free account
hub / github.com/0xShug0/audio.cpp / build_request_from_json

Function build_request_from_json

app/cli/request.cpp:91–230  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

89 return false;
90}
91
92engine::runtime::ArtifactKind parse_artifact_kind(const std::string & name, const std::string & where) {
93 for (const auto & entry : kArtifactKinds) {
94 if (name == entry.name) {
95 return entry.kind;
96 }
97 }
98 std::string names;
99 for (const auto & entry : kArtifactKinds) {
100 names += (names.empty() ? "" : ", ") + std::string(entry.name);
101 }
102 throw InvalidRequestError(where + ": unknown kind '" + name + "', expected one of " + names);
103}
104
105// A field that is absent or null reads as not given.
106const engine::io::json::Value * artifact_field(
107 const engine::io::json::Value & entry,
108 const std::string & key) {
109 const auto * value = entry.find(key);
110 return value == nullptr || value->is_null() ? nullptr : value;
111}
112
113std::string artifact_string(
114 const engine::io::json::Value & entry,
115 const std::string & key,
116 const std::string & where) {
117 const auto * value = artifact_field(entry, key);
118 if (value == nullptr || !value->is_string() || value->as_string().empty()) {
119 throw InvalidRequestError(where + ": " + key + " must be a non-empty string");
120 }
121 return value->as_string();
122}
123
124[[noreturn]] void throw_artifact_limit(const std::string & where) {
125 throw InvalidRequestError(
126 where + ": the request's artifact payloads total more than " +
127 std::to_string(kMaxRequestArtifactBytes) + " bytes");
128}
129
130std::vector<std::byte> decode_artifact_payload(
131 const engine::io::json::Value & payload,
132 uint64_t budget,
133 const std::string & where) {
134 if (!payload.is_string()) {
135 throw InvalidRequestError(where + ": payload must be a base64 string");
136 }
137 // An inline payload is already in memory as part of the request, so the
138 // server's request body limit bounds this decode.
139 std::vector<std::byte> bytes;
140 try {
141 bytes = minitts::app::base64_decode_bytes(payload.as_string());
142 } catch (const std::runtime_error & error) {
143 throw InvalidRequestError(where + ": payload is not valid base64 (" + error.what() + ")");
144 }
145 if (bytes.size() > budget) {
146 throw_artifact_limit(where);
147 }
148 return bytes;

Callers 6

handle_generic_runMethod · 0.85
run_model_step_implFunction · 0.85
run_chunked_model_stepFunction · 0.85
run_model_step_foreachFunction · 0.85
mainFunction · 0.85

Calls 11

json_optional_stringFunction · 0.85
resolve_case_pathFunction · 0.85
json_optional_floatFunction · 0.85
json_options_mapFunction · 0.85
set_optionFunction · 0.85
path_arg_stringFunction · 0.85
read_audio_bufferFunction · 0.70
to_stringFunction · 0.50
findMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected