108 lines
3.4 KiB
C++
108 lines
3.4 KiB
C++
#include "vendors/mistral/calls.h"
|
|
|
|
#include <cstring>
|
|
|
|
#include "llm/api_client.h"
|
|
#include "llm/error.h"
|
|
|
|
namespace molo::mistral {
|
|
|
|
static const CallSpec kCalls[] = {
|
|
#include "vendors/mistral/calls_table.inc"
|
|
};
|
|
|
|
namespace {
|
|
bool listed(const char* csv, const std::string& item) {
|
|
std::string s = csv;
|
|
std::size_t at = 0;
|
|
while (at <= s.size()) {
|
|
std::size_t end = s.find(',', at);
|
|
if (end == std::string::npos) end = s.size();
|
|
if (s.compare(at, end - at, item) == 0 && end - at == item.size()) return true;
|
|
at = end + 1;
|
|
}
|
|
return false;
|
|
}
|
|
} // namespace
|
|
|
|
std::size_t Calls::count() { return sizeof(kCalls) / sizeof(kCalls[0]); }
|
|
|
|
const CallSpec& Calls::at(std::size_t index) {
|
|
if (index >= count()) throw llm::ApiError::invalid("mistral", "call index out of range");
|
|
return kCalls[index];
|
|
}
|
|
|
|
const CallSpec* Calls::try_find(std::string_view op) {
|
|
for (const CallSpec& s : kCalls)
|
|
if (op == s.op) return &s;
|
|
return nullptr;
|
|
}
|
|
|
|
const CallSpec& Calls::find(std::string_view op) {
|
|
const CallSpec* s = try_find(op);
|
|
if (!s) throw llm::ApiError::invalid("mistral", "unknown operation: " + std::string(op.substr(0, 80)));
|
|
return *s;
|
|
}
|
|
|
|
clients::Method Calls::method_of(const CallSpec& spec) {
|
|
const char* m = spec.http_method;
|
|
if (!std::strcmp(m, "GET")) return clients::Method::kGet;
|
|
if (!std::strcmp(m, "POST")) return clients::Method::kPost;
|
|
if (!std::strcmp(m, "PUT")) return clients::Method::kPut;
|
|
if (!std::strcmp(m, "PATCH")) return clients::Method::kPatch;
|
|
return clients::Method::kDelete;
|
|
}
|
|
|
|
bool Calls::is_destructive(const CallSpec& spec) {
|
|
if (!std::strcmp(spec.http_method, "DELETE")) return true;
|
|
std::string op = spec.op;
|
|
for (const char* word : {"cancel", "archive", "revoke", "purge", "reset", "reprocess"})
|
|
if (op.find(word) != std::string::npos) return true;
|
|
return false;
|
|
}
|
|
|
|
std::vector<std::string> Calls::path_params(const CallSpec& spec) {
|
|
std::vector<std::string> out;
|
|
std::string p = spec.path;
|
|
for (std::size_t at = p.find('{'); at != std::string::npos; at = p.find('{', at)) {
|
|
std::size_t end = p.find('}', at);
|
|
if (end == std::string::npos) break;
|
|
out.push_back(p.substr(at + 1, end - at - 1));
|
|
at = end;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
bool Calls::has_body_field(const CallSpec& spec, const std::string& field) {
|
|
return listed(spec.body_fields, field);
|
|
}
|
|
|
|
bool Calls::has_query(const CallSpec& spec, const std::string& name) {
|
|
return listed(spec.query_params, name);
|
|
}
|
|
|
|
std::string Calls::render(const CallSpec& spec, const std::vector<std::string>& ids) {
|
|
std::string path = spec.path;
|
|
std::size_t hash = path.find('#');
|
|
if (hash != std::string::npos) path.resize(hash);
|
|
std::size_t used = 0;
|
|
for (std::size_t at = path.find('{'); at != std::string::npos; at = path.find('{', at)) {
|
|
std::size_t end = path.find('}', at);
|
|
if (end == std::string::npos) break;
|
|
if (used >= ids.size()) throw llm::ApiError::invalid("mistral", "missing id for " + path.substr(at, end - at + 1));
|
|
if (!llm::Service::valid_id(ids[used]))
|
|
throw llm::ApiError::invalid("mistral", "bad id for " + path.substr(at, end - at + 1));
|
|
path.replace(at, end - at + 1, ids[used]);
|
|
at += ids[used].size();
|
|
++used;
|
|
}
|
|
if (used != ids.size()) throw llm::ApiError::invalid("mistral", "too many ids for " + std::string(spec.op));
|
|
return path;
|
|
}
|
|
|
|
std::string Calls::render(const std::string& op, const std::vector<std::string>& ids) {
|
|
return render(find(op), ids);
|
|
}
|
|
|
|
} // namespace molo::mistral
|