feat(clients): meta: Meta Model API module, Muse Code protocol client, demo and tests
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
// Routes: GET /ai/meta, /ai/meta/status, /ai/meta/calls (no credential, no upstream)
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
Fixture f;
|
||||
auto page = f.client->get("/ai/meta");
|
||||
check(page.status == 200, "page 200");
|
||||
check(contains(page.headers.get("Content-Type"), "text/html"), "page is html");
|
||||
check(contains(page.body, "Meta Model API") && contains(page.body, "/embedded/ai_meta.js"),
|
||||
"page names the product and loads its script");
|
||||
|
||||
auto status = f.client->get("/ai/meta/status");
|
||||
check(status.status == 200, "status 200");
|
||||
check(contains(status.headers.get("Content-Type"), "application/json"), "status is json");
|
||||
check(contains(status.body, "https://api.meta.ai/v1"), "base url shown");
|
||||
check(contains(status.body, "muse-spark-1.3") && contains(status.body, "sam-3.1"), "models listed");
|
||||
check(contains(status.body, "\"hosted\":false"), "open-weight models flagged as self-hosted");
|
||||
check(!contains(status.body, "LLM_"), "no credential material shown");
|
||||
|
||||
auto calls = f.client->get("/ai/meta/calls");
|
||||
check(calls.status == 200 && contains(calls.body, "/responses/{response_id}/cancel") &&
|
||||
contains(calls.body, "/asr/realtime"),
|
||||
"call table listed");
|
||||
|
||||
check(f.client->post("/ai/meta").status == 405, "post to page is 405");
|
||||
check(f.client->post("/ai/meta/status").status == 405, "post to status is 405");
|
||||
check(f.client->get("/ai/meta/responses").status == 405, "get on a vendor route is 405");
|
||||
return finish("ai/meta/page");
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
// POST routes under /ai/meta: 422 on bad input, dry runs for paid/destructive
|
||||
// calls, 503 not_configured without a credential. No vendor reply is scripted.
|
||||
#include <cstdlib>
|
||||
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
unsetenv("META_API_KEY");
|
||||
unsetenv("MODEL_API_KEY");
|
||||
Fixture f;
|
||||
auto post = [&](const std::string& path, const std::string& body) { return f.client->post(path, body); };
|
||||
|
||||
// 422 before any credential check.
|
||||
check(post("/ai/meta/responses", "{}").status == 422, "responses: model required");
|
||||
check(post("/ai/meta/responses", "not json").status == 422, "responses: invalid JSON");
|
||||
check(post("/ai/meta/responses", R"({"model":"muse-spark-1.3","input":"x","max_output_tokens":15})").status == 422,
|
||||
"responses: max_output_tokens below 16");
|
||||
check(post("/ai/meta/responses", R"({"model":"muse-spark-1.3","input":"x","reasoning_effort":"none"})").status == 422,
|
||||
"responses: reasoning none rejected");
|
||||
check(post("/ai/meta/responses", R"({"model":"muse-spark-1.2","input":"x","reasoning_effort":"max"})").status == 422,
|
||||
"responses: max effort needs wonder or spark 1.3");
|
||||
check(post("/ai/meta/chat", R"({"model":"muse-spark-1.3"})").status == 422, "chat: message required");
|
||||
check(post("/ai/meta/messages", R"({"model":"muse-spark-1.3","message":"hi"})").status == 422, "messages: max_tokens required");
|
||||
check(post("/ai/meta/responses/get", R"({"id":"../x"})").status == 422, "id path guard");
|
||||
check(post("/ai/meta/files", R"({"action":"explode"})").status == 422, "files: unknown action");
|
||||
check(post("/ai/meta/files", R"({"action":"get"})").status == 422, "files: id required");
|
||||
check(post("/ai/meta/images", R"({"prompt":"x","n":11})").status == 422, "images: n above 10");
|
||||
check(post("/ai/meta/images", R"({"prompt":"x","size":"3200x100"})").status == 422, "images: aspect over 16:1");
|
||||
check(post("/ai/meta/segment", R"({"prompt":"bus, bicycle","image_url":"https://e.x/a.jpg"})").status == 422,
|
||||
"segment: one concept per request");
|
||||
check(post("/ai/meta/segment", R"({"prompt":"bus"})").status == 422, "segment: media required");
|
||||
check(post("/ai/meta/transcribe", R"({"audio_b64":"%%%"})").status == 422, "transcribe: bad base64");
|
||||
check(post("/ai/meta/transcribe", R"({"audio_b64":"AAAA"})").status == 422, "transcribe: not a WAV");
|
||||
check(post("/ai/meta/responses", std::string(9 * 1024 * 1024, 'x')).status == 413, "body cap");
|
||||
|
||||
// Dry runs need no credential.
|
||||
auto img = post("/ai/meta/images", R"({"prompt":"a red fox"})");
|
||||
check(img.status == 200 && contains(img.body, "\"dry_run\":true"), "images: dry run without confirm");
|
||||
auto del = post("/ai/meta/responses/delete", R"({"id":"resp_1"})");
|
||||
check(del.status == 200 && contains(del.body, "\"dry_run\":true"), "delete response: dry run");
|
||||
auto fdel = post("/ai/meta/files", R"({"action":"delete","id":"file-1"})");
|
||||
check(fdel.status == 200 && contains(fdel.body, "\"dry_run\":true"), "delete file: dry run");
|
||||
auto seg = post("/ai/meta/segment", R"({"prompt":"yellow school bus","image_url":"https://e.x/a.jpg"})");
|
||||
check(seg.status == 200 && contains(seg.body, "\"dry_run\":true"), "segment: dry run");
|
||||
auto code = post("/ai/meta/code", "{}");
|
||||
check(code.status == 200 && contains(code.body, "muse serve"), "muse code: dry run names the command");
|
||||
|
||||
// 503 without a credential.
|
||||
check(post("/ai/meta/responses", R"({"model":"muse-spark-1.3","input":"hi"})").status == 503, "responses: 503");
|
||||
check(contains(post("/ai/meta/responses", R"({"model":"muse-spark-1.3","input":"hi"})").body, "not_configured"),
|
||||
"responses: error code not_configured");
|
||||
check(post("/ai/meta/responses/count", R"({"model":"muse-spark-1.3","input":"hi"})").status == 503, "count: 503");
|
||||
check(post("/ai/meta/chat", R"({"model":"muse-spark-1.3","message":"hi"})").status == 503, "chat: 503");
|
||||
check(post("/ai/meta/messages", R"({"model":"muse-spark-1.3","message":"hi","max_tokens":64})").status == 503, "messages: 503");
|
||||
check(post("/ai/meta/models", "{}").status == 503, "models: 503");
|
||||
check(post("/ai/meta/files", R"({"action":"list"})").status == 503, "files list: 503");
|
||||
check(post("/ai/meta/responses/get", R"({"id":"resp_1"})").status == 503, "retrieve: 503");
|
||||
check(post("/ai/meta/images", R"({"prompt":"x","confirm":true})").status == 503, "images confirmed: 503");
|
||||
check(post("/ai/meta/files", R"({"action":"delete","id":"file-1","confirm":true})").status == 503, "file delete confirmed: 503");
|
||||
return finish("ai/meta/routes");
|
||||
}
|
||||
@@ -0,0 +1,443 @@
|
||||
// Meta Model API module: call table, catalog, validation, request bodies,
|
||||
// fixtures taken from the vendor docs, SAM parsing and mask decoding, retry
|
||||
// policy, usage. No vendor reply is scripted for verification: fixtures are
|
||||
// the documented examples, labelled as such.
|
||||
#include <set>
|
||||
|
||||
#include "framework.h"
|
||||
#include "meta/calls.h"
|
||||
#include "meta/catalog.h"
|
||||
#include "meta/chat_completions.h"
|
||||
#include "meta/chat_model.h"
|
||||
#include "meta/check.h"
|
||||
#include "meta/client.h"
|
||||
#include "meta/images.h"
|
||||
#include "meta/messages.h"
|
||||
#include "meta/response_events.h"
|
||||
#include "meta/response_input.h"
|
||||
#include "meta/response_request.h"
|
||||
#include "meta/sam.h"
|
||||
#include "meta/schema_check.h"
|
||||
#include "meta/speech.h"
|
||||
#include "meta/usage.h"
|
||||
|
||||
using namespace molo::meta;
|
||||
using molo::llm::ApiError;
|
||||
|
||||
namespace {
|
||||
ResponseRequest basic() {
|
||||
ResponseRequest r;
|
||||
r.model = "muse-spark-1.3";
|
||||
r.input = Json("What is the capital of France?");
|
||||
return r;
|
||||
}
|
||||
template <typename F>
|
||||
bool rejects(F&& f) {
|
||||
try {
|
||||
f();
|
||||
} catch (const ApiError& e) {
|
||||
return e.http_status() == 400;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST(meta, call_table_shape) {
|
||||
ASSERT_EQ(call_count(), 20u);
|
||||
std::set<std::string> seen;
|
||||
for (std::size_t i = 0; i < call_count(); ++i) {
|
||||
const CallSpec& s = spec_at(i);
|
||||
ASSERT_TRUE(seen.insert(std::string(s.http_method) + " " + s.path).second);
|
||||
ASSERT_TRUE(s.path[0] == '/');
|
||||
}
|
||||
ASSERT_EQ(path_for(Call::kResponsesCancel, "resp_1"), "/responses/resp_1/cancel");
|
||||
ASSERT_EQ(path_for(Call::kFilesContent, "file-842549258569145"), "/files/file-842549258569145/content");
|
||||
ASSERT_TRUE(rejects([] { path_for(Call::kFilesRetrieve, "../x"); }));
|
||||
ASSERT_TRUE(rejects([] { path_for(Call::kFilesRetrieve, ""); }));
|
||||
ASSERT_TRUE(rejects([] { method_of(Call::kAsrRealtime); }));
|
||||
}
|
||||
|
||||
TEST(meta, catalog_matches_docs_tables) {
|
||||
const ModelInfo* spark = ModelCatalog::find("muse-spark-1.3");
|
||||
ASSERT_TRUE(spark != nullptr);
|
||||
ASSERT_EQ(spark->context_tokens, 1048576L);
|
||||
ASSERT_TRUE(spark->input_per_m == 1.25 && spark->output_per_m == 4.25);
|
||||
ASSERT_TRUE(ModelCatalog::accepts_max_effort("muse-wonder-1.0"));
|
||||
ASSERT_FALSE(ModelCatalog::accepts_max_effort("muse-spark-1.3-contributor"));
|
||||
ASSERT_FALSE(ModelCatalog::accepts_max_effort("muse-spark-1.2"));
|
||||
ASSERT_FALSE(ModelCatalog::find("muse-glimmer")->hosted);
|
||||
ASSERT_TRUE(ModelCatalog::find("nope") == nullptr);
|
||||
// 1M cached + 1M fresh input + 1M output on muse-spark-1.3.
|
||||
MetaUsage u;
|
||||
u.input_tokens = 2000000;
|
||||
u.cached_tokens = 1000000;
|
||||
u.output_tokens = 1000000;
|
||||
double c = u.cost_usd("muse-spark-1.3");
|
||||
ASSERT_TRUE(c > 5.6499 && c < 5.6501);
|
||||
ASSERT_TRUE(u.cost_usd("sam-3.1") < 0);
|
||||
}
|
||||
|
||||
TEST(meta, response_request_rules) {
|
||||
Json b = basic().to_json();
|
||||
ASSERT_EQ(b.find("model")->as_string(), "muse-spark-1.3");
|
||||
ASSERT_TRUE(b.find("stream") == nullptr);
|
||||
ASSERT_TRUE(basic().to_json(true).find("stream")->as_bool());
|
||||
|
||||
ResponseRequest r = basic();
|
||||
r.max_output_tokens = 15;
|
||||
ASSERT_TRUE(rejects([&] { r.validate(); }));
|
||||
r = basic();
|
||||
r.reasoning_effort = "none";
|
||||
ASSERT_TRUE(rejects([&] { r.validate(); }));
|
||||
r.reasoning_effort = "max";
|
||||
r.model = "muse-spark-1.2";
|
||||
ASSERT_TRUE(rejects([&] { r.validate(); }));
|
||||
r.model = "muse-wonder-1.0";
|
||||
r.validate();
|
||||
r = basic();
|
||||
r.background = true;
|
||||
ASSERT_TRUE(rejects([&] { r.validate(true); }));
|
||||
r = basic();
|
||||
r.include = {"reasoning.encrypted_content"};
|
||||
r.previous_response_id = "resp_abc123";
|
||||
ASSERT_TRUE(rejects([&] { r.validate(); }));
|
||||
r = basic();
|
||||
r.include = {"message.output_text.logprobs"};
|
||||
ASSERT_TRUE(rejects([&] { r.validate(); }));
|
||||
r = basic();
|
||||
r.truncation = "auto";
|
||||
ASSERT_TRUE(rejects([&] { r.validate(); }));
|
||||
r = basic();
|
||||
r.top_p = 0;
|
||||
ASSERT_TRUE(rejects([&] { r.validate(); }));
|
||||
r = basic();
|
||||
r.input = Json();
|
||||
ASSERT_TRUE(rejects([&] { r.validate(); }));
|
||||
r = basic();
|
||||
r.tools.push_back(ToolBuilder::function("a.b", "x", Json::object()));
|
||||
r.validate();
|
||||
ASSERT_TRUE(rejects([&] { ToolBuilder::function("a.b.c", "x", Json::object()); }));
|
||||
r = basic();
|
||||
r.background = true;
|
||||
r.input = Json(std::string(1048577, 'x'));
|
||||
ASSERT_TRUE(rejects([&] { r.to_json(false); }));
|
||||
// count body carries no sampling or storage fields
|
||||
r = basic();
|
||||
r.store = false;
|
||||
r.temperature = 1.0;
|
||||
ASSERT_TRUE(r.to_count_json().find("store") == nullptr);
|
||||
ASSERT_TRUE(r.to_count_json().find("temperature") == nullptr);
|
||||
}
|
||||
|
||||
TEST(meta, reasoning_replay_item_shape) {
|
||||
Json it = ResponseInput::reasoning("ENC");
|
||||
ASSERT_TRUE(it.find("summary")->as_array().empty()); // summary is required on replay
|
||||
ASSERT_TRUE(it.find("id") == nullptr);
|
||||
ASSERT_EQ(it.find("encrypted_content")->as_string(), "ENC");
|
||||
ASSERT_TRUE(rejects([] { ResponseInput::function_call_output(std::string(65, 'x'), "o"); }));
|
||||
Json img = ResponseInput::image_bytes("image/png", "abc");
|
||||
ASSERT_EQ(img.find("image_url")->as_string(), "data:image/png;base64,YWJj");
|
||||
ASSERT_TRUE(rejects([] { ResponseInput::image_bytes("image/bmp", "abc"); }));
|
||||
}
|
||||
|
||||
TEST(meta, schema_preflight) {
|
||||
Json s = Json::parse(R"({"type":"object","properties":{"a":{"type":"string"}},"required":["a"],"additionalProperties":false})");
|
||||
SchemaCheck::check(s, true);
|
||||
Json loose = Json::parse(R"({"type":"object","properties":{"a":{"type":"string"}}})");
|
||||
SchemaCheck::check(loose, false);
|
||||
ASSERT_TRUE(rejects([&] { SchemaCheck::check(loose, true); }));
|
||||
Json rec = Json::parse(R"({"type":"object","properties":{"n":{"$ref":"#/$defs/N"}},"$defs":{"N":{"type":"object","properties":{"c":{"$ref":"#/$defs/N"}}}}})");
|
||||
ASSERT_TRUE(rejects([&] { SchemaCheck::check(rec, false); }));
|
||||
Json deep = Json::object();
|
||||
Json* cur = &deep;
|
||||
(void)cur;
|
||||
std::string text;
|
||||
for (int i = 0; i < 12; ++i) text += "{\"type\":\"object\",\"properties\":{\"x\":";
|
||||
text += "{\"type\":\"string\"}";
|
||||
for (int i = 0; i < 12; ++i) text += "}}";
|
||||
ASSERT_TRUE(rejects([&] { SchemaCheck::check(Json::parse(text), false); }));
|
||||
}
|
||||
|
||||
TEST(meta, chat_completion_rules) {
|
||||
ChatCompletionRequest r;
|
||||
r.model = "muse-spark-1.3";
|
||||
r.messages = {ChatMessage::developer("be brief"), ChatMessage::user("hi")};
|
||||
Json b = r.to_json(true);
|
||||
ASSERT_TRUE(b.find("stream_options")->find("include_usage")->as_bool());
|
||||
ASSERT_TRUE(b.find("n") == nullptr && b.find("stop") == nullptr);
|
||||
r.extra = Json::parse(R"({"n":2})");
|
||||
ASSERT_TRUE(rejects([&] { r.validate(); }));
|
||||
r.extra = Json();
|
||||
r.reasoning_effort = "none";
|
||||
ASSERT_TRUE(rejects([&] { r.validate(); }));
|
||||
r.reasoning_effort = "low";
|
||||
r.tools = {Json::parse(R"({"type":"custom","name":"x"})")};
|
||||
ASSERT_TRUE(rejects([&] { r.validate(); }));
|
||||
ASSERT_EQ(ChatMessage::tool_result("call_1", "ok").find("role")->as_string(), "tool");
|
||||
}
|
||||
|
||||
TEST(meta, messages_rules) {
|
||||
MessageRequest r;
|
||||
r.model = "muse-spark-1.3";
|
||||
r.messages = {AnthropicBlock::message_text("user", "hi")};
|
||||
ASSERT_TRUE(rejects([&] { r.validate(); })); // max_tokens required
|
||||
r.max_tokens = 2048;
|
||||
r.thinking = "enabled";
|
||||
r.thinking_budget = 512;
|
||||
ASSERT_TRUE(rejects([&] { r.validate(); }));
|
||||
r.thinking_budget = 1024;
|
||||
r.validate();
|
||||
r.extra = Json::parse(R"({"top_k":5})");
|
||||
ASSERT_TRUE(rejects([&] { r.validate(); }));
|
||||
r.extra = Json();
|
||||
r.temperature = 1.5;
|
||||
ASSERT_TRUE(rejects([&] { r.validate(); }));
|
||||
// Docs example: content starts with redacted_thinking, so read text blocks.
|
||||
Json msg = Json::parse(R"({"content":[{"type":"redacted_thinking","data":"x"},{"type":"text","text":"Hi"}],"usage":{"input_tokens":3,"output_tokens":4}})");
|
||||
ASSERT_EQ(MessagesService::text_of(msg), "Hi");
|
||||
ASSERT_EQ(MessagesService::usage_of(msg).total_tokens, 7);
|
||||
}
|
||||
|
||||
TEST(meta, images_rules) {
|
||||
ImageRequest r;
|
||||
r.prompt = "a red fox";
|
||||
r.validate(false);
|
||||
r.n = 11;
|
||||
ASSERT_TRUE(rejects([&] { r.validate(false); }));
|
||||
r.n = 2;
|
||||
r.size = "1792x1024";
|
||||
r.validate(false);
|
||||
r.size = "3200x100"; // 32:1
|
||||
ASSERT_TRUE(rejects([&] { r.validate(false); }));
|
||||
r.size = "";
|
||||
ASSERT_TRUE(rejects([&] { r.validate(true); })); // edit needs images
|
||||
r.images = {ImageRequest::image_url("https://example.com/a.png")};
|
||||
ASSERT_TRUE(r.to_json(true).find("images") != nullptr);
|
||||
r.extra = Json::parse(R"({"moderation":"low"})");
|
||||
ASSERT_TRUE(rejects([&] { r.validate(true); }));
|
||||
// Docs "Response shape" example.
|
||||
ImageResult res = ImageResult::from_json(Json::parse(
|
||||
R"({"created":1784584435,"data":[{"b64_json":"UklGR..."}],"output_format":"webp","background":"opaque","usage":{"input_tokens":9831,"output_tokens":749,"total_tokens":10580}})"));
|
||||
ASSERT_EQ(res.data.size(), 1u);
|
||||
ASSERT_EQ(res.output_format, "webp");
|
||||
}
|
||||
|
||||
TEST(meta, responses_documented_example) {
|
||||
// Verbatim from docs protocols/responses "Example response".
|
||||
Json doc = Json::parse(R"({"id":"resp_abc123","object":"response","created_at":1714502400,"status":"completed","model":"muse-spark-1.3","store":true,"temperature":1.0,"top_p":1.0,"max_output_tokens":null,"tools":[],"tool_choice":"auto","parallel_tool_calls":true,"service_tier":"auto","output":[{"id":"msg_abc123:456","type":"message","role":"assistant","status":"completed","content":[{"type":"output_text","text":"The capital of France is **Paris**.","annotations":[]}]}],"usage":{"input_tokens":69,"output_tokens":163,"total_tokens":232}})");
|
||||
molo::llm::ChatResponse r = MetaChatModel::from_response(doc);
|
||||
ASSERT_EQ(r.text(), "The capital of France is **Paris**.");
|
||||
ASSERT_EQ(r.finish_reason, "stop");
|
||||
ASSERT_EQ(r.usage.total_tokens, 232);
|
||||
ASSERT_EQ(r.id, "resp_abc123");
|
||||
}
|
||||
|
||||
TEST(meta, response_stream_fold_uses_schema_event_names) {
|
||||
// Event names and fields from the ResponseStreamEvent schemas; the order is
|
||||
// constructed (the docs ship no verbatim Responses transcript), see SPEC.
|
||||
const char* events[] = {
|
||||
R"({"type":"response.created","sequence_number":0,"response":{"id":"resp_1"}})",
|
||||
R"({"type":"response.output_item.added","sequence_number":1,"output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"get_weather","arguments":""}})",
|
||||
R"({"type":"response.function_call_arguments.delta","sequence_number":2,"item_id":"fc_1","output_index":0,"delta":"{\"city\":"})",
|
||||
R"({"type":"response.function_call_arguments.delta","sequence_number":3,"item_id":"fc_1","output_index":0,"delta":"\"Paris\"}"})",
|
||||
R"({"type":"response.output_text.delta","sequence_number":4,"item_id":"m","output_index":1,"content_index":0,"delta":"Hel"})",
|
||||
R"({"type":"response.output_text.delta","sequence_number":5,"item_id":"m","output_index":1,"content_index":0,"delta":"lo"})",
|
||||
R"({"type":"response.completed","sequence_number":6,"response":{"id":"resp_1","status":"completed","usage":{"input_tokens":5,"output_tokens":7,"total_tokens":12,"output_tokens_details":{"reasoning_tokens":3}}}})"};
|
||||
ResponseAccumulator acc;
|
||||
for (const char* e : events) {
|
||||
molo::llm::SseEvent se;
|
||||
se.data = Json::parse(e);
|
||||
ResponseEvent ev = ResponseEvent::from_sse(se);
|
||||
ASSERT_TRUE(ResponseEvents::is_known(ev.type));
|
||||
acc.feed(ev);
|
||||
}
|
||||
ASSERT_EQ(acc.text(), "Hello");
|
||||
ASSERT_TRUE(acc.completed());
|
||||
ASSERT_EQ(acc.calls().size(), 1u);
|
||||
ASSERT_EQ(acc.calls()[0].call_id, "call_1");
|
||||
ASSERT_EQ(acc.calls()[0].arguments, "{\"city\":\"Paris\"}");
|
||||
ASSERT_EQ(acc.usage().reasoning_tokens, 3);
|
||||
ASSERT_EQ(acc.response_id(), "resp_1");
|
||||
}
|
||||
|
||||
TEST(meta, chat_model_mapping) {
|
||||
auto client = MetaClient::with_key("");
|
||||
MetaChatModel m(client);
|
||||
molo::llm::ChatRequest req;
|
||||
req.model = "muse-spark-1.3";
|
||||
req.system = "be brief";
|
||||
req.messages.push_back(molo::llm::Message::user("hi"));
|
||||
molo::llm::Message a;
|
||||
a.role = molo::llm::Role::kAssistant;
|
||||
molo::llm::ContentPart reasoning;
|
||||
reasoning.reasoning_signature = "ENC123";
|
||||
a.parts.push_back(reasoning);
|
||||
molo::llm::ToolCall tc;
|
||||
tc.id = "call_1";
|
||||
tc.name = "lookup";
|
||||
tc.arguments.set("q", Json("x"));
|
||||
a.parts.push_back(molo::llm::ContentPart::of_call(tc));
|
||||
req.messages.push_back(a);
|
||||
molo::llm::Message t;
|
||||
t.role = molo::llm::Role::kTool;
|
||||
t.parts.push_back(molo::llm::ContentPart::of_result({"call_1", "result", false}));
|
||||
req.messages.push_back(t);
|
||||
req.max_tokens = 100;
|
||||
Json b = m.to_response_request(req).to_json();
|
||||
ASSERT_EQ(b.find("instructions")->as_string(), "be brief");
|
||||
ASSERT_FALSE(b.find("store")->as_bool());
|
||||
ASSERT_EQ(b.find("include")->as_array()[0].as_string(), "reasoning.encrypted_content");
|
||||
const auto& items = b.find("input")->as_array();
|
||||
ASSERT_EQ(items.size(), 4u); // user, reasoning, function_call, function_call_output
|
||||
ASSERT_EQ(items[1].find("type")->as_string(), "reasoning");
|
||||
ASSERT_EQ(items[2].find("arguments")->as_string(), "{\"q\":\"x\"}");
|
||||
ASSERT_EQ(items[3].find("type")->as_string(), "function_call_output");
|
||||
req.tool_choice = "required";
|
||||
ASSERT_TRUE(rejects([&] { m.to_response_request(req); }));
|
||||
req.tool_choice = "";
|
||||
req.stop = {"x"};
|
||||
ASSERT_TRUE(rejects([&] { m.to_chat_request(req); }));
|
||||
ASSERT_EQ(m.vendor(), "meta");
|
||||
// Not configured: no traffic.
|
||||
req.stop.clear();
|
||||
try {
|
||||
m.generate(req);
|
||||
FAIL("expected NotConfiguredError");
|
||||
} catch (const molo::llm::NotConfiguredError&) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(meta, sam_output_and_masks_match_vendor_conformance_cases) {
|
||||
// Docs "Video output grammar" line (payload bytes are the docs' own).
|
||||
std::string doc = "<0f>0<|box;x1=211;y1=228;x2=270;y2=254;w=320;h=334|><|mask;x=0;y=0;data=27,60,~!!!!M!0c[0o91w?q1!pIH4pPRVp2B3'7`e.ioeAf6-k/#Xd8%dX9x(|>,1<|box;x1=155;y1=228;x2=202;y2=254;w=320;h=334|><|mask;x=0;y=0;data=27,48,~!!!!J!0c[q=Pj_zs=*4C4(/./x#:/`S_GnD`=o3?X{emCgO$y@|>\n";
|
||||
auto frames = SamOutput::parse(doc);
|
||||
ASSERT_EQ(frames.size(), 1u);
|
||||
ASSERT_EQ(frames[0].records.size(), 2u);
|
||||
ASSERT_EQ(frames[0].records[1].object_id, 1);
|
||||
ASSERT_EQ(frames[0].records[0].box.right(), 271);
|
||||
ASSERT_EQ(frames[0].records[0].mask.height, 27);
|
||||
ASSERT_EQ(frames[0].records[0].mask.width, 60);
|
||||
ASSERT_EQ(frames[0].records[0].mask.encoding_name(), "lossless");
|
||||
ASSERT_TRUE(SamOutput::parse("").empty()); // zero matches is valid
|
||||
ASSERT_TRUE(rejects([] { SamOutput::parse("<0f>0<|box;x1=1|>"); }));
|
||||
ASSERT_TRUE(rejects([] { SamOutput::parse("junk"); }));
|
||||
|
||||
// meta-sam conformance case captured-api-image-lossless (vendor repo).
|
||||
auto cap = SamOutput::parse("<0f>0<|box;x1=513;y1=371;x2=523;y2=381;w=1440;h=900|><|mask;x=0;y=0;data=11,11,~!!!!r!kCq#$.pF[_cZIw.?wO3R8-&NJofiCI$_J0aA.1VC*YyK_'z*(LEG8!+H9evY.82Z-4.@C[.9C@T86o$3E=-&8w3W=]xS.|>\n");
|
||||
std::string want = "0000110000000111111100011111111100111111111011111111111111111111110111111111101111111111011111111100011111111000001110000";
|
||||
auto raster = cap[0].records[0].mask.decode();
|
||||
std::string got;
|
||||
for (auto v : raster) got += static_cast<char>('0' + v);
|
||||
ASSERT_EQ(got, want);
|
||||
|
||||
// meta-sam conformance case live-api-content-part-done (one_bit).
|
||||
auto ob = SamOutput::parse("<0f>0<|box;x1=0;y1=0;x2=4;y2=4;w=20;h=10|><|mask;x=0;y=0;data=5,5,!!!!!(QO(0lu8?|>\n");
|
||||
std::string got2;
|
||||
for (auto v : ob[0].records[0].mask.decode()) got2 += static_cast<char>('0' + v);
|
||||
ASSERT_EQ(got2, "1001000110000001011110001");
|
||||
SamMask corrupt = ob[0].records[0].mask;
|
||||
corrupt.payload = "!!!";
|
||||
ASSERT_TRUE(rejects([&] { corrupt.decode(); }));
|
||||
}
|
||||
|
||||
TEST(meta, sam_prompts_and_request) {
|
||||
ASSERT_EQ(SamPrompt::concept_text("yellow school bus"), "yellow school bus");
|
||||
ASSERT_TRUE(rejects([] { SamPrompt::concept_text("bus, bicycle"); }));
|
||||
ASSERT_TRUE(rejects([] { SamPrompt::concept_text("bus and bicycle"); }));
|
||||
// Docs object-prompt example (1800x1200 image).
|
||||
SamObject o1;
|
||||
o1.id = 1;
|
||||
o1.box = SamBox{444, 626, 675, 849, 1800, 1200, -1};
|
||||
SamObject o2;
|
||||
o2.id = 2;
|
||||
o2.w = 1800;
|
||||
o2.h = 1200;
|
||||
o2.points = {{1430, 700, true}, {1300, 450, false}};
|
||||
ASSERT_EQ(SamPrompt::objects_text(0, {o1, o2}),
|
||||
"<0f>1<|box;x1=444;y1=626;x2=675;y2=849;w=1800;h=1200|>,2<|point;x=1430;y=700;w=1800;h=1200|>-<|point;x=1300;y=450;w=1800;h=1200|>");
|
||||
ASSERT_TRUE(rejects([&] { SamPrompt::objects_text(0, {o1, o1}); }));
|
||||
SamRequest sr;
|
||||
sr.prompt = "button";
|
||||
sr.image_url = "https://example.com/a.png";
|
||||
sr.include_confidence = true;
|
||||
sr.mask_encoding = "one_bit";
|
||||
Json b = sr.to_response_request().to_json(true);
|
||||
ASSERT_EQ(b.find("model")->as_string(), "sam-3.1");
|
||||
ASSERT_EQ(b.find("metadata")->find("include_confidence")->as_string(), "true"); // string, not bool
|
||||
sr.video_url = "https://example.com/v.mp4";
|
||||
ASSERT_TRUE(rejects([&] { sr.to_response_request(); }));
|
||||
}
|
||||
|
||||
TEST(meta, speech_wav_and_events) {
|
||||
auto wav = [](int rate, int channels, int bits, int seconds) {
|
||||
int data = rate * channels * (bits / 8) * seconds;
|
||||
auto le32 = [](int v) { return std::string{char(v), char(v >> 8), char(v >> 16), char(v >> 24)}; };
|
||||
auto le16 = [](int v) { return std::string{char(v), char(v >> 8)}; };
|
||||
std::string h = "RIFF" + le32(36 + data) + "WAVEfmt " + le32(16) + le16(1) + le16(channels) +
|
||||
le32(rate) + le32(rate * channels * bits / 8) + le16(channels * bits / 8) +
|
||||
le16(bits) + "data" + le32(data);
|
||||
return h + std::string(static_cast<std::size_t>(data), '\0');
|
||||
};
|
||||
WavInfo ok = WavInfo::parse(wav(16000, 1, 16, 2));
|
||||
ASSERT_EQ(ok.duration_ms, 2000);
|
||||
ASSERT_TRUE(rejects([&] { WavInfo::parse(wav(44100, 1, 16, 1)); }));
|
||||
ASSERT_TRUE(rejects([&] { WavInfo::parse(wav(16000, 2, 16, 1)); }));
|
||||
ASSERT_TRUE(rejects([&] { WavInfo::parse("not a wav"); }));
|
||||
SpeechOptions o;
|
||||
o.mode = "DIARIZATION";
|
||||
o.keywords = {"Muse"};
|
||||
Json s = o.settings_json("WAV");
|
||||
ASSERT_EQ(s.find("audioEncoding")->as_string(), "WAV");
|
||||
o.mode = "LOUD";
|
||||
ASSERT_TRUE(rejects([&] { o.validate(); }));
|
||||
// Docs speech-to-text transcript / speechComplete examples.
|
||||
SpeechEvent t = SpeechEvent::parse(Json::parse(R"({"type":"transcript","transcript":"How is the weather","final":false,"audioProcessedMs":2400})"));
|
||||
ASSERT_EQ(t.type, "transcript");
|
||||
ASSERT_FALSE(t.final_flag);
|
||||
SpeechEvent c = SpeechEvent::parse(Json::parse(R"({"type":"speechComplete","turnId":1,"transcript":"How is the weather?","audioProcessedMs":4640})"));
|
||||
ASSERT_EQ(c.turn_id, 1);
|
||||
// Handshake frame carries the credential, redacted unless revealed.
|
||||
RealtimeConfig cfg;
|
||||
cfg.key = "LLM_1_secret";
|
||||
cfg.options.mode = "ENDPOINTING";
|
||||
RealtimeSession rs(cfg);
|
||||
ASSERT_EQ(rs.handshake_json(false).find("authorization")->find("accessToken")->as_string(), "Bearer ***");
|
||||
ASSERT_EQ(rs.handshake_json(true).find("authorization")->find("accessToken")->as_string(), "Bearer LLM_1_secret");
|
||||
}
|
||||
|
||||
TEST(meta, retry_policy_and_rate_headers) {
|
||||
ApiError e429("meta", 429, "rate_limit_error", "slow");
|
||||
ApiError e504("meta", 504, "server_error", "gateway_timeout");
|
||||
ApiError e400("meta", 400, "invalid_request_error", "bad");
|
||||
ASSERT_TRUE(RetryPolicy::retryable(e429));
|
||||
ASSERT_FALSE(RetryPolicy::retryable(e504));
|
||||
ASSERT_FALSE(RetryPolicy::retryable(e400));
|
||||
ASSERT_TRUE(RetryPolicy::decide(e429, 0, 7).retry);
|
||||
ASSERT_FALSE(RetryPolicy::decide(e429, 5, 7).retry);
|
||||
int d0 = RetryPolicy::delay_ms(0, 7), d3 = RetryPolicy::delay_ms(3, 7), d9 = RetryPolicy::delay_ms(9, 7);
|
||||
ASSERT_TRUE(d0 >= 500 && d0 <= 750);
|
||||
ASSERT_TRUE(d3 >= 4000 && d3 <= 6000);
|
||||
ASSERT_TRUE(d9 <= 45000);
|
||||
ASSERT_EQ(RetryPolicy::delay_ms(2, 99), RetryPolicy::delay_ms(2, 99));
|
||||
int calls = 0, slept = 0;
|
||||
int v = call_with_retry([&] {
|
||||
if (++calls < 3) throw ApiError("meta", 503, "server_error", "busy");
|
||||
return 42;
|
||||
}, 1, [&](int ms) { slept += ms; });
|
||||
ASSERT_EQ(v, 42);
|
||||
ASSERT_EQ(calls, 3);
|
||||
ASSERT_TRUE(slept >= 1500);
|
||||
molo::clients::HeaderMap h;
|
||||
h.set("x-ratelimit-limit-tokens", "4000000");
|
||||
h.set("x-ratelimit-remaining-requests", "2999");
|
||||
RateLimits rl = RateLimits::from_headers(h);
|
||||
ASSERT_EQ(rl.limit_tokens, 4000000);
|
||||
ASSERT_EQ(rl.remaining_requests, 2999);
|
||||
ASSERT_EQ(rl.remaining_tokens, -1);
|
||||
}
|
||||
|
||||
TEST(meta, usage_shapes) {
|
||||
ASSERT_EQ(MetaUsage::from_chat(Json::parse(R"({"prompt_tokens":10,"completion_tokens":5,"total_tokens":15,"prompt_tokens_details":{"cached_tokens":4},"completion_tokens_details":{"reasoning_tokens":2}})")).cached_tokens, 4);
|
||||
MetaUsage s = MetaUsage::from_responses(Json::parse(R"({"input_tokens":1,"output_tokens":2,"total_tokens":3,"video_frames_processed":50})"));
|
||||
ASSERT_EQ(s.video_frames_processed, 50);
|
||||
}
|
||||
@@ -0,0 +1,350 @@
|
||||
// Meta services through the real ApiClient over MockTransport: request paths,
|
||||
// headers, bodies against the call table; error envelopes from the docs;
|
||||
// SSE decoding of documented stream transcripts; MSP client against the
|
||||
// vendor's recorded handshake transcript; opt-in live checks.
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include "framework.h"
|
||||
#include "clients/transports/mock_transport.h"
|
||||
#include "meta/chat_completions.h"
|
||||
#include "meta/chat_model.h"
|
||||
#include "meta/client.h"
|
||||
#include "meta/files.h"
|
||||
#include "meta/images.h"
|
||||
#include "meta/messages.h"
|
||||
#include "meta/models.h"
|
||||
#include "meta/muse_code.h"
|
||||
#include "meta/responses.h"
|
||||
#include "meta/speech.h"
|
||||
|
||||
using namespace molo::meta;
|
||||
using molo::clients::ClientResponse;
|
||||
using molo::clients::HeaderMap;
|
||||
using molo::clients::Method;
|
||||
using molo::clients::transports::MockTransport;
|
||||
using molo::llm::ApiError;
|
||||
|
||||
// Captured at load: other suites change the environment while they run.
|
||||
static const bool kLiveNet = std::getenv("MOLO_LIVE_NET") && std::string(std::getenv("MOLO_LIVE_NET")) == "1";
|
||||
static const std::string kLiveKey = MetaClient::key_from_env();
|
||||
|
||||
namespace {
|
||||
ClientResponse reply(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<molo::llm::ApiClient> client;
|
||||
explicit Rig(const std::string& key = "LLM_1_SECRETKEY") {
|
||||
auto t = std::make_unique<MockTransport>();
|
||||
mock = t.get();
|
||||
client = std::make_shared<molo::llm::ApiClient>(std::move(t), MetaClient::config(), key);
|
||||
}
|
||||
};
|
||||
|
||||
std::string tiny_wav() {
|
||||
auto le32 = [](int v) { return std::string{char(v), char(v >> 8), char(v >> 16), char(v >> 24)}; };
|
||||
auto le16 = [](int v) { return std::string{char(v), char(v >> 8)}; };
|
||||
return "RIFF" + le32(40) + "WAVEfmt " + le32(16) + le16(1) + le16(1) + le32(16000) + le32(32000) +
|
||||
le16(2) + le16(16) + "data" + le32(4) + "abcd";
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
bool rejects_msp(F&& f) {
|
||||
try {
|
||||
f();
|
||||
} catch (const ApiError& e) {
|
||||
return e.http_status() == 400;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
ResponseRequest basic() {
|
||||
ResponseRequest r;
|
||||
r.model = "muse-spark-1.3";
|
||||
r.input = Json("hi");
|
||||
return r;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST(meta_net, create_response_path_auth_body) {
|
||||
Rig r;
|
||||
r.mock->expect_response(Method::kPost, "/v1/responses", reply(R"({"id":"resp_1","status":"completed","output":[{"type":"message","content":[{"type":"output_text","text":"Hi"}]}],"usage":{"input_tokens":1,"output_tokens":2,"total_tokens":3}})"));
|
||||
ResponsesService svc(r.client);
|
||||
ResponseResult res = svc.create(basic());
|
||||
ASSERT_EQ(res.id(), "resp_1");
|
||||
ASSERT_EQ(res.output_text(), "Hi");
|
||||
ASSERT_EQ(res.usage().total_tokens, 3);
|
||||
auto s = r.mock->seen()[0];
|
||||
ASSERT_EQ(s.headers.get_or("Authorization", ""), "Bearer LLM_1_SECRETKEY");
|
||||
ASSERT_EQ(s.body, R"({"input":"hi","model":"muse-spark-1.3"})");
|
||||
ASSERT_EQ(s.target.find("SECRET"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST(meta_net, not_configured_makes_no_traffic) {
|
||||
Rig r("");
|
||||
ResponsesService svc(r.client);
|
||||
try {
|
||||
svc.create(basic());
|
||||
FAIL("expected NotConfiguredError");
|
||||
} catch (const molo::llm::NotConfiguredError& e) {
|
||||
ASSERT_EQ(e.http_status(), 503);
|
||||
ASSERT_TRUE(e.message().find("META_API_KEY") != std::string::npos);
|
||||
}
|
||||
ASSERT_EQ(r.mock->send_count(), 0u);
|
||||
}
|
||||
|
||||
TEST(meta_net, docs_error_envelopes_map_to_api_error) {
|
||||
Rig r;
|
||||
r.mock->expect_response(Method::kPost, "/v1/responses",
|
||||
reply(R"({"error":{"message":"`top_p`: The number must be `<= 1.0`.","type":"invalid_request_error","param":"top_p","code":null}})", 400));
|
||||
r.mock->expect_response(Method::kGet, "/v1/files/file-nonexistent",
|
||||
reply(R"({"error":{"message":"No file found with id 'file-nonexistent'.","type":"invalid_request_error","param":null,"code":"file_not_found"}})", 404));
|
||||
r.mock->expect_response(Method::kPost, "/v1/responses",
|
||||
reply(R"({"error":{"message":"Rate limit exceeded. Please retry after 15 seconds.","type":"rate_limit_error","param":null,"code":"rate_limit_exceeded"}})", 429));
|
||||
ResponsesService svc(r.client);
|
||||
try { svc.create(basic()); FAIL("400"); } catch (const ApiError& e) {
|
||||
ASSERT_EQ(e.http_status(), 400);
|
||||
ASSERT_EQ(e.type(), "invalid_request_error");
|
||||
}
|
||||
FilesService files(r.client);
|
||||
try { files.retrieve("file-nonexistent"); FAIL("404"); } catch (const ApiError& e) { ASSERT_EQ(e.http_status(), 404); }
|
||||
try { svc.create(basic()); FAIL("429"); } catch (const ApiError& e) { ASSERT_TRUE(RetryPolicy::retryable(e)); }
|
||||
}
|
||||
|
||||
TEST(meta_net, response_stream_over_sse) {
|
||||
Rig r;
|
||||
std::string sse =
|
||||
"event: response.created\ndata: {\"type\":\"response.created\",\"sequence_number\":0,\"response\":{\"id\":\"resp_9\"}}\n\n"
|
||||
"event: response.output_text.delta\ndata: {\"type\":\"response.output_text.delta\",\"sequence_number\":1,\"item_id\":\"m\",\"output_index\":0,\"content_index\":0,\"delta\":\"Hi\"}\n\n"
|
||||
"event: response.completed\ndata: {\"type\":\"response.completed\",\"sequence_number\":2,\"response\":{\"id\":\"resp_9\",\"status\":\"completed\",\"usage\":{\"input_tokens\":1,\"output_tokens\":1,\"total_tokens\":2}}}\n\n"
|
||||
"data: [DONE]\n\n";
|
||||
r.mock->expect_response(Method::kPost, "/v1/responses", reply(sse, 200, "text/event-stream"));
|
||||
ResponsesService svc(r.client);
|
||||
std::vector<std::string> types;
|
||||
ResponseAccumulator acc = svc.stream(basic(), [&](const ResponseEvent& e) { types.push_back(e.type); return true; });
|
||||
ASSERT_EQ(acc.text(), "Hi");
|
||||
ASSERT_TRUE(acc.completed());
|
||||
ASSERT_EQ(types.size(), 3u);
|
||||
auto s = r.mock->seen()[0];
|
||||
ASSERT_EQ(s.headers.get_or("Accept", ""), "text/event-stream");
|
||||
ASSERT_TRUE(s.body.find("\"stream\":true") != std::string::npos);
|
||||
}
|
||||
|
||||
TEST(meta_net, lifecycle_calls_and_destructive_paths) {
|
||||
Rig r;
|
||||
r.mock->expect_response(Method::kGet, "/v1/responses/resp_1", reply(R"({"id":"resp_1"})"));
|
||||
r.mock->expect_response(Method::kDelete, "/v1/responses/resp_1", reply(R"({"id":"resp_1","object":"response.deleted","deleted":true})"));
|
||||
r.mock->expect_response(Method::kPost, "/v1/responses/resp_1/cancel", reply(R"({"id":"resp_1","status":"cancelled"})"));
|
||||
r.mock->expect_response(Method::kPost, "/v1/responses/input_tokens", reply(R"({"object":"response.input_tokens","input_tokens":321})"));
|
||||
ResponsesService svc(r.client);
|
||||
ASSERT_EQ(svc.retrieve("resp_1").id(), "resp_1");
|
||||
ASSERT_TRUE(svc.remove("resp_1").find("deleted")->as_bool());
|
||||
ASSERT_EQ(svc.cancel("resp_1").find("status")->as_string(), "cancelled");
|
||||
ASSERT_EQ(svc.count_input_tokens(basic()), 321);
|
||||
ASSERT_EQ(r.mock->send_count(), 4u);
|
||||
try { svc.remove("../etc"); FAIL("bad id"); } catch (const ApiError& e) { ASSERT_EQ(e.http_status(), 400); }
|
||||
ASSERT_EQ(r.mock->send_count(), 4u);
|
||||
}
|
||||
|
||||
TEST(meta_net, chat_and_messages_paths) {
|
||||
Rig r;
|
||||
r.mock->expect_response(Method::kPost, "/v1/chat/completions", reply(R"({"id":"c1","model":"muse-spark-1.3","choices":[{"index":0,"finish_reason":"stop","message":{"role":"assistant","content":"yo"}}],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}})"));
|
||||
r.mock->expect_response(Method::kPost, "/v1/messages", reply(R"({"id":"m1","type":"message","role":"assistant","content":[{"type":"text","text":"ok"}],"usage":{"input_tokens":1,"output_tokens":1}})"));
|
||||
r.mock->expect_response(Method::kPost, "/v1/messages/count_tokens", reply(R"({"input_tokens":12})"));
|
||||
ChatCompletionRequest cr;
|
||||
cr.model = "muse-spark-1.3";
|
||||
cr.messages = {ChatMessage::user("hi")};
|
||||
ChatCompletionsService chat(r.client);
|
||||
ASSERT_EQ(MetaChatModel::from_chat(chat.create(cr)).text(), "yo");
|
||||
MessageRequest mr;
|
||||
mr.model = "muse-spark-1.3";
|
||||
mr.max_tokens = 64;
|
||||
mr.messages = {AnthropicBlock::message_text("user", "hi")};
|
||||
MessagesService ms(r.client);
|
||||
ASSERT_EQ(MessagesService::text_of(ms.create(mr)), "ok");
|
||||
ASSERT_EQ(ms.count_tokens(mr), 12);
|
||||
ASSERT_TRUE(r.mock->seen()[2].body.find("max_tokens") == std::string::npos
|
||||
|| r.mock->seen()[2].body.find("\"stream\"") == std::string::npos);
|
||||
}
|
||||
|
||||
TEST(meta_net, chat_stream_chunks) {
|
||||
Rig r;
|
||||
std::string sse =
|
||||
"data: {\"id\":\"c\",\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"A\"}}]}\n\n"
|
||||
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_1\",\"type\":\"function\",\"function\":{\"name\":\"f\",\"arguments\":\"{\\\"a\\\"\"}}]}}]}\n\n"
|
||||
"data: {\"id\":\"c\",\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\":1}\"}}]},\"finish_reason\":\"tool_calls\"}],\"usage\":{\"prompt_tokens\":3,\"completion_tokens\":4,\"total_tokens\":7}}\n\n"
|
||||
"data: [DONE]\n\n";
|
||||
r.mock->expect_response(Method::kPost, "/v1/chat/completions", reply(sse, 200, "text/event-stream"));
|
||||
ChatCompletionRequest cr;
|
||||
cr.model = "muse-spark-1.3";
|
||||
cr.messages = {ChatMessage::user("hi")};
|
||||
ChatChunkAccumulator acc = ChatCompletionsService(r.client).stream(cr);
|
||||
ASSERT_EQ(acc.text(), "A");
|
||||
ASSERT_EQ(acc.tool_calls().size(), 1u);
|
||||
ASSERT_EQ(acc.tool_calls()[0].arguments, "{\"a\":1}");
|
||||
ASSERT_EQ(acc.finish_reason(), "tool_calls");
|
||||
ASSERT_EQ(acc.usage().total_tokens, 7);
|
||||
}
|
||||
|
||||
TEST(meta_net, files_upload_list_walk_and_caps) {
|
||||
Rig r;
|
||||
r.mock->expect_response(Method::kPost, "/v1/files", reply(R"({"id":"file-842549258569145","object":"file","bytes":3,"created_at":1,"filename":"a.png","purpose":"user_data","status":"processed"})"));
|
||||
FileUpload up;
|
||||
up.filename = "a.png";
|
||||
up.mime = "image/png";
|
||||
up.bytes = "abc";
|
||||
FilesService fs(r.client);
|
||||
FileObject f = fs.upload(up);
|
||||
ASSERT_EQ(f.id, "file-842549258569145");
|
||||
auto s = r.mock->seen()[0];
|
||||
ASSERT_TRUE(s.headers.get_or("Content-Type", "").find("multipart/form-data") == 0);
|
||||
ASSERT_TRUE(s.body.find("name=\"purpose\"") < s.body.find("name=\"file\"")); // purpose before file
|
||||
up.expires_after_seconds = 10;
|
||||
try { fs.upload(up); FAIL("expiry range"); } catch (const ApiError& e) { ASSERT_EQ(e.http_status(), 400); }
|
||||
// Two pages, then done.
|
||||
r.mock->expect_response(Method::kGet, "/v1/files", reply(R"({"object":"list","data":[{"id":"file-1"}],"first_id":"file-1","last_id":"file-1","has_more":true})"));
|
||||
r.mock->expect_response(Method::kGet, "/v1/files", reply(R"({"object":"list","data":[{"id":"file-2"}],"first_id":"file-2","last_id":"file-2","has_more":false})"));
|
||||
FileList all = fs.list_all("", 1);
|
||||
ASSERT_EQ(all.files.size(), 2u);
|
||||
ASSERT_FALSE(all.truncated);
|
||||
try { fs.list_page("", 100, "sideways"); FAIL("order"); } catch (const ApiError& e) { ASSERT_EQ(e.http_status(), 400); }
|
||||
}
|
||||
|
||||
TEST(meta_net, models_images_and_transcribe_paths) {
|
||||
Rig r;
|
||||
r.mock->expect_response(Method::kGet, "/v1/models", reply(R"({"object":"list","data":[{"id":"muse-spark-1.3","object":"model","created":1,"owned_by":"meta"}]})"));
|
||||
r.mock->expect_response(Method::kPost, "/v1/images/generations", reply(R"({"created":1,"data":[{"b64_json":"AA=="}]})"));
|
||||
auto models = ModelsService(r.client).list();
|
||||
ASSERT_EQ(models.size(), 1u);
|
||||
ASSERT_EQ(models[0].id, "muse-spark-1.3");
|
||||
ImageRequest ir;
|
||||
ir.prompt = "fox";
|
||||
ASSERT_EQ(ImagesService(r.client).generate(ir).data.size(), 1u);
|
||||
ASSERT_TRUE(r.mock->seen()[1].body.find("muse-image-1.0") != std::string::npos);
|
||||
// Transcribe: multipart with a JSON request part and the audio, Accept honoured.
|
||||
std::string wav = tiny_wav();
|
||||
r.mock->expect_response(Method::kPost, "/v1/asr/transcribe", reply(R"({"sessionId":"my-session","transcript":"hello","audioDurationMs":1,"turns":[{"turnId":0,"startMs":0,"endMs":1,"transcript":"hello"}]})"));
|
||||
SpeechOptions so;
|
||||
so.session_id = "my-session";
|
||||
so.mode = "ENDPOINTING";
|
||||
TranscribeResult tr = TranscribeService(r.client).transcribe(so, wav);
|
||||
ASSERT_EQ(tr.transcript, "hello");
|
||||
ASSERT_EQ(tr.turns.size(), 1u);
|
||||
auto s = r.mock->seen()[2];
|
||||
ASSERT_TRUE(s.body.find("name=\"request\"\r\nContent-Type: application/json") != std::string::npos);
|
||||
ASSERT_TRUE(s.body.find("\"audioEncoding\":\"WAV\"") != std::string::npos);
|
||||
ASSERT_EQ(s.headers.get_or("Accept", ""), "application/json");
|
||||
}
|
||||
|
||||
TEST(meta_net, transcribe_event_stream_documented_sample) {
|
||||
Rig r;
|
||||
// Verbatim from docs speech-to-text "Choose another response format".
|
||||
std::string sse =
|
||||
"event: transcript\ndata: {\"type\":\"transcript\",\"transcript\":\"How is the weather\",\"final\":false,\"audioProcessedMs\":2400}\n\n"
|
||||
"event: speechComplete\ndata: {\"type\":\"speechComplete\",\"turnId\":1,\"transcript\":\"How is the weather?\",\"audioProcessedMs\":4640}\n\n";
|
||||
r.mock->expect_response(Method::kPost, "/v1/asr/transcribe", reply(sse, 200, "text/event-stream"));
|
||||
std::string wav = tiny_wav();
|
||||
auto ev = TranscribeService(r.client).transcribe_events(SpeechOptions(), wav);
|
||||
ASSERT_EQ(ev.size(), 2u);
|
||||
ASSERT_EQ(ev[1].type, "speechComplete");
|
||||
ASSERT_EQ(ev[1].turn_id, 1);
|
||||
ASSERT_EQ(r.mock->seen()[0].headers.get_or("Accept", ""), "text/event-stream");
|
||||
}
|
||||
|
||||
namespace {
|
||||
// Replays the vendor's recorded `session-start` client lines against our
|
||||
// client: we check what we WRITE equals the recorded client frames (ids
|
||||
// normalized), and that the recorded server frames parse.
|
||||
class RecordedTransport : public ILineTransport {
|
||||
public:
|
||||
std::vector<std::string> written;
|
||||
std::vector<std::string> to_read;
|
||||
std::size_t pos = 0;
|
||||
void write_line(const std::string& l) override { written.push_back(l); }
|
||||
std::optional<std::string> read_line(int) override {
|
||||
if (pos >= to_read.size()) return std::nullopt;
|
||||
return to_read[pos++];
|
||||
}
|
||||
void close() override {}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
TEST(meta_net, msp_handshake_matches_recorded_transcript) {
|
||||
// schema/msp/transcripts/handshake-liststream-granted (muse-code-sdk, verbatim server line).
|
||||
auto t = std::make_unique<RecordedTransport>();
|
||||
RecordedTransport* rec = t.get();
|
||||
rec->to_read.push_back(R"J({"id":1,"jsonrpc":"2.0","result":{"experimentalApi":false,"grantedCapabilities":["sessionListStream"],"museHome":"/home/fixture/.muse","platformFamily":"unix","platformOs":"linux","schema":{"fingerprint":"sha256:c8d1a2a1866814e220fd396d382a9a75861412feee884b5021b2ee359bd3dc59","version":1},"serverInfo":{"name":"muse-session-server","version":"0.0.0-fixture"},"sessionDurability":"durable","userAgent":"muse-session-server/0.0.0-fixture (linux; x86_64)"}})J");
|
||||
MspClient c(std::move(t));
|
||||
Json res = c.initialize("conformance", "0.0.0", {"sessionListStream"});
|
||||
ASSERT_EQ(res.find("grantedCapabilities")->as_array()[0].as_string(), "sessionListStream");
|
||||
ASSERT_EQ(c.schema_fingerprint(), "sha256:c8d1a2a1866814e220fd396d382a9a75861412feee884b5021b2ee359bd3dc59");
|
||||
ASSERT_EQ(rec->written[0],
|
||||
R"({"id":1,"jsonrpc":"2.0","method":"initialize","params":{"capabilities":{"requestedCapabilities":["sessionListStream"]},"clientInfo":{"name":"conformance","version":"0.0.0"}}})");
|
||||
ASSERT_EQ(rec->written[1], R"({"jsonrpc":"2.0","method":"initialized"})");
|
||||
}
|
||||
|
||||
TEST(meta_net, msp_requests_notifications_errors_and_caps) {
|
||||
auto t = std::make_unique<RecordedTransport>();
|
||||
RecordedTransport* rec = t.get();
|
||||
rec->to_read.push_back(R"({"jsonrpc":"2.0","id":1,"result":{"schema":{"version":1,"fingerprint":"sha256:x"}}})");
|
||||
rec->to_read.push_back(R"({"jsonrpc":"2.0","method":"session/started","params":{"session":{"sessionId":"s1"}},"emittedAtMs":1})");
|
||||
rec->to_read.push_back(R"({"jsonrpc":"2.0","method":"approval/request","id":"srv-1","params":{"sessionId":"s1"}})");
|
||||
rec->to_read.push_back(R"({"jsonrpc":"2.0","id":2,"error":{"code":-32020,"message":"no such session"}})");
|
||||
MspClient c(std::move(t));
|
||||
ASSERT_TRUE(rejects_msp([&] { c.model_list(); })); // before initialize
|
||||
c.initialize("t", "1");
|
||||
try {
|
||||
c.turn_start("s1", "hello");
|
||||
FAIL("expected sessionNotFound");
|
||||
} catch (const ApiError& e) {
|
||||
ASSERT_EQ(e.type(), "sessionNotFound");
|
||||
}
|
||||
auto n = c.next_notification(10);
|
||||
ASSERT_TRUE(n.has_value());
|
||||
ASSERT_EQ(n->method, "session/started");
|
||||
ASSERT_TRUE(rec->written.back().find("\"approval\"") == std::string::npos);
|
||||
bool answered = false;
|
||||
for (const std::string& w : rec->written)
|
||||
if (w.find("\"id\":\"srv-1\"") != std::string::npos) answered = true;
|
||||
ASSERT_TRUE(answered); // server request answered with a receipt
|
||||
ASSERT_TRUE(MspFrames::is_known_method("turn/start"));
|
||||
ASSERT_FALSE(MspFrames::is_known_method("turn/nope"));
|
||||
ASSERT_EQ(MspFrames::error_kind(-32001), "overloaded");
|
||||
ASSERT_TRUE(MspFrames::error_retryable(-32001));
|
||||
LineSplitter sp(8);
|
||||
sp.feed("ab\r\ncd\n");
|
||||
ASSERT_EQ(*sp.pop(), "ab");
|
||||
ASSERT_EQ(*sp.pop(), "cd");
|
||||
ASSERT_TRUE(rejects_msp([&] { sp.feed("123456789"); }));
|
||||
ASSERT_EQ(MspFrames::new_id().size(), 36u);
|
||||
}
|
||||
|
||||
TEST(meta_net, live_opt_in_unauthenticated_and_keyed) {
|
||||
if (!kLiveNet) {
|
||||
std::cout << "SKIP meta live checks: set MOLO_LIVE_NET=1 (and META_API_KEY or MODEL_API_KEY for keyed ones)\n";
|
||||
return;
|
||||
}
|
||||
ServiceStatus st = StatusService(MetaClient::unauthenticated()).get();
|
||||
ASSERT_TRUE(st.is_alive);
|
||||
if (kLiveKey.empty()) {
|
||||
std::cout << "SKIP meta keyed live checks: no META_API_KEY / MODEL_API_KEY\n";
|
||||
// Unauthenticated probe: a documented path answers 401 invalid_api_key, not 404.
|
||||
try {
|
||||
ModelsService(MetaClient::with_key("LLM_0_bogus")).list();
|
||||
FAIL("expected 401");
|
||||
} catch (const ApiError& e) {
|
||||
ASSERT_EQ(e.http_status(), 401);
|
||||
ASSERT_EQ(e.type(), "authentication_error");
|
||||
}
|
||||
return;
|
||||
}
|
||||
auto models = ModelsService(MetaClient::with_key(kLiveKey)).list(); // read only
|
||||
ASSERT_FALSE(models.empty());
|
||||
}
|
||||
Reference in New Issue
Block a user