feat(clients): llm vendor foundation: api client, SSE, errors, neutral chat types, ai.mk, example stubs
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,233 @@
|
||||
// Shared LLM layer: SSE decoding, auth styles, error envelopes, verbs,
|
||||
// multipart, streaming through the client, config lookup, neutral types.
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
|
||||
#include "framework.h"
|
||||
#include "clients/transports/mock_transport.h"
|
||||
#include "llm/api_client.h"
|
||||
#include "llm/chat_types.h"
|
||||
#include "llm/config.h"
|
||||
#include "llm/sse.h"
|
||||
|
||||
using namespace molo::llm;
|
||||
using molo::clients::ClientResponse;
|
||||
using molo::clients::HeaderMap;
|
||||
using molo::clients::Method;
|
||||
using molo::clients::body::Json;
|
||||
using molo::clients::transports::MockTransport;
|
||||
|
||||
namespace {
|
||||
ClientResponse ok(const std::string& body, int status = 200,
|
||||
const std::string& type = "application/json") {
|
||||
HeaderMap h;
|
||||
h.set("Content-Type", type);
|
||||
return ClientResponse(status, h, body);
|
||||
}
|
||||
struct Rig {
|
||||
MockTransport* mock;
|
||||
std::shared_ptr<ApiClient> client;
|
||||
explicit Rig(AuthStyle style = AuthStyle::kBearer, const std::string& key = "SECRETKEY") {
|
||||
auto t = std::make_unique<MockTransport>();
|
||||
mock = t.get();
|
||||
ApiClientConfig c;
|
||||
c.vendor = "acme";
|
||||
c.base_url = "https://api.acme.example";
|
||||
c.key_env = "ACME_API_KEY";
|
||||
c.auth = style;
|
||||
c.auth_header = "x-api-key";
|
||||
c.default_headers["acme-version"] = "2024-01-01";
|
||||
client = std::make_shared<ApiClient>(std::move(t), c, key);
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
TEST(llm, bearer_and_header_auth_plus_default_headers) {
|
||||
Rig b;
|
||||
b.mock->expect_response(Method::kGet, "/v1/x", ok("{\"a\":1}"));
|
||||
ASSERT_EQ(b.client->get("/v1/x").find("a")->as_int(), 1);
|
||||
auto s = b.mock->seen()[0];
|
||||
ASSERT_EQ(s.headers.get_or("Authorization", ""), "Bearer SECRETKEY");
|
||||
ASSERT_EQ(s.headers.get_or("acme-version", ""), "2024-01-01");
|
||||
ASSERT_EQ(s.target.find("SECRETKEY"), std::string::npos);
|
||||
|
||||
Rig h(AuthStyle::kHeader);
|
||||
h.mock->expect_response(Method::kPost, "/v1/y", ok("{}"));
|
||||
Json body = Json::object();
|
||||
body.set("k", Json("v"));
|
||||
h.client->post("/v1/y", body, {{"extra-beta", "1"}});
|
||||
auto s2 = h.mock->seen()[0];
|
||||
ASSERT_EQ(s2.headers.get_or("x-api-key", ""), "SECRETKEY");
|
||||
ASSERT_EQ(s2.headers.get_or("Authorization", ""), "");
|
||||
ASSERT_EQ(s2.headers.get_or("extra-beta", ""), "1");
|
||||
ASSERT_EQ(s2.body, "{\"k\":\"v\"}");
|
||||
}
|
||||
|
||||
TEST(llm, not_configured_before_network) {
|
||||
Rig r(AuthStyle::kBearer, "");
|
||||
ASSERT_FALSE(r.client->configured());
|
||||
try {
|
||||
r.client->get("/v1/x");
|
||||
FAIL("expected NotConfiguredError");
|
||||
} catch (const NotConfiguredError& e) {
|
||||
ASSERT_EQ(e.http_status(), 503);
|
||||
ASSERT_EQ(e.type(), "NOT_CONFIGURED");
|
||||
ASSERT_TRUE(e.message().find("ACME_API_KEY") != std::string::npos);
|
||||
}
|
||||
ASSERT_EQ(r.mock->send_count(), 0u);
|
||||
}
|
||||
|
||||
TEST(llm, error_envelopes) {
|
||||
struct Case { const char* body; int status; const char* type; const char* msg; };
|
||||
const Case cases[] = {
|
||||
{"{\"error\":{\"type\":\"authentication_error\",\"message\":\"bad key\"}}", 401, "authentication_error", "bad key"},
|
||||
{"{\"type\":\"error\",\"error\":{\"type\":\"not_found_error\",\"message\":\"nope\"}}", 404, "not_found_error", "nope"},
|
||||
{"{\"error\":{\"code\":\"rate_limit_exceeded\",\"message\":\"slow\"}}", 429, "rate_limit_exceeded", "slow"},
|
||||
{"{\"error\":\"plain text error\"}", 400, "UNKNOWN", "plain text error"},
|
||||
{"{\"message\":\"top level\",\"code\":\"E42\"}", 400, "E42", "top level"},
|
||||
{"<html>bad gateway</html>", 400, "UNKNOWN", "HTTP 400"},
|
||||
};
|
||||
for (const Case& c : cases) {
|
||||
Rig r;
|
||||
molo::clients::transports::MockExpectation ex; // 429/5xx are retried: allow every attempt
|
||||
ex.method = Method::kGet;
|
||||
ex.path = "/v1/e";
|
||||
ex.response = ok(c.body, c.status);
|
||||
ex.times = 5;
|
||||
r.mock->expect(ex);
|
||||
try {
|
||||
r.client->get("/v1/e");
|
||||
FAIL("expected ApiError");
|
||||
} catch (const ApiError& e) {
|
||||
ASSERT_EQ(e.http_status(), c.status);
|
||||
ASSERT_EQ(e.type(), c.type);
|
||||
ASSERT_EQ(e.message(), c.msg);
|
||||
ASSERT_EQ(e.vendor(), "acme");
|
||||
ASSERT_EQ(std::string(e.what()).find("SECRETKEY"), std::string::npos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(llm, request_id_header_is_captured) {
|
||||
Rig r;
|
||||
HeaderMap h;
|
||||
h.set("Content-Type", "application/json");
|
||||
h.set("request-id", "req_123");
|
||||
r.mock->expect_response(Method::kGet, "/v1/e", ClientResponse(500, h, "{\"error\":{\"message\":\"x\"}}"));
|
||||
try {
|
||||
r.client->get("/v1/e");
|
||||
FAIL("expected");
|
||||
} catch (const ApiError& e) {
|
||||
ASSERT_EQ(e.request_id(), "req_123");
|
||||
}
|
||||
}
|
||||
|
||||
TEST(llm, path_guard_and_empty_body) {
|
||||
Rig r;
|
||||
ASSERT_THROW(r.client->get("v1/x"), ApiError);
|
||||
ASSERT_THROW(r.client->get("/v1/../x"), ApiError);
|
||||
ASSERT_EQ(r.mock->send_count(), 0u);
|
||||
r.mock->expect_response(Method::kDelete, "/v1/x", ok("", 204));
|
||||
ASSERT_TRUE(r.client->del("/v1/x").is_object());
|
||||
}
|
||||
|
||||
TEST(llm, raw_and_multipart) {
|
||||
Rig r;
|
||||
r.mock->expect_response(Method::kGet, "/v1/f", ok("BINARY\x01\x02", 200, "application/octet-stream"));
|
||||
RawResponse raw = r.client->request_raw(Method::kGet, "/v1/f");
|
||||
ASSERT_EQ(raw.body, std::string("BINARY\x01\x02"));
|
||||
ASSERT_EQ(raw.content_type, "application/octet-stream");
|
||||
|
||||
r.mock->expect_response(Method::kPost, "/v1/files", ok("{\"id\":\"file_1\"}"));
|
||||
molo::clients::body::MultipartBody form;
|
||||
form.add_text("purpose", "assistants");
|
||||
form.add_file("file", "a.txt", "text/plain", "hello");
|
||||
ASSERT_EQ(r.client->post_multipart("/v1/files", form).find("id")->as_string(), "file_1");
|
||||
auto seen = r.mock->seen()[1];
|
||||
ASSERT_TRUE(seen.headers.get_or("Content-Type", "").find("multipart/form-data") == 0);
|
||||
ASSERT_TRUE(seen.body.find("hello") != std::string::npos);
|
||||
}
|
||||
|
||||
TEST(llm, sse_named_events_done_and_chunking) {
|
||||
std::vector<std::pair<std::string, std::string>> got;
|
||||
SseDecoder d("acme", [&](const SseEvent& e) {
|
||||
got.push_back({e.event, e.data.dump()});
|
||||
return true;
|
||||
});
|
||||
d.feed("event: message_start\ndata: {\"a\":1}\n\n: ping\n\nevent: delta\nda");
|
||||
d.feed("ta: {\"b\":2}\r\n\r\ndata: {\"c\":3}\n\ndata: [DONE]\n\ndata: {\"never\":1}\n\n");
|
||||
d.finish();
|
||||
ASSERT_EQ(got.size(), 3u);
|
||||
ASSERT_EQ(got[0].first, "message_start");
|
||||
ASSERT_EQ(got[1].second, "{\"b\":2}");
|
||||
ASSERT_EQ(got[2].first, "");
|
||||
}
|
||||
|
||||
TEST(llm, sse_errors_and_stop) {
|
||||
SseDecoder bad("acme", [](const SseEvent&) { return true; });
|
||||
ASSERT_THROW(bad.feed("data: {not json}\n\n"), ApiError);
|
||||
SseDecoder err("acme", [](const SseEvent&) { return true; });
|
||||
err.feed("{\"error\":{\"type\":\"overloaded_error\",\"message\":\"busy\"}}");
|
||||
try {
|
||||
err.finish();
|
||||
FAIL("expected");
|
||||
} catch (const ApiError& e) {
|
||||
ASSERT_EQ(e.type(), "overloaded_error");
|
||||
ASSERT_EQ(e.message(), "busy");
|
||||
}
|
||||
int n = 0;
|
||||
SseDecoder stop("acme", [&](const SseEvent&) { return ++n < 2; });
|
||||
ASSERT_FALSE(stop.feed("data: {\"a\":1}\n\ndata: {\"a\":2}\n\ndata: {\"a\":3}\n\n"));
|
||||
ASSERT_EQ(n, 2);
|
||||
}
|
||||
|
||||
TEST(llm, stream_through_client) {
|
||||
Rig r;
|
||||
r.mock->expect_response(
|
||||
Method::kPost, "/v1/chat",
|
||||
ok("data: {\"d\":\"a\"}\n\ndata: {\"d\":\"b\"}\n\ndata: [DONE]\n\n", 200, "text/event-stream"));
|
||||
std::string text;
|
||||
r.client->stream("/v1/chat", Json::object(), [&](const SseEvent& e) {
|
||||
text += e.data.find("d")->as_string();
|
||||
return true;
|
||||
});
|
||||
ASSERT_EQ(text, "ab");
|
||||
ASSERT_EQ(r.mock->seen()[0].headers.get_or("Accept", ""), "text/event-stream");
|
||||
}
|
||||
|
||||
TEST(llm, id_validation) {
|
||||
ASSERT_TRUE(Service::valid_id("msgbatch_01ABC"));
|
||||
ASSERT_TRUE(Service::valid_id("grok-4.1:fast"));
|
||||
ASSERT_FALSE(Service::valid_id(""));
|
||||
ASSERT_FALSE(Service::valid_id(".."));
|
||||
ASSERT_FALSE(Service::valid_id("a/b"));
|
||||
ASSERT_FALSE(Service::valid_id("a b"));
|
||||
ASSERT_FALSE(Service::valid_id(std::string(257, 'a')));
|
||||
}
|
||||
|
||||
TEST(llm, config_lookup_order_and_off_switch) {
|
||||
setenv("MOLO_ENV_FILES", "off", 1);
|
||||
unsetenv("MOLO_LIVE_NET");
|
||||
unsetenv("ACME_TEST_KNOB");
|
||||
ASSERT_EQ(Config::get("ACME_TEST_KNOB"), "");
|
||||
setenv("ACME_TEST_KNOB", "from-env", 1);
|
||||
ASSERT_EQ(Config::get("ACME_TEST_KNOB"), "from-env");
|
||||
unsetenv("ACME_TEST_KNOB");
|
||||
unsetenv("MOLO_ENV_FILES");
|
||||
}
|
||||
|
||||
TEST(llm, neutral_types) {
|
||||
Message m = Message::user("hi");
|
||||
ASSERT_EQ(m.text(), "hi");
|
||||
ASSERT_EQ(std::string(role_name(m.role)), "user");
|
||||
ChatResponse r;
|
||||
r.message.role = Role::kAssistant;
|
||||
r.message.parts.push_back(ContentPart::of_text("a"));
|
||||
ToolCall c;
|
||||
c.id = "t1";
|
||||
c.name = "f";
|
||||
r.message.parts.push_back(ContentPart::of_call(c));
|
||||
ASSERT_EQ(r.text(), "a");
|
||||
ASSERT_EQ(r.tool_calls().size(), 1u);
|
||||
ASSERT_EQ(r.tool_calls()[0].name, "f");
|
||||
}
|
||||
Reference in New Issue
Block a user