diff --git a/example/app/ai/BedrockDemo.cpp b/example/app/ai/BedrockDemo.cpp index 7be3bc9..eb9ab75 100644 --- a/example/app/ai/BedrockDemo.cpp +++ b/example/app/ai/BedrockDemo.cpp @@ -1,13 +1,649 @@ #include "../AiDemos.hpp" +#include + #include "../../fw/Router.hpp" +#include "../../fw/Templates.hpp" +#include "vendors/bedrock/agent.h" +#include "vendors/bedrock/calls.h" +#include "vendors/bedrock/caps.h" +#include "vendors/bedrock/chat_model.h" +#include "vendors/bedrock/client.h" +#include "vendors/bedrock/control.h" +#include "vendors/bedrock/credentials.h" +#include "vendors/bedrock/runtime.h" +#include "vendors/bedrock/sigv4.h" namespace molo::example::app { -// bedrock. Stub: the package agent fills this in. +// Amazon Bedrock over the real src/vendors/bedrock package. GET routes need no +// credential. Every route that calls AWS is a POST: 422 on bad input, 503 +// not_configured without AWS_BEARER_TOKEN_BEDROCK or AWS_ACCESS_KEY_ID + +// AWS_SECRET_ACCESS_KEY. Routes that cost money or change a resource (inference, +// guardrail checks, agents, retrieval, batch create / stop) are dry runs that +// show the exact request unless the body has "confirm":true. /ai/bedrock/sign +// is local: it signs with AWS's documentation credentials only. + +namespace { + +namespace bk = bedrock; +using Json = clients::body::Json; + +constexpr std::size_t kMaxBody = 8 * 1024 * 1024; +constexpr std::size_t kMaxStreamChunks = 400; + +const char kPage[] = R"BEDROCK({% extends "base.html" %} +{% block content %} +

Amazon Bedrock

+

Converse and ConverseStream, InvokeModel (and stream), count tokens, guardrails, agents, +knowledge base retrieval, models, inference profiles, batch jobs, and a local SigV4 signer. Paid and +changing calls are dry runs until the body says "confirm":true.

+

Checking credential...

+
+ + +
+ + +
+ +

+
+ + +{% endblock %} +)BEDROCK"; + +[[noreturn]] void bad(const std::string& why) { throw fw::HttpError(422, "validation_error", why); } + +fw::Response json_out(const Json& j, int status = 200) { + fw::Response res = fw::JsonResponse(j.dump(), status); + res.headers.set("Cache-Control", "no-store"); + return res; +} + +Json parse_body(const fw::Request& req) { + if (req.body.size() > kMaxBody) throw fw::HttpError(413, "payload_too_large", "body too large"); + if (req.body.empty()) return Json::object(); + try { + Json j = Json::parse(req.body); + if (!j.is_object()) bad("body must be a JSON object"); + return j; + } catch (const clients::body::JsonError&) { + bad("body is not valid JSON"); + } +} + +std::string text(const Json& b, const char* key, bool required = false) { + const Json* v = b.find(key); + if (!v || v->is_null()) { + if (required) bad(std::string(key) + " is required"); + return ""; + } + if (!v->is_string()) bad(std::string(key) + " must be a string"); + return v->as_string(); +} + +std::optional number(const Json& b, const char* key) { + const Json* v = b.find(key); + if (!v || v->is_null()) return std::nullopt; + if (!v->is_number()) bad(std::string(key) + " must be a number"); + return static_cast(v->as_int()); +} + +std::optional real(const Json& b, const char* key) { + const Json* v = b.find(key); + if (!v || v->is_null()) return std::nullopt; + if (!v->is_number()) bad(std::string(key) + " must be a number"); + return v->as_number(); +} + +bool flag(const Json& b, const char* key) { + const Json* v = b.find(key); + if (!v || v->is_null()) return false; + if (!v->is_bool()) bad(std::string(key) + " must be true or false"); + return v->as_bool(); +} + +std::vector strings(const Json& b, const char* key) { + std::vector out; + const Json* v = b.find(key); + if (!v || v->is_null()) return out; + if (!v->is_array()) bad(std::string(key) + " must be an array of strings"); + for (const Json& s : v->as_array()) { + if (!s.is_string()) bad(std::string(key) + " must be an array of strings"); + out.push_back(s.as_string()); + } + return out; +} + +Json object_at(const Json& b, const char* key, bool required = false) { + const Json* v = b.find(key); + if (!v || v->is_null()) { + if (required) bad(std::string(key) + " is required"); + return Json::object(); + } + if (!v->is_object()) bad(std::string(key) + " must be an object"); + return *v; +} + +Json usage_json(const bk::ConverseUsage& u) { + Json o = Json::object(); + o.set("input_tokens", Json(u.input_tokens)); + o.set("output_tokens", Json(u.output_tokens)); + o.set("total_tokens", Json(u.total_tokens)); + o.set("cache_read_tokens", Json(u.cache_read_tokens)); + o.set("cache_write_tokens", Json(u.cache_write_tokens)); + return o; +} + +Json converse_json(const bk::ConverseResult& r) { + Json o = Json::object(); + o.set("text", Json(r.text())); + o.set("reasoning", Json(r.reasoning())); + o.set("stop_reason", Json(r.stop_reason())); + o.set("latency_ms", Json(static_cast(r.latency_ms()))); + o.set("usage", usage_json(r.usage())); + Json calls = Json::array(); + for (const bk::ConverseToolUse& t : r.tool_uses()) { + Json x = Json::object(); + x.set("id", Json(t.id)); + x.set("name", Json(t.name)); + x.set("input", t.input); + calls.push(x); + } + o.set("tool_uses", calls); + return o; +} + +Json list_json(const bk::ListResult& l, const char* key) { + Json o = Json::object(); + o.set(key, l.items); + o.set("count", Json(static_cast(l.items.as_array().size()))); + o.set("pages", Json(static_cast(l.pages))); + o.set("truncated", Json(l.truncated)); + if (!l.next_token.empty()) o.set("next_token", Json(l.next_token)); + return o; +} + +Json dry_run(const Json& request, const std::string& method, const std::string& path) { + Json o = Json::object(); + o.set("dry_run", Json(true)); + o.set("would_send_to", Json(method + " " + path)); + o.set("request", request); + o.set("hint", Json("send confirm:true to run it")); + return o; +} + +bk::ConverseRequest converse_request(const Json& b) { + bk::ConverseRequest r; + r.model_id = text(b, "model"); + if (r.model_id.empty()) r.model_id = bk::BedrockChatModel::kDefaultModel; + std::string system = text(b, "system"); + if (!system.empty()) r.add_system(system); + r.add_user(text(b, "prompt", true)); + r.max_tokens = number(b, "max_tokens"); + r.temperature = real(b, "temperature"); + r.top_p = real(b, "top_p"); + r.stop_sequences = strings(b, "stop"); + r.effort = text(b, "effort"); + r.guardrail_id = text(b, "guardrail_id"); + r.guardrail_version = text(b, "guardrail_version"); + r.service_tier = text(b, "service_tier"); + r.latency = text(b, "latency"); + r.additional_fields = object_at(b, "additional_fields"); + return r; +} + +// One route = validate into a Plan, then the generic runner decides dry run, +// credential, call and error mapping. +struct Plan { + Json request; // what would be sent (shown in a dry run) + std::string method = "POST"; + std::string path; + bool needs_confirm = false; + std::function call; +}; + +class BedrockRoutes { + public: + explicit BedrockRoutes(std::shared_ptr client) : client_(std::move(client)) {} + + const bk::BedrockClient& client() const { return *client_; } + + using Planner = std::function; + + fw::Response run(const fw::Request& req, const Planner& planner) const { + Json body = parse_body(req); + Plan plan; + try { + plan = planner(body); + } catch (const llm::ApiError& e) { + map(e); + } + if (plan.needs_confirm && !flag(body, "confirm")) return json_out(dry_run(plan.request, plan.method, plan.path)); + try { + return json_out(plan.call()); + } catch (const llm::ApiError& e) { + map(e); + } + } + + // ---- planners ----------------------------------------------------------- + Plan converse(const Json& b) const { + bk::ConverseRequest r = converse_request(b); + Json wire = r.to_json(false); + auto rt = std::make_shared(client_); + Plan pl{wire, "POST", bk::Calls::render(bk::Call::kConverse, {{"modelId", r.model_id}}), true, nullptr}; + pl.call = [rt, r] { return converse_json(rt->converse(r)); }; + return pl; + } + Plan converse_stream(const Json& b) const { + bk::ConverseRequest r = converse_request(b); + Json wire = r.to_json(true); + auto rt = std::make_shared(client_); + Plan pl{wire, "POST", bk::Calls::render(bk::Call::kConverseStream, {{"modelId", r.model_id}}), true, nullptr}; + pl.call = [rt, r] { + std::size_t events = 0; + bk::ConverseResult res = rt->converse_stream(r, [&](const bk::StreamDelta&) { + return ++events < bk::BedrockCaps::kMaxStreamEvents; + }); + Json o = converse_json(res); + o.set("events", Json(static_cast(events))); + return o; + }; + return pl; + } + Plan invoke(const Json& b) const { + std::string model = text(b, "model", true); + Json body = object_at(b, "body", true); + bk::InvokeOptions opt; + if (std::string ct = text(b, "content_type"); !ct.empty()) opt.content_type = ct; + opt.trace = text(b, "trace"); + opt.guardrail_id = text(b, "guardrail_id"); + opt.guardrail_version = text(b, "guardrail_version"); + opt.service_tier = text(b, "service_tier"); + opt.latency = text(b, "latency"); + opt.validate(); + if (!bk::Calls::valid_path_value(model)) bad("model has invalid characters"); + auto rt = std::make_shared(client_); + Plan pl{body, "POST", bk::Calls::render(bk::Call::kInvokeModel, {{"modelId", model}}), true, nullptr}; + pl.call = [rt, model, body, opt] { + bk::InvokeResult r = rt->invoke_model(model, body, opt); + Json o = Json::object(); + o.set("content_type", Json(r.content_type)); + try { + o.set("body", r.json()); + } catch (const llm::ApiError&) { + o.set("body_text", Json(r.body.substr(0, 20000))); + } + return o; + }; + return pl; + } + Plan invoke_stream(const Json& b) const { + std::string model = text(b, "model", true); + Json body = object_at(b, "body", true); + if (!bk::Calls::valid_path_value(model)) bad("model has invalid characters"); + auto rt = std::make_shared(client_); + Plan pl{body, "POST", bk::Calls::render(bk::Call::kInvokeModelStream, {{"modelId", model}}), true, nullptr}; + pl.call = [rt, model, body] { + Json chunks = Json::array(); + std::size_t n = rt->invoke_model_stream(model, body, [&](const std::string& c) { + chunks.push(Json(c.substr(0, 4000))); + return chunks.as_array().size() < kMaxStreamChunks; + }); + Json o = Json::object(); + o.set("chunks", chunks); + o.set("count", Json(static_cast(n))); + return o; + }; + return pl; + } + Plan count_tokens(const Json& b) const { + bk::ConverseRequest r = converse_request(b); + Json wire = r.to_count_json(); + auto rt = std::make_shared(client_); + Plan pl{wire, "POST", bk::Calls::render(bk::Call::kCountTokens, {{"modelId", r.model_id}}), false, nullptr}; + pl.call = [rt, r] { + Json o = Json::object(); + o.set("input_tokens", Json(static_cast(rt->count_tokens(r)))); + return o; + }; + return pl; + } + Plan guardrail(const Json& b) const { + std::string id = text(b, "guardrail_id", true); + std::string version = text(b, "version"); + if (version.empty()) version = "DRAFT"; + std::string source = text(b, "source"); + if (source.empty()) source = "INPUT"; + std::string scope = text(b, "scope"); + Json block = bk::Runtime::guardrail_text(text(b, "text", true), strings(b, "qualifiers")); + if (!bk::Calls::valid_path_value(id) || !bk::Calls::valid_path_value(version)) bad("guardrail id or version has invalid characters"); + if (source != "INPUT" && source != "OUTPUT") bad("source must be INPUT or OUTPUT"); + if (!scope.empty() && scope != "INTERVENTIONS" && scope != "FULL") bad("scope must be INTERVENTIONS or FULL"); + Json req = Json::object(); + req.set("source", Json(source)); + Json content = Json::array(); + content.push(block); + req.set("content", content); + auto rt = std::make_shared(client_); + Plan pl{req, "POST", bk::Calls::render(bk::Call::kApplyGuardrail, {{"guardrailIdentifier", id}, {"guardrailVersion", version}}), true, nullptr}; + pl.call = [rt, id, version, source, block, scope] { + bk::GuardrailResult r = rt->apply_guardrail(id, version, source, {block}, scope); + Json o = Json::object(); + o.set("action", Json(r.action())); + o.set("intervened", Json(r.intervened())); + o.set("output_text", Json(r.output_text())); + o.set("raw", r.raw); + return o; + }; + return pl; + } + Plan agent(const Json& b) const { + bk::AgentInvokeParams p; + p.agent_id = text(b, "agent_id", true); + p.alias_id = text(b, "alias_id", true); + p.session_id = text(b, "session_id", true); + p.input_text = text(b, "prompt", true); + p.enable_trace = flag(b, "trace"); + p.end_session = flag(b, "end_session"); + p.memory_id = text(b, "memory_id"); + Json wire = p.to_json(); + auto ar = std::make_shared(client_); + Plan pl{wire, "POST", bk::Calls::render(bk::Call::kInvokeAgent, {{"agentId", p.agent_id}, {"agentAliasId", p.alias_id}, {"sessionId", p.session_id}}), true, nullptr}; + pl.call = [ar, p] { + bk::AgentResult r = ar->invoke_agent(p); + Json o = Json::object(); + o.set("text", Json(r.text)); + o.set("session_id", Json(r.session_id)); + o.set("chunks", Json(static_cast(r.chunks))); + o.set("traces", Json(static_cast(r.traces.as_array().size()))); + o.set("return_control", r.return_control); + return o; + }; + return pl; + } + Plan retrieve(const Json& b) const { + bk::RetrieveParams p; + p.knowledge_base_id = text(b, "knowledge_base_id", true); + p.query = text(b, "query", true); + p.number_of_results = number(b, "number_of_results").value_or(0); + p.search_type = text(b, "search_type"); + p.next_token = text(b, "next_token"); + if (const Json* f = b.find("filter"); f && f->is_object()) p.filter = *f; + Json wire = p.to_json(); + auto ar = std::make_shared(client_); + Plan pl{wire, "POST", bk::Calls::render(bk::Call::kRetrieve, {{"knowledgeBaseId", p.knowledge_base_id}}), true, nullptr}; + pl.call = [ar, p] { + bk::RetrieveResult r = ar->retrieve(p); + Json o = Json::object(); + o.set("results", r.results); + o.set("count", Json(static_cast(r.results.as_array().size()))); + if (!r.next_token.empty()) o.set("next_token", Json(r.next_token)); + if (!r.guardrail_action.empty()) o.set("guardrail_action", Json(r.guardrail_action)); + return o; + }; + return pl; + } + Plan models(const Json& b) const { + std::string action = text(b, "action"); + if (action.empty()) action = "list"; + auto api = std::make_shared(client_); + if (action == "list") { + bk::ModelFilter f; + f.provider = text(b, "provider"); + f.output_modality = text(b, "output_modality"); + f.inference_type = text(b, "inference_type"); + f.customization_type = text(b, "customization_type"); + Json req = Json::object(); + req.set("provider", Json(f.provider)); + return Plan{req, "GET", "/foundation-models", false, [api, f] { return list_json(api->list(f), "models"); }}; + } + if (action == "get") { + std::string id = text(b, "id", true); + if (!bk::Calls::valid_path_value(id)) bad("id has invalid characters"); + Json req = Json::object(); + req.set("id", Json(id)); + return Plan{req, "GET", bk::Calls::render(bk::Call::kGetFoundationModel, {{"modelIdentifier", id}}), false, [api, id] { + Json o = Json::object(); + o.set("model", api->get(id)); + return o; + }}; + } + bad("action must be list or get"); + } + Plan profiles(const Json& b) const { + std::string action = text(b, "action"); + if (action.empty()) action = "list"; + auto api = std::make_shared(client_); + if (action == "list") { + std::string type = text(b, "type"); + int max = number(b, "max_results").value_or(100); + if (max < 1 || max > bk::BedrockCaps::kMaxResults) bad("max_results must be 1..1000"); + Json req = Json::object(); + req.set("type", Json(type)); + return Plan{req, "GET", "/inference-profiles", false, [api, type, max] { return list_json(api->list_profiles(type, max), "profiles"); }}; + } + if (action == "get") { + std::string id = text(b, "id", true); + if (!bk::Calls::valid_path_value(id)) bad("id has invalid characters"); + Json req = Json::object(); + req.set("id", Json(id)); + return Plan{req, "GET", bk::Calls::render(bk::Call::kGetInferenceProfile, {{"inferenceProfileIdentifier", id}}), false, [api, id] { + Json o = Json::object(); + o.set("profile", api->get_profile(id)); + return o; + }}; + } + bad("action must be list or get"); + } + Plan guardrails(const Json& b) const { + std::string action = text(b, "action"); + if (action.empty()) action = "list"; + auto api = std::make_shared(client_); + if (action == "list") { + std::string id = text(b, "id"); + if (!id.empty() && !bk::Calls::valid_path_value(id)) bad("id has invalid characters"); + Json req = Json::object(); + req.set("id", Json(id)); + return Plan{req, "GET", "/guardrails", false, [api, id] { return list_json(api->list(id), "guardrails"); }}; + } + if (action == "get") { + std::string id = text(b, "id", true); + std::string version = text(b, "version"); + if (!bk::Calls::valid_path_value(id)) bad("id has invalid characters"); + Json req = Json::object(); + req.set("id", Json(id)); + return Plan{req, "GET", bk::Calls::render(bk::Call::kGetGuardrail, {{"guardrailIdentifier", id}}), false, [api, id, version] { + Json o = Json::object(); + o.set("guardrail", api->get(id, version)); + return o; + }}; + } + bad("action must be list or get"); + } + Plan batch(const Json& b) const { + std::string action = text(b, "action", true); + auto api = std::make_shared(client_); + if (action == "list") { + std::string status = text(b, "status"), name = text(b, "name_contains"); + Json req = Json::object(); + req.set("status", Json(status)); + return Plan{req, "GET", "/model-invocation-jobs", false, [api, status, name] { return list_json(api->list(status, name), "jobs"); }}; + } + if (action == "get" || action == "stop") { + std::string id = text(b, "id", true); + if (!bk::Calls::valid_path_value(id)) bad("id has invalid characters"); + Json req = Json::object(); + req.set("id", Json(id)); + if (action == "get") + return Plan{req, "GET", bk::Calls::render(bk::Call::kGetBatchJob, {{"jobIdentifier", id}}), false, [api, id] { + Json o = Json::object(); + o.set("job", api->get(id)); + return o; + }}; + return Plan{req, "POST", bk::Calls::render(bk::Call::kStopBatchJob, {{"jobIdentifier", id}}), true, [api, id] { + api->stop(id); + Json o = Json::object(); + o.set("stopping", Json(id)); + return o; + }}; + } + if (action == "create") { + bk::BatchJobParams p; + p.job_name = text(b, "job_name", true); + p.role_arn = text(b, "role_arn", true); + p.model_id = text(b, "model", true); + p.input_s3_uri = text(b, "input_s3_uri", true); + p.output_s3_uri = text(b, "output_s3_uri", true); + p.invocation_type = text(b, "invocation_type"); + p.timeout_hours = number(b, "timeout_hours").value_or(0); + Json wire = p.to_json(); + return Plan{wire, "POST", "/model-invocation-job", true, [api, p] { + Json o = Json::object(); + o.set("job_arn", Json(api->create(p))); + return o; + }}; + } + bad("action must be list, get, create or stop"); + } + // Local: signs with AWS's documentation credentials, never a real secret. + Plan sign(const Json& b) const { + bk::AwsCredentials creds{"AKIDEXAMPLE", "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY", ""}; + std::string region = text(b, "region"); + if (region.empty()) region = "us-east-1"; + if (!bk::Endpoints::valid_region(region)) bad("bad region name"); + std::string service = text(b, "service"); + if (service.empty()) service = "bedrock"; + std::string method = text(b, "method"); + if (method.empty()) method = "GET"; + if (method != "GET" && method != "POST" && method != "PUT" && method != "DELETE" && method != "PATCH") bad("unsupported method"); + bk::SigV4Request r; + r.method = method; + r.path = text(b, "path"); + if (r.path.empty()) r.path = "/"; + if (r.path[0] != '/') bad("path must start with /"); + r.query = text(b, "query"); + r.body = text(b, "body"); + std::string host = text(b, "host"); + r.headers.emplace_back("Host", host.empty() ? "example.amazonaws.com" : host); + std::string date = text(b, "date"); + if (date.empty()) date = "20150830T123600Z"; + Json req = Json::object(); + req.set("method", Json(method)); + req.set("path", Json(r.path)); + Plan pl{req, "LOCAL", "", false, nullptr}; + pl.call = [creds, region, service, r, date] { + bk::SigV4Result s = bk::SigV4Signer(creds, region, service).sign(r, date); + Json o = Json::object(); + o.set("canonical_request", Json(s.canonical_request)); + o.set("string_to_sign", Json(s.string_to_sign)); + o.set("signed_headers", Json(s.signed_headers)); + o.set("signature", Json(s.signature)); + o.set("authorization", Json(s.authorization)); + o.set("note", Json("signed with AWS documentation credentials, not yours")); + return o; + }; + return pl; + } + + private: + [[noreturn]] static void map(const llm::ApiError& e) { + if (e.http_status() == 503 && e.type() == "NOT_CONFIGURED") throw fw::HttpError(503, "not_configured", e.message()); + if (e.type() == "INVALID_ARGUMENT" && e.http_status() == 400 && e.request_id().empty()) + throw fw::HttpError(422, "validation_error", e.message()); + throw fw::HttpError(502, "bedrock_error", std::to_string(e.http_status()) + " " + e.type() + ": " + e.message()); + } + std::shared_ptr client_; +}; + +} // namespace + void register_ai_bedrock_demo(fw::Router& router, Services& services) { - (void)router; - (void)services; + auto routes = std::make_shared(bk::BedrockClient::from_env()); + fw::Environment* env = services.templates.get(); + services.templates->add_template("ai_bedrock.html", kPage); + services.add_feature("ai-bedrock", "/ai/bedrock"); + + router.get("/ai/bedrock", "ai-bedrock-page", [env](fw::Request req) { + (void)req; + fw::TemplateCtx ctx; + ctx["title"] = std::string("Amazon Bedrock"); + return fw::Response(200, env->render("ai_bedrock.html", ctx), "text/html; charset=utf-8"); + }); + + router.get("/ai/bedrock/status", "ai-bedrock-status", [routes](fw::Request req) { + (void)req; + const bk::Credentials& c = routes->client().credentials(); + Json o = Json::object(); + o.set("configured", Json(routes->client().configured())); + o.set("auth_mode", Json(c.mode_name())); + o.set("region", Json(c.region)); + Json env = Json::array(); + for (const char* n : {bk::Credentials::kEnvBearer, bk::Credentials::kEnvAccessKey, bk::Credentials::kEnvSecretKey, + bk::Credentials::kEnvSessionToken, bk::Credentials::kEnvRegion}) + env.push(Json(n)); + o.set("credential_env", env); + Json hosts = Json::object(); + hosts.set("runtime", Json(bk::Endpoints::host(bk::AwsService::kRuntime, c.region))); + hosts.set("control", Json(bk::Endpoints::host(bk::AwsService::kControl, c.region))); + hosts.set("agent_runtime", Json(bk::Endpoints::host(bk::AwsService::kAgentRuntime, c.region))); + o.set("hosts", hosts); + o.set("default_model", Json(bk::BedrockChatModel::kDefaultModel)); + o.set("calls", Json(static_cast(bk::Calls::count()))); + o.set("max_invoke_body_bytes", Json(static_cast(bk::BedrockCaps::kMaxInvokeBodyBytes))); + o.set("max_pages", Json(static_cast(bk::BedrockCaps::kMaxPages))); + return json_out(o); + }); + + router.get("/ai/bedrock/calls", "ai-bedrock-calls", [](fw::Request req) { + (void)req; + Json arr = Json::array(); + for (std::size_t i = 0; i < bk::Calls::count(); ++i) { + const bk::CallSpec& s = bk::Calls::at(i); + Json row = Json::object(); + row.set("operation", Json(s.op)); + row.set("http", Json(s.http_method)); + row.set("path", Json(s.uri)); + row.set("host", Json(bk::Endpoints::prefix(s.service))); + row.set("bearer_ok", Json(s.bearer_ok)); + row.set("event_stream", Json(s.event_stream)); + arr.push(row); + } + Json o = Json::object(); + o.set("calls", arr); + return json_out(o); + }); + + using Member = BedrockRoutes::Planner; + auto add = [&](const char* path, const char* name, Member planner) { + router.post(path, name, [routes, planner](fw::Request req) { return routes->run(req, planner); }); + }; + BedrockRoutes* r = routes.get(); + auto bind = [r](Plan (BedrockRoutes::*m)(const Json&) const) { + return Member([r, m](const Json& b) { return (r->*m)(b); }); + }; + add("/ai/bedrock/converse", "ai-bedrock-converse", bind(&BedrockRoutes::converse)); + add("/ai/bedrock/converse-stream", "ai-bedrock-converse-stream", bind(&BedrockRoutes::converse_stream)); + add("/ai/bedrock/invoke", "ai-bedrock-invoke", bind(&BedrockRoutes::invoke)); + add("/ai/bedrock/invoke-stream", "ai-bedrock-invoke-stream", bind(&BedrockRoutes::invoke_stream)); + add("/ai/bedrock/count-tokens", "ai-bedrock-count-tokens", bind(&BedrockRoutes::count_tokens)); + add("/ai/bedrock/guardrail", "ai-bedrock-guardrail", bind(&BedrockRoutes::guardrail)); + add("/ai/bedrock/agent", "ai-bedrock-agent", bind(&BedrockRoutes::agent)); + add("/ai/bedrock/retrieve", "ai-bedrock-retrieve", bind(&BedrockRoutes::retrieve)); + add("/ai/bedrock/models", "ai-bedrock-models", bind(&BedrockRoutes::models)); + add("/ai/bedrock/profiles", "ai-bedrock-profiles", bind(&BedrockRoutes::profiles)); + add("/ai/bedrock/guardrails", "ai-bedrock-guardrails", bind(&BedrockRoutes::guardrails)); + add("/ai/bedrock/batch", "ai-bedrock-batch", bind(&BedrockRoutes::batch)); + add("/ai/bedrock/sign", "ai-bedrock-sign", bind(&BedrockRoutes::sign)); } } // namespace molo::example::app diff --git a/example/assets/ai_bedrock.js b/example/assets/ai_bedrock.js new file mode 100644 index 0000000..cc19cff --- /dev/null +++ b/example/assets/ai_bedrock.js @@ -0,0 +1,59 @@ +// Bedrock page: thin client over /ai/bedrock/* (all limits server side). +(function () { + "use strict"; + var $ = function (id) { return document.getElementById(id); }; + var out = $("bk-out"), err = $("bk-error"); + var templates = { + "converse": { prompt: "Hello!", system: "Be brief.", max_tokens: 256, temperature: 0.5 }, + "converse-stream": { prompt: "Count to five.", max_tokens: 128 }, + "invoke": { model: "amazon.titan-embed-text-v2:0", body: { inputText: "hello" } }, + "invoke-stream": { model: "anthropic.claude-3-haiku-20240307-v1:0", body: { anthropic_version: "bedrock-2023-05-31", max_tokens: 64, messages: [{ role: "user", content: "Hi" }] } }, + "count-tokens": { prompt: "How many tokens is this?" }, + "guardrail": { guardrail_id: "", version: "DRAFT", source: "INPUT", text: "my card number is 4111 1111 1111 1111" }, + "agent": { agent_id: "", alias_id: "", session_id: "demo-session-1", prompt: "Hello" }, + "retrieve": { knowledge_base_id: "", query: "What is AWS?", number_of_results: 5 }, + "models": { action: "list", provider: "Anthropic", output_modality: "TEXT" }, + "profiles": { action: "list", type: "SYSTEM_DEFINED" }, + "guardrails": { action: "list" }, + "batch": { action: "list" }, + "sign": { method: "POST", path: "/model/anthropic.claude-3-sonnet-20240229-v1%3A0/converse", body: "{}", host: "bedrock-runtime.us-east-1.amazonaws.com" } + }; + var sel = $("bk-route"); + Object.keys(templates).forEach(function (k) { + var o = document.createElement("option"); + o.value = k; o.textContent = k; + sel.appendChild(o); + }); + function fill() { $("bk-body").value = JSON.stringify(templates[sel.value], null, 1); } + sel.onchange = fill; + fill(); + + function send(confirm) { + var body; + try { body = JSON.parse($("bk-body").value || "{}"); } + catch (e) { err.textContent = "Body is not valid JSON"; return; } + if (confirm) body.confirm = true; + err.textContent = ""; + out.textContent = "..."; + fetch("/ai/bedrock/" + sel.value, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(body) + }).then(function (res) { + return res.text().then(function (t) { + var j; + try { j = JSON.parse(t); } catch (e) { j = { message: t }; } + if (!res.ok) { err.textContent = j.message || ("HTTP " + res.status); out.textContent = ""; } + else { out.textContent = JSON.stringify(j, null, 2); } + }); + }).catch(function (e) { err.textContent = String(e); out.textContent = ""; }); + } + $("bk-send").onclick = function () { send(false); }; + $("bk-confirm").onclick = function () { send(true); }; + + fetch("/ai/bedrock/status").then(function (r) { return r.json(); }).then(function (s) { + $("bk-config").textContent = s.configured + ? "Credential found (" + s.auth_mode + ", " + s.region + "). Calls are real; paid ones need confirm." + : "No AWS_BEARER_TOKEN_BEDROCK or AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY: calls answer 503 until one is set; dry runs and /sign still work."; + }); +})(); diff --git a/src/vendors/bedrock/SPEC.md b/src/vendors/bedrock/SPEC.md new file mode 100644 index 0000000..4803462 --- /dev/null +++ b/src/vendors/bedrock/SPEC.md @@ -0,0 +1,230 @@ +# Amazon Bedrock (src/vendors/bedrock) + +Namespace `molo::bedrock`. Amazon Bedrock over HTTPS with our own AWS Signature +V4 signer. Plan: `plans/18-breadth.md` (vendor rules of `plans/17`). Status +2026-10-25: implemented and tested locally; no AWS credential exists in this +environment, so only unauthenticated probes were verified live. + +## Scope + +Wrapped (18 operations, three hosts, one call table): + +- bedrock-runtime (`bedrock-runtime..amazonaws.com`): Converse, + ConverseStream, InvokeModel, InvokeModelWithResponseStream, CountTokens, + ApplyGuardrail. +- bedrock-agent-runtime (`bedrock-agent-runtime..amazonaws.com`): + InvokeAgent (event stream), Retrieve (knowledge bases). +- bedrock control plane (`bedrock..amazonaws.com`): ListFoundationModels, + GetFoundationModel, ListInferenceProfiles, GetInferenceProfile, ListGuardrails, + GetGuardrail, batch inference (CreateModelInvocationJob, GetModelInvocationJob, + ListModelInvocationJobs, StopModelInvocationJob). +- `SigV4Signer` (own implementation over `security::crypto` Sha256 + Hmac), + event-stream decoder with CRC checks, `BedrockChatModel : llm::ChatModel`. + +Not wrapped, said plainly (136 operations, each with its reason in +`tools/check_spec.py` `EXCLUDED`): async invoke (Nova Reel), bidirectional +streaming (needs h2), `InvokeGuardrailChecks`, sessions / flows / agent memory / +RetrieveAndGenerate / Rerank / inline agents of the agent runtime, and the +management plane (customization, import, evaluation, provisioned throughput, +marketplace, automated reasoning, guardrail and profile writes, logging, tags). +Bedrock Agents build-time APIs (`bedrock-agent`) are a separate service and are +not touched. + +## Module map (one concern per file) + +| File | Class | Concern | +|---|---|---| +| `caps.h` | `BedrockCaps` | static ceilings | +| `crc32.h/.cpp` | `Crc32` | IEEE CRC-32 for the event-stream framing | +| `sigv4.h/.cpp` | `SigV4Signer`, `AwsCredentials`, `SigV4Request/Result` | canonical request, string to sign, signing key, Authorization header | +| `credentials.h/.cpp` | `Credentials`, `Endpoints`, `AuthMode`, `AwsService` | env credentials, region, the three hosts | +| `calls.h/.cpp` | `Calls`, `CallSpec`, `SchemaFields` | the ONE call table (18 rows) and schema table (29 rows); path rendering | +| `event_stream.h/.cpp` | `EventStreamDecoder`, `EventMessage` | `application/vnd.amazon.eventstream` framing, both CRCs, exceptions | +| `client.h/.cpp` | `BedrockClient`, `BedrockService`, `CallInput`, `WirePreview` | builds, signs and sends a call from the table; error mapping | +| `converse.h/.cpp` | `Blocks`, `ConverseRequest`, `ConverseResult`, `ConverseStreamAccumulator`, `StreamDelta` | Converse bodies, results, stream folding | +| `runtime.h/.cpp` | `Runtime` | Converse, ConverseStream, InvokeModel (+ stream), CountTokens, ApplyGuardrail | +| `control.h/.cpp` | `Models`, `Guardrails`, `BatchJobs`, `BatchJobParams`, `ListResult` | control plane | +| `agent.h/.cpp` | `AgentRuntime`, `AgentInvokeParams`, `RetrieveParams` | InvokeAgent, Retrieve (+ capped paging) | +| `chat_model.h/.cpp` | `BedrockChatModel` | neutral `llm::ChatModel` over Converse | +| `tools/check_spec.py` | | checks the tables against the AWS service models | + +`llm::ApiClient` is not used: it sends a fixed bearer or header key, while +SigV4 signs the final request (method, path, query, headers, body hash) per +call. `BedrockClient` does that job and keeps the foundation contracts: no +credential means `llm::NotConfiguredError` (503 `NOT_CONFIGURED`) before any +traffic, every failure is an `llm::ApiError`, the secret never appears in an +URL, an error or a preview. `BedrockService` replaces `llm::Service` for the +same reason (its constructor takes an `ApiClient`); `valid_id` is the same +rule widened to ARNs. + +## Credentials + +Read through `llm::Config` (environment, then `.env.json.local`, then +`.env.json`): + +| Variable | Use | +|---|---| +| `AWS_BEARER_TOKEN_BEDROCK` | Bedrock API key, sent as `Authorization: Bearer`; nothing is signed | +| `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` | SigV4 | +| `AWS_SESSION_TOKEN` | optional, signed and sent as `x-amz-security-token` | +| `AWS_REGION`, then `AWS_DEFAULT_REGION` | region; invalid or unset gives `us-east-1` | +| `BEDROCK_LIVE_MODEL` | live test only, model id of the one paid call | + +When both an API key and SigV4 keys are set the API key is used for the calls +whose service model lists `httpBearerAuth` (bedrock, bedrock-runtime) and the +SigV4 keys for the agent runtime host, whose model lists SigV4 only. With only +an API key, an agent runtime call is refused before any traffic (503 +`NOT_CONFIGURED`, message names the variables). Console: IAM access keys are +created at https://console.aws.amazon.com/iam/home#/security_credentials +(unverified: page not fetched); Bedrock API keys at the Bedrock console, "API +keys" (unverified, see https://docs.aws.amazon.com/bedrock/latest/userguide/api-keys.html). + +## Static caps (caps.h) + +| Knob | Value | +|---|---| +| InvokeModel body | 25,000,000 bytes (spec blob max) | +| Response / stream bytes | 64 MiB / 64 MiB | +| Event message / headers | 16 MiB / 128 KiB | +| Stream events | 200,000 | +| Model id | 2048 characters (spec) | +| Messages / content blocks per message / tools | 4096 / 512 / 128 | +| Stop sequences | 2500 (spec) | +| Retrieve results / query | 100 / 20,000 characters (spec) | +| Agent input | 25,000,000 characters (spec) | +| List `maxResults` | 1..1000 (spec); walks stop at 20 pages, 5000 items, and say `truncated` | +| Batch timeout | 24..168 hours (spec); job name 63 characters | +| Signed headers / query params / path | 64 / 64 / 4096 | + +## Contracts + +- Every path, query parameter and header goes through the call table: + `BedrockClient` refuses a query or header name the row does not declare, a + path value outside `[A-Za-z0-9._:/-]`, `..`, an empty or over-long value. + Path values are percent-encoded as one segment (`:` becomes `%3A`, `/` + becomes `%2F`), so ARNs work. +- Canonical URI: the path as sent is encoded again (double encoding), the rule + for every non-S3 service; the suite and the Bedrock model-id test cover it. +- Validation runs before the credential check: bad input is 400 + `INVALID_ARGUMENT`, never 503. +- Event streams: both CRC32s are checked before a message is delivered; a + message over 16 MiB, a header block over 128 KiB, a prelude that fails its + CRC, or a stream that ends inside a message throws. `:message-type` + `exception` / `error` becomes `ApiError` (type = exception name, status from + the service model: throttling 429, validation 400, modelStreamError 424, ...). + The transport hides the HTTP status of a stream, so a JSON error document in + place of a stream is thrown as status 502, type `STREAM_HTTP_ERROR`, with the + server's message. +- Errors: type from `x-amzn-ErrorType` (text after `:` dropped, `ns#` prefix + dropped), else `__type` / `code`; message from `message` / `Message`; request + id from `x-amzn-RequestId`. +- Lists never truncate silently: `ListResult.truncated`, `pages`, `next_token`. +- Create and stop (batch) are explicit methods; `BatchJobs::create` costs money + once the job runs. The demo makes them dry runs. +- `BedrockChatModel` mapping decisions are listed in `chat_model.h`. + +## Sources + +1. AWS service models (botocore `botocore/data///service-2.json`, + `develop` branch, fetched 2026-10-25): `bedrock` 2023-04-20 (108 + operations), `bedrock-runtime` 2023-09-30 (11), `bedrock-agent-runtime` + 2023-07-26 (35). Primary source for paths, members, enums, limits, error + status codes, auth lists, event-stream unions. +2. AWS Signature V4 test suite (`aws4_testsuite`, 24 cases, files `.req` + `.creq` `.sts` `.authz`), via the copy in the botocore repository + (`tests/unit/auth/aws4_testsuite`). Credentials of the suite are AWS's + published example ones. +3. AWS API reference pages fetched 2026-10-25: `API_runtime_Converse.html`, + `API_runtime_ConverseStream.html`, `API_agent-runtime_Retrieve.html` + (sample requests and responses used as fixtures). +4. AWS documentation "Examples of how to derive a signing key for Signature + Version 4" (key vector for `iam`, 20150830, us-east-1; recalled, matched by + our computation, not re-fetched). +5. Live unauthenticated probes (below). + +Cross-checks: service models and reference pages agree on paths, members and +the Converse sample bodies; the suite and the key vector agree on the signing +chain. + +## Verified against spec + +`python3 -I src/vendors/bedrock/tools/check_spec.py` on 2026-10-25: 18 rows, +29 schema rows, 154 operations in the three models, 136 excluded with a reason, +0 problems. It checks, per row: operation exists, HTTP method, request URI, +URI placeholders equal the declared path params, every path / query / header / +body member exists with the right location and location name, every required +input member is covered, the response schema equals the operation output (the +event-stream union for stream rows), `bearer_ok` equals `httpBearerAuth` in the +model's auth list, `event_stream` equals the model, every member we read exists +(dotted paths descend); and fails on any operation neither in the table nor in +`EXCLUDED`. A mutated copy of `calls.cpp` was run to prove it fails. + +SigV4: all 24 cases of the AWS test suite (96 files under +`tests/src/vendors/bedrock/sigv4_suite/`) give the suite's canonical request, +string to sign and Authorization header; the documented `iam` signing key +`c4afb1cc...a4b9` matches. + +## Verified live (2026-10-25, no account) + +`MOLO_LIVE_NET=1 ./build/ai_test` (tests `bedrock_live.*`): + +- Control plane, `GET /foundation-models` with an invalid API key: 403 + `AccessDeniedException`, "Invalid API Key format: Must start with pre-defined + prefix" (the host resolves, TLS works, the Bearer path is routed). +- With the documentation SigV4 key (`AKIDEXAMPLE`) on all three hosts + (`GET /foundation-models`, `POST /model//converse`, + `POST /knowledgebases//retrieve`): 403 `UnrecognizedClientException`, + "The security token included in the request is invalid." This is the answer + to a well-formed, correctly routed request with an unknown key, and shows the + error type header and message shape the client parses. +- `POST /model//converse-stream` with the same key: a JSON error document + instead of a stream, thrown as `STREAM_HTTP_ERROR`. + +## Unverified + +- No successful response was seen from any host. Converse / Retrieve success + bodies are the AWS reference samples (Converse) or shaped by the service + model (everything else); tests label which. +- The event-stream byte layout (prelude, CRCs, header types) was written from + the public description of the format, not compared with a recorded real + Bedrock stream. CRC-32 itself is checked with the standard check value + (`CBF43926`). The `:event-type` names are the union member names of the + service model. +- A real SigV4 signature accepted by AWS: the signer matches the published + suite, and the live probes got past routing, but only a key can prove an + accepted signature (the signed header set `content-type;host;x-amz-date` and + the double-encoded model path are the likely failure points). +- Whether Bedrock API keys work on `ListGuardrails`, batch and the other + control plane rows: the service model lists `httpBearerAuth` for the whole + `bedrock` model, AWS's documentation of key limits was not fetched. +- The agent runtime model lists no bearer auth; whether the host accepts API + keys anyway is unknown (the client refuses them by the model). +- Default model `us.amazon.nova-micro-v1:0` (used by the demo and + `BedrockChatModel`): model access and the inference profile must exist in + the account and region; not verified. +- The JSON-schema output (`outputConfig.textFormat`, schema as a string) and + `serviceTier` / `performanceConfig` members come from the model, not from a + call. +- `+` in a query string is treated as data when signing (`%2B`); our own + requests encode it, but a hand-written query with `+` meaning space would + sign differently from AWS's reading. +- Region endpoints other than `..amazonaws.com[.cn]` (FIPS, + VPC endpoints, GovCloud variants) are not modelled. + +## Gaps + +- `src/llm` has no signing hook: `ApiClient` cannot sign per request, so this + package carries `BedrockClient` and `BedrockService` (foundation gap; a + `RequestSigner` seam in `ApiClient::build` would let vendors share it). +- The transport exposes no HTTP status or headers for streamed responses, so a + failed stream call reports 502 `STREAM_HTTP_ERROR` instead of the real status + and error type (`src/clients` `Transport::send_stream`). +- `InvokeModelWithBidirectionalStream` needs HTTP/2 framing the client does not + have; async invoke, sessions, flows, RetrieveAndGenerate, Rerank and inline + agents are listed in `EXCLUDED` and could be added row by row. +- Responses are mostly `Json` accessors, not typed classes (models, profiles, + guardrails, jobs, retrieval results). +- No credential provider chain (shared credentials file, IMDS, SSO, AssumeRole): + environment only. +- Requests are signed once; a clock skew over 5 minutes fails with the + server's own error, there is no resign-and-retry. diff --git a/src/vendors/bedrock/agent.cpp b/src/vendors/bedrock/agent.cpp new file mode 100644 index 0000000..5f926ab --- /dev/null +++ b/src/vendors/bedrock/agent.cpp @@ -0,0 +1,166 @@ +#include "vendors/bedrock/agent.h" + +#include "security/crypto/Base64.h" +#include "vendors/bedrock/caps.h" + +namespace molo::bedrock { + +namespace { + +constexpr std::size_t kMaxTraces = 200; + +[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("bedrock", why); } + +bool alnum(const std::string& s, std::size_t min, std::size_t max, const char* extra = "") { + if (s.size() < min || s.size() > max) return false; + for (char c : s) { + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) continue; + if (*extra && std::string(extra).find(c) != std::string::npos) continue; + return false; + } + return true; +} + +} // namespace + +void AgentInvokeParams::validate() const { + if (!alnum(agent_id, 1, 10)) bad("agentId must be 1..10 letters or digits"); + if (!alnum(alias_id, 1, 10)) bad("agentAliasId must be 1..10 letters or digits"); + if (!alnum(session_id, 2, 100, "._:-")) bad("sessionId must be 2..100 of [0-9a-zA-Z._:-]"); + if (input_text.empty() && session_state.is_object() && session_state.as_object().empty() && !end_session) + bad("inputText is required"); + if (input_text.size() > BedrockCaps::kMaxAgentInputChars) bad("inputText too long"); + if (!memory_id.empty() && !alnum(memory_id, 2, 100, "._:-")) bad("memoryId must be 2..100 of [0-9a-zA-Z._:-]"); + if (!session_state.is_object()) bad("sessionState must be an object"); +} + +Json AgentInvokeParams::to_json() const { + validate(); + Json b = Json::object(); + if (!input_text.empty()) b.set("inputText", Json(input_text)); + if (enable_trace) b.set("enableTrace", Json(true)); + if (end_session) b.set("endSession", Json(true)); + if (!memory_id.empty()) b.set("memoryId", Json(memory_id)); + if (!session_state.as_object().empty()) b.set("sessionState", session_state); + if (stream_final_response) { + Json s = Json::object(); + s.set("streamFinalResponse", Json(true)); + b.set("streamingConfigurations", s); + } + return b; +} + +void RetrieveParams::validate() const { + if (!alnum(knowledge_base_id, 10, 10)) bad("knowledgeBaseId must be 10 letters or digits"); + if (query.empty() || query.size() > BedrockCaps::kMaxRetrieveQueryChars) bad("query must be 1..20000 characters"); + if (number_of_results != 0 && (number_of_results < 1 || number_of_results > BedrockCaps::kMaxRetrieveResults)) + bad("numberOfResults must be 1..100"); + if (!search_type.empty() && search_type != "HYBRID" && search_type != "SEMANTIC") bad("overrideSearchType must be HYBRID or SEMANTIC"); + if (!filter.is_null() && !filter.is_object()) bad("filter must be an object"); + if (next_token.size() > 2048) bad("nextToken too long"); + if (!guardrail_id.empty() && guardrail_version.empty()) bad("guardrail version is required with a guardrail"); +} + +Json RetrieveParams::to_json() const { + validate(); + Json b = Json::object(); + Json q = Json::object(); + q.set("text", Json(query)); + b.set("retrievalQuery", q); + Json vs = Json::object(); + if (number_of_results != 0) vs.set("numberOfResults", Json(number_of_results)); + if (!search_type.empty()) vs.set("overrideSearchType", Json(search_type)); + if (filter.is_object()) vs.set("filter", filter); + if (!vs.as_object().empty()) { + Json rc = Json::object(); + rc.set("vectorSearchConfiguration", vs); + b.set("retrievalConfiguration", rc); + } + if (!guardrail_id.empty()) { + Json g = Json::object(); + g.set("guardrailId", Json(guardrail_id)); + g.set("guardrailVersion", Json(guardrail_version)); + b.set("guardrailConfiguration", g); + } + if (!next_token.empty()) b.set("nextToken", Json(next_token)); + return b; +} + +AgentResult AgentRuntime::invoke_agent(const AgentInvokeParams& params, + const std::function& on_text) { + Json body = params.to_json(); + CallInput in; + in.call = Call::kInvokeAgent; + in.path["agentId"] = params.agent_id; + in.path["agentAliasId"] = params.alias_id; + in.path["sessionId"] = params.session_id; + in.json = &body; + AgentResult result; + result.session_id = params.session_id; + EventStreamDecoder decoder(BedrockClient::vendor(), [&](const EventMessage& m) { + EventStreamDecoder::raise_if_failure(BedrockClient::vendor(), m); + if (!m.is_event()) return true; + Json p = m.payload.empty() ? Json::object() : Json::parse(m.payload); + std::string type = m.event_type(); + if (type == "chunk") { + const Json* b = p.find("bytes"); + std::string decoded; + if (!b || !b->is_string() || !security::crypto::Base64::decode(b->as_string(), decoded)) + throw llm::ApiError(BedrockClient::vendor(), 502, "BAD_RESPONSE", "chunk without valid base64 bytes"); + ++result.chunks; + result.text += decoded; + if (const Json* a = p.find("attribution"); a && result.attributions.as_array().size() < kMaxTraces) + result.attributions.push(*a); + if (on_text && !on_text(decoded)) return false; + } else if (type == "trace") { + if (result.traces.as_array().size() < kMaxTraces) result.traces.push(p); + else result.traces_truncated = true; + } else if (type == "returnControl") { + result.return_control = p; + } + return true; + }); + client_->stream(in, [&](const std::string& chunk) { return decoder.feed(chunk); }); + decoder.finish(); + return result; +} + +RetrieveResult AgentRuntime::retrieve(const RetrieveParams& params) { + Json body = params.to_json(); + CallInput in; + in.call = Call::kRetrieve; + in.path["knowledgeBaseId"] = params.knowledge_base_id; + in.json = &body; + Json r = client_->json(in); + RetrieveResult out; + out.pages = 1; + if (const Json* a = r.find("retrievalResults"); a && a->is_array()) out.results = *a; + out.next_token = string_at(r, "nextToken"); + out.guardrail_action = string_at(r, "guardrailAction"); + return out; +} + +RetrieveResult AgentRuntime::retrieve_all(RetrieveParams params, std::size_t max_pages) { + if (max_pages < 1 || max_pages > BedrockCaps::kMaxPages) bad("max_pages must be 1..20"); + RetrieveResult all; + for (std::size_t page = 0; page < max_pages; ++page) { + RetrieveResult r = retrieve(params); + ++all.pages; + for (const Json& x : r.results.as_array()) { + if (all.results.as_array().size() >= BedrockCaps::kMaxItemsReturned) { + all.truncated = true; + all.next_token = r.next_token; + return all; + } + all.results.push(x); + } + all.guardrail_action = r.guardrail_action; + all.next_token = r.next_token; + if (r.next_token.empty()) return all; + params.next_token = r.next_token; + } + all.truncated = !all.next_token.empty(); + return all; +} + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/agent.h b/src/vendors/bedrock/agent.h new file mode 100644 index 0000000..ba3fffa --- /dev/null +++ b/src/vendors/bedrock/agent.h @@ -0,0 +1,70 @@ +#pragma once + +// bedrock-agent-runtime (host bedrock-agent-runtime..amazonaws.com): +// InvokeAgent (event stream) and knowledge base Retrieve. This host takes SigV4 +// only; Bedrock API keys are refused before any traffic. + +#include +#include + +#include "vendors/bedrock/client.h" +#include "vendors/bedrock/control.h" + +namespace molo::bedrock { + +struct AgentInvokeParams { + std::string agent_id; // [0-9a-zA-Z]{1,10} + std::string alias_id; // [0-9a-zA-Z]{1,10} + std::string session_id; // [0-9a-zA-Z._:-]{2,100} + std::string input_text; // up to 25,000,000 characters + bool enable_trace = false; + bool end_session = false; + std::string memory_id; // optional, same pattern as the session id + Json session_state = Json::object(); // sessionState passthrough, optional + bool stream_final_response = false; // streamingConfigurations.streamFinalResponse + void validate() const; + Json to_json() const; +}; + +struct AgentResult { + std::string text; + std::string session_id; + std::size_t chunks = 0; + Json traces = Json::array(); // trace events, capped + Json return_control = Json(); // returnControl payload when the agent asks the caller to act + Json attributions = Json::array(); + bool traces_truncated = false; +}; + +struct RetrieveParams { + std::string knowledge_base_id; // [0-9a-zA-Z]{10} + std::string query; // 1..20000 characters + int number_of_results = 0; // 0: service default, else 1..100 + std::string search_type; // "" | HYBRID | SEMANTIC + Json filter; // optional RetrievalFilter union, passthrough + std::string next_token; + std::string guardrail_id, guardrail_version; + void validate() const; + Json to_json() const; +}; + +struct RetrieveResult { + Json results = Json::array(); // retrievalResults + std::string next_token; + std::string guardrail_action; + bool truncated = false; // only set by retrieve_all + std::size_t pages = 0; +}; + +class AgentRuntime : public BedrockService { + public: + using BedrockService::BedrockService; + // on_text gets each decoded text chunk; return false to stop. Costs money. + AgentResult invoke_agent(const AgentInvokeParams& params, + const std::function& on_text = nullptr); + RetrieveResult retrieve(const RetrieveParams& params); + // Follows nextToken up to `max_pages` (<= BedrockCaps::kMaxPages) pages. + RetrieveResult retrieve_all(RetrieveParams params, std::size_t max_pages); +}; + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/calls.cpp b/src/vendors/bedrock/calls.cpp new file mode 100644 index 0000000..cf84a85 --- /dev/null +++ b/src/vendors/bedrock/calls.cpp @@ -0,0 +1,122 @@ +#include "vendors/bedrock/calls.h" + +#include "clients/url.h" +#include "llm/error.h" +#include "vendors/bedrock/caps.h" + +namespace molo::bedrock { + +namespace { + +constexpr AwsService R = AwsService::kRuntime; +constexpr AwsService C = AwsService::kControl; +constexpr AwsService A = AwsService::kAgentRuntime; + +// One row per line: the checker reads this file with a regex. Quoted strings +// in order: op, http method, uri, path params, query params, header params, +// body fields, response schema. +const CallSpec kCalls[] = { + {Call::kConverse, R, "Converse", "POST", "/model/{modelId}/converse", "modelId", "", "", "messages,system,inferenceConfig,toolConfig,guardrailConfig,additionalModelRequestFields,promptVariables,additionalModelResponseFieldPaths,requestMetadata,performanceConfig,serviceTier,outputConfig", "ConverseResponse", true, false, true}, + {Call::kConverseStream, R, "ConverseStream", "POST", "/model/{modelId}/converse-stream", "modelId", "", "", "messages,system,inferenceConfig,toolConfig,guardrailConfig,additionalModelRequestFields,promptVariables,additionalModelResponseFieldPaths,requestMetadata,performanceConfig,serviceTier,outputConfig", "ConverseStreamOutput", true, true, true}, + {Call::kInvokeModel, R, "InvokeModel", "POST", "/model/{modelId}/invoke", "modelId", "", "Content-Type,Accept,X-Amzn-Bedrock-Trace,X-Amzn-Bedrock-GuardrailIdentifier,X-Amzn-Bedrock-GuardrailVersion,X-Amzn-Bedrock-PerformanceConfig-Latency,X-Amzn-Bedrock-Service-Tier", "body", "InvokeModelResponse", true, false, true}, + {Call::kInvokeModelStream, R, "InvokeModelWithResponseStream", "POST", "/model/{modelId}/invoke-with-response-stream", "modelId", "", "Content-Type,X-Amzn-Bedrock-Accept,X-Amzn-Bedrock-Trace,X-Amzn-Bedrock-GuardrailIdentifier,X-Amzn-Bedrock-GuardrailVersion,X-Amzn-Bedrock-PerformanceConfig-Latency,X-Amzn-Bedrock-Service-Tier", "body", "ResponseStream", true, true, true}, + {Call::kCountTokens, R, "CountTokens", "POST", "/model/{modelId}/count-tokens", "modelId", "", "", "input", "CountTokensResponse", true, false, false}, + {Call::kApplyGuardrail, R, "ApplyGuardrail", "POST", "/guardrail/{guardrailIdentifier}/version/{guardrailVersion}/apply", "guardrailIdentifier,guardrailVersion", "", "", "source,content,outputScope", "ApplyGuardrailResponse", true, false, true}, + {Call::kInvokeAgent, A, "InvokeAgent", "POST", "/agents/{agentId}/agentAliases/{agentAliasId}/sessions/{sessionId}/text", "agentId,agentAliasId,sessionId", "", "", "inputText,enableTrace,endSession,memoryId,sessionState,streamingConfigurations", "ResponseStream", false, true, true}, + {Call::kRetrieve, A, "Retrieve", "POST", "/knowledgebases/{knowledgeBaseId}/retrieve", "knowledgeBaseId", "", "", "retrievalQuery,retrievalConfiguration,guardrailConfiguration,nextToken", "RetrieveResponse", false, false, false}, + {Call::kListFoundationModels, C, "ListFoundationModels", "GET", "/foundation-models", "", "byProvider,byCustomizationType,byOutputModality,byInferenceType", "", "", "ListFoundationModelsResponse", true, false, false}, + {Call::kGetFoundationModel, C, "GetFoundationModel", "GET", "/foundation-models/{modelIdentifier}", "modelIdentifier", "", "", "", "GetFoundationModelResponse", true, false, false}, + {Call::kListInferenceProfiles, C, "ListInferenceProfiles", "GET", "/inference-profiles", "", "maxResults,nextToken,type", "", "", "ListInferenceProfilesResponse", true, false, false}, + {Call::kGetInferenceProfile, C, "GetInferenceProfile", "GET", "/inference-profiles/{inferenceProfileIdentifier}", "inferenceProfileIdentifier", "", "", "", "GetInferenceProfileResponse", true, false, false}, + {Call::kListGuardrails, C, "ListGuardrails", "GET", "/guardrails", "", "guardrailIdentifier,maxResults,nextToken", "", "", "ListGuardrailsResponse", true, false, false}, + {Call::kGetGuardrail, C, "GetGuardrail", "GET", "/guardrails/{guardrailIdentifier}", "guardrailIdentifier", "guardrailVersion", "", "", "GetGuardrailResponse", true, false, false}, + {Call::kCreateBatchJob, C, "CreateModelInvocationJob", "POST", "/model-invocation-job", "", "", "", "jobName,roleArn,clientRequestToken,modelId,inputDataConfig,outputDataConfig,timeoutDurationInHours,tags,modelInvocationType", "CreateModelInvocationJobResponse", true, false, true}, + {Call::kGetBatchJob, C, "GetModelInvocationJob", "GET", "/model-invocation-job/{jobIdentifier}", "jobIdentifier", "", "", "", "GetModelInvocationJobResponse", true, false, false}, + {Call::kListBatchJobs, C, "ListModelInvocationJobs", "GET", "/model-invocation-jobs", "", "statusEquals,nameContains,maxResults,nextToken,sortBy,sortOrder", "", "", "ListModelInvocationJobsResponse", true, false, false}, + {Call::kStopBatchJob, C, "StopModelInvocationJob", "POST", "/model-invocation-job/{jobIdentifier}/stop", "jobIdentifier", "", "", "", "StopModelInvocationJobResponse", true, false, true}, +}; + +// Members we read from replies; dotted names descend into structures. +const SchemaFields kSchemas[] = { + {"ConverseResponse", "bedrock-runtime", "output.message,stopReason,usage.inputTokens,usage.outputTokens,usage.totalTokens,usage.cacheReadInputTokens,usage.cacheWriteInputTokens,metrics.latencyMs,additionalModelResponseFields,trace,performanceConfig,serviceTier"}, + {"ConverseStreamOutput", "bedrock-runtime", "messageStart,contentBlockStart,contentBlockDelta,contentBlockStop,messageStop,metadata,internalServerException,modelStreamErrorException,validationException,throttlingException,serviceUnavailableException"}, + {"ContentBlockDeltaEvent", "bedrock-runtime", "delta,contentBlockIndex"}, + {"ContentBlockStartEvent", "bedrock-runtime", "start,contentBlockIndex"}, + {"MessageStopEvent", "bedrock-runtime", "stopReason,additionalModelResponseFields"}, + {"ConverseStreamMetadataEvent", "bedrock-runtime", "usage.inputTokens,usage.outputTokens,usage.totalTokens,usage.cacheReadInputTokens,usage.cacheWriteInputTokens,metrics.latencyMs"}, + {"InvokeModelResponse", "bedrock-runtime", "body,contentType"}, + {"ResponseStream", "bedrock-runtime", "chunk,internalServerException,modelStreamErrorException,validationException,throttlingException,modelTimeoutException,serviceUnavailableException"}, + {"PayloadPart", "bedrock-runtime", "bytes"}, + {"CountTokensResponse", "bedrock-runtime", "inputTokens"}, + {"ApplyGuardrailResponse", "bedrock-runtime", "usage,action,actionReason,outputs,assessments,guardrailCoverage"}, + {"RetrieveResponse", "bedrock-agent-runtime", "retrievalResults,nextToken,guardrailAction"}, + {"KnowledgeBaseRetrievalResult", "bedrock-agent-runtime", "content.text,content.type,content.byteContent,documentId,location.type,location.s3Location.uri,location.webLocation.url,metadata,score"}, + {"ResponseStream", "bedrock-agent-runtime", "chunk,trace,returnControl,files,accessDeniedException,badGatewayException,conflictException,dependencyFailedException,internalServerException,modelNotReadyException,resourceNotFoundException,serviceQuotaExceededException,throttlingException,validationException"}, + {"PayloadPart", "bedrock-agent-runtime", "bytes,attribution"}, + {"ListFoundationModelsResponse", "bedrock", "modelSummaries"}, + {"FoundationModelSummary", "bedrock", "modelArn,modelId,modelName,providerName,inputModalities,outputModalities,responseStreamingSupported,customizationsSupported,inferenceTypesSupported,modelLifecycle.status"}, + {"GetFoundationModelResponse", "bedrock", "modelDetails"}, + {"GetInferenceProfileResponse", "bedrock", "inferenceProfileName,inferenceProfileArn,inferenceProfileId,models,status,type,description"}, + {"ListInferenceProfilesResponse", "bedrock", "inferenceProfileSummaries,nextToken"}, + {"InferenceProfileSummary", "bedrock", "inferenceProfileName,inferenceProfileArn,inferenceProfileId,models,status,type,description"}, + {"ListGuardrailsResponse", "bedrock", "guardrails,nextToken"}, + {"GuardrailSummary", "bedrock", "id,arn,status,name,description,version,createdAt,updatedAt"}, + {"GetGuardrailResponse", "bedrock", "name,description,guardrailId,guardrailArn,version,status,blockedInputMessaging,blockedOutputsMessaging,createdAt,updatedAt"}, + {"CreateModelInvocationJobResponse", "bedrock", "jobArn"}, + {"GetModelInvocationJobResponse", "bedrock", "jobArn,jobName,modelId,status,message,submitTime,lastModifiedTime,endTime,inputDataConfig,outputDataConfig,timeoutDurationInHours,totalRecordCount,processedRecordCount,successRecordCount,errorRecordCount"}, + {"ListModelInvocationJobsResponse", "bedrock", "invocationJobSummaries,nextToken"}, + {"ModelInvocationJobSummary", "bedrock", "jobArn,jobName,modelId,status,message,submitTime,lastModifiedTime,endTime,totalRecordCount,processedRecordCount,successRecordCount,errorRecordCount"}, + {"StopModelInvocationJobResponse", "bedrock", ""}, +}; + +constexpr std::size_t kCallCount = sizeof(kCalls) / sizeof(kCalls[0]); +constexpr std::size_t kSchemaCount = sizeof(kSchemas) / sizeof(kSchemas[0]); + +} // namespace + +const CallSpec& Calls::spec_of(Call call) { + for (const CallSpec& s : kCalls) + if (s.call == call) return s; + throw llm::ApiError::invalid("bedrock", "unknown call"); +} + +clients::Method Calls::method_of(Call call) { + return std::string(spec_of(call).http_method) == "GET" ? clients::Method::kGet : clients::Method::kPost; +} + +std::size_t Calls::count() { return kCallCount; } +const CallSpec& Calls::at(std::size_t index) { return kCalls[index]; } +std::size_t Calls::schema_count() { return kSchemaCount; } +const SchemaFields& Calls::schema_at(std::size_t index) { return kSchemas[index]; } + +bool Calls::valid_path_value(const std::string& value) { + if (value.empty() || value.size() > BedrockCaps::kMaxModelIdChars) return false; + if (value.find("..") != std::string::npos) return false; + for (char c : value) + if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || + c == '_' || c == '.' || c == ':' || c == '/')) + return false; + return true; +} + +std::string Calls::render(Call call, const PathArgs& args) { + std::string uri = spec_of(call).uri; + std::string out; + std::size_t i = 0; + while (i < uri.size()) { + if (uri[i] != '{') { + out.push_back(uri[i++]); + continue; + } + std::size_t close = uri.find('}', i); + std::string name = uri.substr(i + 1, close - i - 1); + auto it = args.find(name); + if (it == args.end()) throw llm::ApiError::invalid("bedrock", "missing " + name); + if (!valid_path_value(it->second)) throw llm::ApiError::invalid("bedrock", "bad " + name); + out += clients::Url::encode_component(it->second); + i = close + 1; + } + return out; +} + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/calls.h b/src/vendors/bedrock/calls.h new file mode 100644 index 0000000..786ecfd --- /dev/null +++ b/src/vendors/bedrock/calls.h @@ -0,0 +1,65 @@ +#pragma once + +// The ONE table of REST calls this module makes. tools/check_spec.py checks +// every row against the AWS service models (botocore service-2.json for +// bedrock, bedrock-runtime, bedrock-agent-runtime): operation name, HTTP +// method, request URI, path / query / header / body members, bearer support, +// and the response fields we read. Code builds every path from this table. + +#include +#include +#include + +#include "clients/request.h" +#include "vendors/bedrock/credentials.h" + +namespace molo::bedrock { + +enum class Call { + kConverse, kConverseStream, kInvokeModel, kInvokeModelStream, kCountTokens, kApplyGuardrail, + kInvokeAgent, kRetrieve, + kListFoundationModels, kGetFoundationModel, kListInferenceProfiles, kGetInferenceProfile, + kListGuardrails, kGetGuardrail, + kCreateBatchJob, kGetBatchJob, kListBatchJobs, kStopBatchJob, +}; + +struct CallSpec { + Call call; + AwsService service; + const char* op; // service model operation name + const char* http_method; // GET POST + const char* uri; // request URI template, "{name}" placeholders + const char* path_params; // comma list, same names as the placeholders + const char* query_params; // comma list of query string names we send + const char* header_params; // comma list of header names we send + const char* body_fields; // comma list of JSON body members we send ("body" = raw payload) + const char* response; // response schema name in the schema table + bool bearer_ok; // service model lists httpBearerAuth (Bedrock API keys) + bool event_stream; // the success reply is application/vnd.amazon.eventstream + bool changes_state; // creates, stops or costs money: demo needs confirm +}; + +struct SchemaFields { + const char* schema; + const char* service; // which service model: bedrock, bedrock-runtime, bedrock-agent-runtime + const char* fields; // comma list of members we read, dotted for nested ("usage.inputTokens") +}; + +using PathArgs = std::map; +using QueryArgs = std::map; + +struct Calls { + static const CallSpec& spec_of(Call call); + static clients::Method method_of(Call call); + static std::size_t count(); + static const CallSpec& at(std::size_t index); + static std::size_t schema_count(); + static const SchemaFields& schema_at(std::size_t index); + // The URI with every {placeholder} replaced by the percent-encoded value; + // throws ApiError::invalid for a missing, empty, over-long or odd value. + static std::string render(Call call, const PathArgs& args); + // Allowed characters of an id placed in a path: [A-Za-z0-9._:/-], no "..". + static bool valid_path_value(const std::string& value); +}; + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/caps.h b/src/vendors/bedrock/caps.h new file mode 100644 index 0000000..66a0dba --- /dev/null +++ b/src/vendors/bedrock/caps.h @@ -0,0 +1,42 @@ +#pragma once + +// Static ceilings for the Bedrock module. Never autotuned. Numbers marked +// (spec) come from the AWS service models (see SPEC.md ## Sources), the rest +// are our own hard caps. + +#include + +namespace molo::bedrock { + +struct BedrockCaps { + static constexpr std::size_t kMaxInvokeBodyBytes = 25000000; // (spec) blob max + static constexpr std::size_t kMaxResponseBytes = 64u * 1024u * 1024u; + static constexpr std::size_t kMaxStreamBytes = 64u * 1024u * 1024u; + // Event stream framing: AWS documents 16 MiB per message and 128 KiB of headers. + static constexpr std::size_t kMaxEventMessageBytes = 16u * 1024u * 1024u; + static constexpr std::size_t kMaxEventHeaderBytes = 128u * 1024u; + static constexpr std::size_t kMaxStreamEvents = 200000; + static constexpr std::size_t kMaxModelIdChars = 2048; // (spec) + static constexpr std::size_t kMaxRegionChars = 32; + static constexpr std::size_t kMaxMessages = 4096; + static constexpr std::size_t kMaxTools = 128; + static constexpr std::size_t kMaxStopSequences = 2500; // (spec) + static constexpr std::size_t kMaxContentBlocks = 512; + static constexpr std::size_t kMaxSystemBlocks = 64; + static constexpr std::size_t kMaxSignedHeaders = 64; + static constexpr std::size_t kMaxQueryParams = 64; + static constexpr std::size_t kMaxPathChars = 4096; + static constexpr int kMaxResults = 1000; // (spec) list calls + static constexpr std::size_t kMaxPages = 20; // list walks + static constexpr std::size_t kMaxItemsReturned = 5000; + static constexpr int kMaxRetrieveResults = 100; // (spec) + static constexpr std::size_t kMaxRetrieveQueryChars = 20000; // (spec) + static constexpr std::size_t kMaxAgentInputChars = 25000000; // (spec) + static constexpr std::size_t kMaxGuardrailBlocks = 64; + static constexpr std::size_t kMaxGuardrailTextChars = 1000000; + static constexpr int kMinBatchTimeoutHours = 24; // (spec) + static constexpr int kMaxBatchTimeoutHours = 168; // (spec) + static constexpr std::size_t kMaxJobNameChars = 63; // (spec) +}; + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/chat_model.cpp b/src/vendors/bedrock/chat_model.cpp new file mode 100644 index 0000000..c3212bb --- /dev/null +++ b/src/vendors/bedrock/chat_model.cpp @@ -0,0 +1,193 @@ +#include "vendors/bedrock/chat_model.h" + +namespace molo::bedrock { + +namespace { + +[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("bedrock", why); } + +std::string image_format(const std::string& mime) { + if (mime == "image/png") return "png"; + if (mime == "image/jpeg" || mime == "image/jpg") return "jpeg"; + if (mime == "image/gif") return "gif"; + if (mime == "image/webp") return "webp"; + bad("image mime must be image/png, image/jpeg, image/gif or image/webp"); +} + +const char* role_of(llm::Role r) { return r == llm::Role::kAssistant ? "assistant" : "user"; } + +std::string str(const Json* v) { return v && v->is_string() ? v->as_string() : std::string(); } + +} // namespace + +BedrockChatModel::BedrockChatModel(std::shared_ptr runtime, std::string default_model) + : runtime_(std::move(runtime)), default_model_(std::move(default_model)) {} + +std::shared_ptr BedrockChatModel::from_env(const std::string& default_model) { + return std::make_shared(std::make_shared(BedrockClient::from_env()), default_model); +} + +std::string BedrockChatModel::finish_of(const std::string& s) { + if (s == "end_turn" || s == "stop_sequence") return "stop"; + if (s == "max_tokens" || s == "model_context_window_exceeded") return "length"; + if (s == "tool_use") return "tool_calls"; + if (s == "guardrail_intervened" || s == "content_filtered") return "content_filter"; + return s; +} + +ConverseRequest BedrockChatModel::to_converse(const llm::ChatRequest& r) const { + ConverseRequest out; + out.model_id = r.model.empty() ? default_model_ : r.model; + if (!r.system.empty()) out.add_system(r.system); + + // Build messages, merging consecutive same-role ones. + std::string last_role; + std::vector pending; + auto flush = [&] { + if (!pending.empty()) out.add(Blocks::message(last_role, pending)); + pending.clear(); + }; + for (const llm::Message& m : r.messages) { + if (m.role == llm::Role::kSystem) { + if (!m.text().empty()) out.add_system(m.text()); + continue; + } + std::string role = role_of(m.role); + std::vector blocks; + for (const llm::ContentPart& p : m.parts) { + if (!p.reasoning.empty()) { + if (m.role == llm::Role::kAssistant && !p.reasoning_signature.empty()) { + Json rt = Json::object(); + rt.set("text", Json(p.reasoning)); + rt.set("signature", Json(p.reasoning_signature)); + Json rc = Json::object(); + rc.set("reasoningText", rt); + Json b = Json::object(); + b.set("reasoningContent", rc); + blocks.push_back(b); + } + } + if (!p.text.empty()) blocks.push_back(Blocks::text(p.text)); + if (p.image) { + if (p.image->bytes.empty()) bad("Converse takes image bytes, not image URLs"); + blocks.push_back(Blocks::image(image_format(p.image->mime), p.image->bytes)); + } + if (p.tool_call) blocks.push_back(Blocks::tool_use(p.tool_call->id, p.tool_call->name, p.tool_call->arguments)); + if (p.tool_result) blocks.push_back(Blocks::tool_result(p.tool_result->call_id, p.tool_result->content, p.tool_result->is_error)); + } + if (blocks.empty()) continue; + if (role != last_role) flush(); + last_role = role; + for (Json& b : blocks) pending.push_back(std::move(b)); + } + flush(); + + if (r.max_tokens) out.max_tokens = r.max_tokens; + out.temperature = r.temperature; + out.top_p = r.top_p; + out.stop_sequences = r.stop; + + if (r.tool_choice != "none") { + for (const llm::ToolSpec& t : r.tools) out.tools.push_back(Blocks::tool(t.name, t.description, t.parameters)); + if (!out.tools.empty()) { + if (r.tool_choice == "required") out.tool_choice = "any"; + else out.tool_choice = r.tool_choice; // "", "auto" or a tool name + } + } + + if (r.json_mode && !r.response_schema.is_object()) + bad("Converse has no JSON mode: pass response_schema (outputConfig.textFormat)"); + if (r.response_schema.is_object()) out.output_schema = r.response_schema; + out.effort = r.reasoning_effort; + + if (r.extra.is_object()) { + for (const auto& [key, value] : r.extra.as_object()) { + if (key == "additionalModelRequestFields") { + if (!value.is_object()) bad("additionalModelRequestFields must be an object"); + out.additional_fields = value; + } else if (key == "guardrailIdentifier") out.guardrail_id = str(&value); + else if (key == "guardrailVersion") out.guardrail_version = str(&value); + else if (key == "guardrailTrace") out.guardrail_trace = str(&value); + else if (key == "serviceTier") out.service_tier = str(&value); + else if (key == "latency") out.latency = str(&value); + else if (key == "requestMetadata") { + if (!value.is_object()) bad("requestMetadata must be an object"); + for (const auto& [k, v] : value.as_object()) { + if (!v.is_string()) bad("requestMetadata values must be strings"); + out.request_metadata[k] = v.as_string(); + } + } else { + bad("unsupported extra key: " + key); + } + } + } + return out; +} + +llm::ChatResponse BedrockChatModel::to_response(const ConverseResult& result, const std::string& model) { + llm::ChatResponse out; + out.model = model; + out.message.role = llm::Role::kAssistant; + std::string reasoning = result.reasoning(); + if (!reasoning.empty()) { + llm::ContentPart p; + p.reasoning = reasoning; + p.reasoning_signature = result.reasoning_signature(); + out.message.parts.push_back(std::move(p)); + } + std::string text = result.text(); + if (!text.empty()) out.message.parts.push_back(llm::ContentPart::of_text(text)); + for (const ConverseToolUse& u : result.tool_uses()) { + llm::ToolCall c; + c.id = u.id; + c.name = u.name; + c.arguments = u.input; + out.message.parts.push_back(llm::ContentPart::of_call(std::move(c))); + } + out.finish_reason = finish_of(result.stop_reason()); + ConverseUsage u = result.usage(); + out.usage.input_tokens = u.input_tokens; + out.usage.output_tokens = u.output_tokens; + out.usage.total_tokens = u.total_tokens; + out.usage.cached_tokens = u.cache_read_tokens; + out.raw = result.raw(); + return out; +} + +llm::ChatResponse BedrockChatModel::generate(const llm::ChatRequest& request) { + ConverseRequest wire = to_converse(request); + return to_response(runtime_->converse(wire), wire.model_id); +} + +void BedrockChatModel::stream(const llm::ChatRequest& request, + const std::function& on_chunk) { + ConverseRequest wire = to_converse(request); + runtime_->converse_stream(wire, [&](const StreamDelta& d) { + llm::ChatChunk c; + bool emit = false; + if (d.event == "contentBlockDelta") { + c.text_delta = d.text; + c.reasoning_delta = d.reasoning; + c.tool_args_delta = d.tool_args; + emit = !d.text.empty() || !d.reasoning.empty() || !d.tool_args.empty(); + } else if (d.event == "contentBlockStart" && !d.tool_name.empty()) { + c.tool_call_id = d.tool_id; + c.tool_call_name = d.tool_name; + emit = true; + } else if (d.event == "messageStop") { + c.finish_reason = finish_of(d.stop_reason); + emit = true; + } else if (d.event == "metadata" && d.usage) { + llm::Usage u; + u.input_tokens = d.usage->input_tokens; + u.output_tokens = d.usage->output_tokens; + u.total_tokens = d.usage->total_tokens; + u.cached_tokens = d.usage->cache_read_tokens; + c.usage = u; + emit = true; + } + return emit ? on_chunk(c) : true; + }); +} + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/chat_model.h b/src/vendors/bedrock/chat_model.h new file mode 100644 index 0000000..036468a --- /dev/null +++ b/src/vendors/bedrock/chat_model.h @@ -0,0 +1,56 @@ +#pragma once + +// BedrockChatModel: the llm::ChatModel seam over Converse / ConverseStream. +// +// Mapping notes (each one is a decision, not a guess): +// - system text and Role::kSystem messages become Converse `system` blocks. +// - Role::kTool messages (tool results) become user-role toolResult blocks; +// consecutive messages of the same role are merged, because Converse needs +// alternating roles. +// - Assistant reasoning is replayed as reasoningContent only when it has a +// signature; unsigned reasoning is dropped (the service would reject it). +// - Images must carry bytes (Converse takes no image URLs): a URL-only image +// is rejected loudly. mime image/png|jpeg|gif|webp maps to the format enum. +// - tool_choice: auto -> auto, required -> any, a tool name -> tool, "none" +// drops the tools from the request (Converse has no "none"). +// - json_mode alone is rejected (Converse has no JSON mode); response_schema +// maps to outputConfig.textFormat json_schema; reasoning_effort to +// outputConfig.effort. +// - extra accepts: additionalModelRequestFields (object), guardrailIdentifier, +// guardrailVersion, guardrailTrace, serviceTier, latency, requestMetadata +// (object of strings). Any other key is rejected. +// - finish_reason: end_turn / stop_sequence -> stop; max_tokens / +// model_context_window_exceeded -> length; tool_use -> tool_calls; +// guardrail_intervened / content_filtered -> content_filter; other values +// pass through. + +#include + +#include "llm/chat_types.h" +#include "vendors/bedrock/runtime.h" + +namespace molo::bedrock { + +class BedrockChatModel : public llm::ChatModel { + public: + static constexpr const char* kDefaultModel = "us.amazon.nova-micro-v1:0"; + + explicit BedrockChatModel(std::shared_ptr runtime, std::string default_model = kDefaultModel); + static std::shared_ptr from_env(const std::string& default_model = kDefaultModel); + + std::string vendor() const override { return "bedrock"; } + llm::ChatResponse generate(const llm::ChatRequest& request) override; + void stream(const llm::ChatRequest& request, + const std::function& on_chunk) override; + + // The wire request a neutral request maps to (also used by tests). + ConverseRequest to_converse(const llm::ChatRequest& request) const; + static llm::ChatResponse to_response(const ConverseResult& result, const std::string& model); + static std::string finish_of(const std::string& stop_reason); + + private: + std::shared_ptr runtime_; + std::string default_model_; +}; + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/client.cpp b/src/vendors/bedrock/client.cpp new file mode 100644 index 0000000..a12a742 --- /dev/null +++ b/src/vendors/bedrock/client.cpp @@ -0,0 +1,248 @@ +#include "vendors/bedrock/client.h" + +#include +#include + +#include "clients/transports/http_transport.h" +#include "llm/caps.h" +#include "vendors/bedrock/caps.h" + +namespace molo::bedrock { + +using clients::body::Json; + +namespace { + +clients::ClientConfig make_config() { + clients::ClientConfig c; + c.persist_cookies = false; + return c; +} + +std::string lower(std::string s) { + for (char& c : s) c = static_cast(std::tolower(static_cast(c))); + return s; +} + +std::vector split(const std::string& list) { + std::vector out; + std::string cur; + for (char c : list) { + if (c == ',') { + if (!cur.empty()) out.push_back(cur); + cur.clear(); + } else { + cur.push_back(c); + } + } + if (!cur.empty()) out.push_back(cur); + return out; +} + +bool listed(const std::string& list, const std::string& name) { + for (const std::string& item : split(list)) + if (lower(item) == lower(name)) return true; + return false; +} + +// "ValidationException:http://internal.amazon.com/..." or "com.x#Name" -> "Name". +std::string clean_error_type(std::string t) { + auto colon = t.find(':'); + if (colon != std::string::npos) t.resize(colon); + auto hash = t.rfind('#'); + if (hash != std::string::npos) t.erase(0, hash + 1); + return t; +} + +std::string time_text(std::time_t t) { return SigV4Signer::format_amz_date(t); } + +} // namespace + +BedrockClient::BedrockClient(std::unique_ptr transport, Credentials credentials, + Clock clock) + : credentials_(std::move(credentials)), + clock_(clock ? std::move(clock) : Clock([] { return std::time(nullptr); })), + client_(std::move(transport), make_config()) {} + +std::shared_ptr BedrockClient::from_env() { + return std::make_shared(std::make_unique(), + Credentials::from_env()); +} + +void BedrockClient::guard(const CallSpec& spec) const { + if (!credentials_.configured()) throw llm::NotConfiguredError(vendor(), Credentials::env_hint()); + bool bearer = spec.bearer_ok && !credentials_.bearer.empty(); + if (!bearer && !credentials_.aws.valid()) + throw llm::ApiError(vendor(), 503, "NOT_CONFIGURED", + std::string(spec.op) + " needs SigV4 credentials (set " + + Credentials::kEnvAccessKey + " and " + Credentials::kEnvSecretKey + + "); Bedrock API keys do not work on this host"); +} + +BedrockClient::Built BedrockClient::build(const CallInput& in) const { + const CallSpec& spec = Calls::spec_of(in.call); + guard(spec); + if (in.json && in.raw) throw llm::ApiError::invalid(vendor(), "a call has one body"); + + std::string path = Calls::render(in.call, in.path); + if (path.size() > BedrockCaps::kMaxPathChars) throw llm::ApiError::invalid(vendor(), "path too long"); + + // Query: only names of the table, encoded and sorted so the wire form equals + // the canonical form. + if (in.query.size() > BedrockCaps::kMaxQueryParams) + throw llm::ApiError::invalid(vendor(), "too many query parameters"); + std::vector> q; + for (const auto& [k, v] : in.query) { + if (!listed(spec.query_params, k)) throw llm::ApiError::invalid(vendor(), "query parameter " + k + " is not part of " + spec.op); + q.emplace_back(clients::Url::encode_component(k), clients::Url::encode_component(v)); + } + std::sort(q.begin(), q.end()); + std::string query; + for (const auto& [k, v] : q) { + if (!query.empty()) query.push_back('&'); + query += k + "=" + v; + } + + std::string body; + bool has_body = false; + std::string content_type = in.content_type; + if (in.json) { + body = in.json->dump(); + has_body = true; + } else if (in.raw) { + body = *in.raw; + has_body = true; + } + if (body.size() > BedrockCaps::kMaxInvokeBodyBytes) + throw llm::ApiError::invalid(vendor(), "request body over the byte cap"); + + std::string host = Endpoints::host(spec.service, credentials_.region); + HeaderList headers; + headers.emplace_back("Host", host); + if (has_body) headers.emplace_back("Content-Type", content_type); + for (const auto& [name, value] : in.headers) { + if (!listed(spec.header_params, name)) + throw llm::ApiError::invalid(vendor(), "header " + name + " is not part of " + spec.op); + if (lower(name) == "content-type") { + // The table lists Content-Type for the raw calls: it replaces the default. + for (auto& h : headers) + if (lower(h.first) == "content-type") h.second = value; + content_type = value; + continue; + } + headers.emplace_back(name, value); + } + + Built built; + built.preview.method = spec.http_method; + built.preview.url = "https://" + host + path + (query.empty() ? "" : "?" + query); + + bool bearer = spec.bearer_ok && !credentials_.bearer.empty(); + HeaderList wire = headers; + if (bearer) { + built.preview.auth_mode = "bearer"; + } else { + SigV4Signer signer(credentials_.aws, credentials_.region, Endpoints::signing_name()); + SigV4Request sr; + sr.method = spec.http_method; + sr.path = path; + sr.query = query; + sr.headers = headers; + sr.body = body; + SigV4Result sig = signer.sign(sr, time_text(clock_())); + for (const auto& h : sig.added_headers) wire.push_back(h); + built.preview.auth_mode = "sigv4"; + built.preview.signed_headers = sig.signed_headers; + built.preview.canonical_request = sig.canonical_request; + } + + clients::ClientRequest req(Calls::method_of(in.call), clients::Url::parse(built.preview.url)); + for (const auto& [name, value] : wire) req.header(name, value); + if (bearer) req.bearer(credentials_.bearer); + if (has_body) req.bytes(std::move(body), content_type); + // A POST without a body still says "Content-Length: 0" (the transport only + // sends the header when a body string exists); no Content-Type is signed. + else if (Calls::method_of(in.call) == clients::Method::kPost) req.bytes(std::string(), ""); + built.request = std::move(req); + return built; +} + +WirePreview BedrockClient::preview(const CallInput& input) const { return build(input).preview; } + +void BedrockClient::throw_http(const clients::ClientResponse& response) { + std::string type = "UNKNOWN"; + std::string message = "HTTP " + std::to_string(response.status()); + std::string request_id = response.headers().get_or("x-amzn-RequestId", ""); + if (auto h = response.headers().get("x-amzn-ErrorType"); h && !h->empty()) type = clean_error_type(*h); + try { + Json doc = response.json(); + if (doc.is_object()) { + for (const char* key : {"message", "Message", "errorMessage"}) + if (const Json* m = doc.find(key); m && m->is_string()) { + message = m->as_string(); + break; + } + if (type == "UNKNOWN") + for (const char* key : {"__type", "code", "Code"}) + if (const Json* t = doc.find(key); t && t->is_string()) { + type = clean_error_type(t->as_string()); + break; + } + } + } catch (const std::exception&) { + // Not JSON: keep the generic message. + } + throw llm::ApiError(vendor(), response.status(), type, message, request_id); +} + +Reply BedrockClient::raw(const CallInput& input) { + try { + Built built = build(input); + clients::ClientResponse response = client_.send_blocking(std::move(built.request)); + if (!response.is_success()) throw_http(response); + if (response.body().size() > BedrockCaps::kMaxResponseBytes) + throw llm::ApiError(vendor(), 502, "BAD_RESPONSE", "response over the byte cap"); + Reply r; + r.status = response.status(); + r.body = response.body(); + r.content_type = response.content_type(); + r.headers = response.headers(); + return r; + } catch (const llm::ApiError&) { + throw; + } catch (const std::exception& e) { + throw llm::ApiError(vendor(), 502, "UNAVAILABLE", e.what()); + } +} + +Json BedrockClient::json(const CallInput& input) { + Reply r = raw(input); + if (r.body.find_first_not_of(" \t\r\n") == std::string::npos) return Json::object(); + try { + return Json::parse(r.body); + } catch (const std::exception& e) { + throw llm::ApiError(vendor(), 502, "BAD_RESPONSE", e.what()); + } +} + +void BedrockClient::stream(const CallInput& input, const std::function& sink) { + try { + Built built = build(input); + client_.send_stream_blocking(std::move(built.request), sink); + } catch (const llm::ApiError&) { + throw; + } catch (const std::exception& e) { + throw llm::ApiError(vendor(), 502, "UNAVAILABLE", e.what()); + } +} + +void BedrockService::check_id(const std::string& what, const std::string& id) const { + if (!valid_id(id)) throw llm::ApiError::invalid("bedrock", "bad " + what + " id"); +} + +std::string BedrockService::string_at(const Json& doc, const std::string& key, const std::string& fallback) { + const Json* v = doc.is_object() ? doc.find(key) : nullptr; + return v && v->is_string() ? v->as_string() : fallback; +} + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/client.h b/src/vendors/bedrock/client.h new file mode 100644 index 0000000..d73baa8 --- /dev/null +++ b/src/vendors/bedrock/client.h @@ -0,0 +1,111 @@ +#pragma once + +// BedrockClient: the one place the module talks HTTP. llm::ApiClient cannot be +// used because SigV4 signs the final request (method, path, query, headers and +// body hash) per call, which a fixed bearer / header-key client cannot do. +// This class does the same job for Bedrock: builds the request from the call +// table, signs it (SigV4) or sends the Bedrock API key (bearer), and maps every +// failure to llm::ApiError. + +#include +#include +#include +#include + +#include "clients/body/json.h" +#include "clients/client.h" +#include "llm/error.h" +#include "vendors/bedrock/calls.h" +#include "vendors/bedrock/credentials.h" +#include "vendors/bedrock/event_stream.h" +#include "vendors/bedrock/sigv4.h" + +namespace molo::bedrock { + +using Json = clients::body::Json; + +struct CallInput { + Call call = Call::kConverse; + PathArgs path; + QueryArgs query; + HeaderList headers; // extra headers; names must be in the table + const clients::body::Json* json = nullptr; // JSON body + const std::string* raw = nullptr; // raw body (InvokeModel payload) + std::string content_type = "application/json"; +}; + +struct Reply { + int status = 0; + std::string body; + std::string content_type; + clients::HeaderMap headers; +}; + +// What a call would put on the wire, without sending it. The secret is not in +// it; the Authorization header is shown as "". Used by dry runs. +struct WirePreview { + std::string method; + std::string url; + std::string auth_mode; + std::string signed_headers; + std::string canonical_request; // empty for bearer +}; + +class BedrockClient { + public: + using Clock = std::function; + + BedrockClient(std::unique_ptr transport, Credentials credentials, + Clock clock = nullptr); + static std::shared_ptr from_env(); + + bool configured() const { return credentials_.configured(); } + const Credentials& credentials() const { return credentials_; } + const std::string& region() const { return credentials_.region; } + clients::ClientStatsSnapshot stats() const { return client_.stats(); } + + // JSON in, JSON out. Empty 2xx body gives {}. + clients::body::Json json(const CallInput& input); + Reply raw(const CallInput& input); + // Sink gets raw body chunks of an event-stream reply (feed an + // EventStreamDecoder); the decoder's finish() must be called by the caller. + void stream(const CallInput& input, const std::function& sink); + + // Builds, validates and signs without sending. + WirePreview preview(const CallInput& input) const; + + static std::string vendor() { return "bedrock"; } + + private: + struct Built { + clients::ClientRequest request; + WirePreview preview; + }; + Built build(const CallInput& input) const; + void guard(const CallSpec& spec) const; + [[noreturn]] static void throw_http(const clients::ClientResponse& response); + + Credentials credentials_; + Clock clock_; + clients::Client client_; +}; + +// Base for the service classes. Not llm::Service: that wraps an ApiClient. +class BedrockService { + public: + explicit BedrockService(std::shared_ptr client) : client_(std::move(client)) {} + virtual ~BedrockService() = default; + bool configured() const { return client_->configured(); } + BedrockClient& client() const { return *client_; } + + // Same rule as llm::Service::valid_id but for ids that may be ARNs. + static bool valid_id(const std::string& id) { return Calls::valid_path_value(id); } + void check_id(const std::string& what, const std::string& id) const; + static std::string string_at(const clients::body::Json& doc, const std::string& key, + const std::string& fallback = ""); + + protected: + std::shared_ptr client_; +}; + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/control.cpp b/src/vendors/bedrock/control.cpp new file mode 100644 index 0000000..2282e57 --- /dev/null +++ b/src/vendors/bedrock/control.cpp @@ -0,0 +1,238 @@ +#include "vendors/bedrock/control.h" + +#include "vendors/bedrock/caps.h" + +namespace molo::bedrock { + +namespace { + +[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("bedrock", why); } + +bool one_of(const std::string& v, std::initializer_list allowed) { + for (const char* a : allowed) + if (v == a) return true; + return false; +} + +bool alnum_dash(const std::string& s, const char* extra) { + for (char c : s) { + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) continue; + if (std::string(extra).find(c) != std::string::npos) continue; + return false; + } + return true; +} + +void check_max(int max_results) { + if (max_results < 1 || max_results > BedrockCaps::kMaxResults) bad("maxResults must be 1..1000"); +} + +// Walks nextToken pages of `call` collecting `field`, up to the page and item caps. +ListResult walk(BedrockClient& client, Call call, QueryArgs query, const char* field) { + ListResult out; + std::string token; + for (std::size_t page = 0; page < BedrockCaps::kMaxPages; ++page) { + CallInput in; + in.call = call; + in.query = query; + if (!token.empty()) in.query["nextToken"] = token; + Json r = client.json(in); + ++out.pages; + if (const Json* arr = r.find(field); arr && arr->is_array()) + for (const Json& x : arr->as_array()) { + if (out.items.as_array().size() >= BedrockCaps::kMaxItemsReturned) { + out.truncated = true; + break; + } + out.items.push(x); + } + const Json* t = r.find("nextToken"); + token = t && t->is_string() ? t->as_string() : ""; + if (out.truncated || token.empty()) break; + } + if (!token.empty()) { + out.truncated = true; + out.next_token = token; + } + return out; +} + +} // namespace + +// ---- Models --------------------------------------------------------------- + +ListResult Models::list(const ModelFilter& f) { + if (!f.customization_type.empty() && !one_of(f.customization_type, {"FINE_TUNING", "CONTINUED_PRE_TRAINING", "DISTILLATION"})) + bad("customization type must be FINE_TUNING, CONTINUED_PRE_TRAINING or DISTILLATION"); + if (!f.output_modality.empty() && !one_of(f.output_modality, {"TEXT", "IMAGE", "EMBEDDING"})) + bad("output modality must be TEXT, IMAGE or EMBEDDING"); + if (!f.inference_type.empty() && !one_of(f.inference_type, {"ON_DEMAND", "PROVISIONED"})) + bad("inference type must be ON_DEMAND or PROVISIONED"); + if (f.provider.size() > 64 || !alnum_dash(f.provider, " .-_")) bad("bad provider name"); + CallInput in; + in.call = Call::kListFoundationModels; + if (!f.provider.empty()) in.query["byProvider"] = f.provider; + if (!f.customization_type.empty()) in.query["byCustomizationType"] = f.customization_type; + if (!f.output_modality.empty()) in.query["byOutputModality"] = f.output_modality; + if (!f.inference_type.empty()) in.query["byInferenceType"] = f.inference_type; + Json r = client_->json(in); + ListResult out; + out.pages = 1; + if (const Json* arr = r.find("modelSummaries"); arr && arr->is_array()) { + for (const Json& x : arr->as_array()) { + if (out.items.as_array().size() >= BedrockCaps::kMaxItemsReturned) { + out.truncated = true; + break; + } + out.items.push(x); + } + } + return out; +} + +Json Models::get(const std::string& model_identifier) { + check_id("model", model_identifier); + CallInput in; + in.call = Call::kGetFoundationModel; + in.path["modelIdentifier"] = model_identifier; + Json r = client_->json(in); + const Json* d = r.find("modelDetails"); + return d ? *d : r; +} + +ListResult Models::list_profiles(const std::string& type, int max_results) { + check_max(max_results); + if (!type.empty() && !one_of(type, {"SYSTEM_DEFINED", "APPLICATION"})) bad("type must be SYSTEM_DEFINED or APPLICATION"); + QueryArgs q; + q["maxResults"] = std::to_string(max_results); + if (!type.empty()) q["type"] = type; + return walk(*client_, Call::kListInferenceProfiles, q, "inferenceProfileSummaries"); +} + +Json Models::get_profile(const std::string& id) { + check_id("inference profile", id); + CallInput in; + in.call = Call::kGetInferenceProfile; + in.path["inferenceProfileIdentifier"] = id; + return client_->json(in); +} + +// ---- Guardrails ----------------------------------------------------------- + +ListResult Guardrails::list(const std::string& guardrail_identifier, int max_results) { + check_max(max_results); + QueryArgs q; + q["maxResults"] = std::to_string(max_results); + if (!guardrail_identifier.empty()) { + check_id("guardrail", guardrail_identifier); + q["guardrailIdentifier"] = guardrail_identifier; + } + return walk(*client_, Call::kListGuardrails, q, "guardrails"); +} + +Json Guardrails::get(const std::string& guardrail_identifier, const std::string& version) { + check_id("guardrail", guardrail_identifier); + CallInput in; + in.call = Call::kGetGuardrail; + in.path["guardrailIdentifier"] = guardrail_identifier; + if (!version.empty()) { + bool digits = version.size() <= 8 && version.find_first_not_of("0123456789") == std::string::npos && version[0] != '0'; + if (!digits && version != "DRAFT") bad("version must be a number 1..99999999 or DRAFT"); + in.query["guardrailVersion"] = version; + } + return client_->json(in); +} + +// ---- Batch ---------------------------------------------------------------- + +void BatchJobParams::validate() const { + if (job_name.empty() || job_name.size() > BedrockCaps::kMaxJobNameChars) bad("jobName must be 1..63 characters"); + if (!alnum_dash(job_name, "+-.")) bad("jobName may hold letters, digits, + - and ."); + if (!((job_name[0] >= 'a' && job_name[0] <= 'z') || (job_name[0] >= 'A' && job_name[0] <= 'Z') || + (job_name[0] >= '0' && job_name[0] <= '9'))) + bad("jobName must start with a letter or digit"); + if (role_arn.rfind("arn:aws", 0) != 0 || role_arn.find(":iam::") == std::string::npos || role_arn.find(":role/") == std::string::npos || + role_arn.size() > 2048) + bad("roleArn must look like arn:aws:iam:::role/"); + if (model_id.empty() || model_id.size() > BedrockCaps::kMaxModelIdChars || !Calls::valid_path_value(model_id)) bad("bad modelId"); + for (const std::string* uri : {&input_s3_uri, &output_s3_uri}) + if (uri->rfind("s3://", 0) != 0 || uri->size() < 8 || uri->size() > 1024) bad("S3 URIs must start with s3:// and name a bucket"); + if (!invocation_type.empty() && !one_of(invocation_type, {"InvokeModel", "Converse"})) bad("modelInvocationType must be InvokeModel or Converse"); + if (!client_request_token.empty() && (client_request_token.size() > 256 || !alnum_dash(client_request_token, "-"))) + bad("clientRequestToken is 1..256 letters, digits and dashes"); + if (timeout_hours != 0 && (timeout_hours < BedrockCaps::kMinBatchTimeoutHours || timeout_hours > BedrockCaps::kMaxBatchTimeoutHours)) + bad("timeoutDurationInHours must be 24..168"); + if (tags.size() > 200) bad("at most 200 tags"); +} + +Json BatchJobParams::to_json() const { + validate(); + Json b = Json::object(); + b.set("jobName", Json(job_name)); + b.set("roleArn", Json(role_arn)); + b.set("modelId", Json(model_id)); + Json s3in = Json::object(); + s3in.set("s3Uri", Json(input_s3_uri)); + Json in = Json::object(); + in.set("s3InputDataConfig", s3in); + b.set("inputDataConfig", in); + Json s3out = Json::object(); + s3out.set("s3Uri", Json(output_s3_uri)); + Json out = Json::object(); + out.set("s3OutputDataConfig", s3out); + b.set("outputDataConfig", out); + if (!invocation_type.empty()) b.set("modelInvocationType", Json(invocation_type)); + if (!client_request_token.empty()) b.set("clientRequestToken", Json(client_request_token)); + if (timeout_hours != 0) b.set("timeoutDurationInHours", Json(timeout_hours)); + if (!tags.empty()) { + Json t = Json::array(); + for (const auto& [k, v] : tags) { + Json tag = Json::object(); + tag.set("key", Json(k)); + tag.set("value", Json(v)); + t.push(tag); + } + b.set("tags", t); + } + return b; +} + +std::string BatchJobs::create(const BatchJobParams& params) { + Json body = params.to_json(); + CallInput in; + in.call = Call::kCreateBatchJob; + in.json = &body; + Json r = client_->json(in); + return string_at(r, "jobArn"); +} + +Json BatchJobs::get(const std::string& job_identifier) { + check_id("job", job_identifier); + CallInput in; + in.call = Call::kGetBatchJob; + in.path["jobIdentifier"] = job_identifier; + return client_->json(in); +} + +ListResult BatchJobs::list(const std::string& status, const std::string& name_contains, int max_results) { + check_max(max_results); + if (!status.empty() && + !one_of(status, {"Submitted", "InProgress", "Completed", "Failed", "Stopping", "Stopped", "PartiallyCompleted", "Expired", "Validating", "Scheduled"})) + bad("unknown job status"); + if (name_contains.size() > 63) bad("nameContains is at most 63 characters"); + QueryArgs q; + q["maxResults"] = std::to_string(max_results); + if (!status.empty()) q["statusEquals"] = status; + if (!name_contains.empty()) q["nameContains"] = name_contains; + return walk(*client_, Call::kListBatchJobs, q, "invocationJobSummaries"); +} + +void BatchJobs::stop(const std::string& job_identifier) { + check_id("job", job_identifier); + CallInput in; + in.call = Call::kStopBatchJob; + in.path["jobIdentifier"] = job_identifier; + client_->json(in); +} + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/control.h b/src/vendors/bedrock/control.h new file mode 100644 index 0000000..60f70cc --- /dev/null +++ b/src/vendors/bedrock/control.h @@ -0,0 +1,77 @@ +#pragma once + +// bedrock (control plane, host bedrock..amazonaws.com): foundation +// models, inference profiles, guardrails (read only), batch inference jobs. +// Create and stop are explicit methods with no defaults and cost money or change +// state; everything else is read only. + +#include +#include + +#include "vendors/bedrock/client.h" + +namespace molo::bedrock { + +// A list that may have been cut: `truncated` is true when more pages existed +// than the cap allowed (never silent). +struct ListResult { + Json items = Json::array(); + bool truncated = false; + std::size_t pages = 0; + std::string next_token; // set when truncated, to continue by hand +}; + +struct ModelFilter { + std::string provider; // byProvider, e.g. "anthropic" + std::string customization_type; // FINE_TUNING | CONTINUED_PRE_TRAINING | DISTILLATION + std::string output_modality; // TEXT | IMAGE | EMBEDDING + std::string inference_type; // ON_DEMAND | PROVISIONED +}; + +class Models : public BedrockService { + public: + using BedrockService::BedrockService; + // modelSummaries (the call is not paginated). + ListResult list(const ModelFilter& filter = {}); + // modelDetails + Json get(const std::string& model_identifier); + // type: "" | SYSTEM_DEFINED | APPLICATION. Walks pages up to BedrockCaps::kMaxPages. + ListResult list_profiles(const std::string& type = "", int max_results = 100); + Json get_profile(const std::string& profile_identifier); +}; + +class Guardrails : public BedrockService { + public: + using BedrockService::BedrockService; + // guardrail_identifier filters to the versions of one guardrail ("" lists all). + ListResult list(const std::string& guardrail_identifier = "", int max_results = 100); + // version: "" returns the DRAFT / working view; "1".."99999999" or "DRAFT". + Json get(const std::string& guardrail_identifier, const std::string& version = ""); +}; + +struct BatchJobParams { + std::string job_name; // 1..63, [a-zA-Z0-9]... as the service model pattern + std::string role_arn; // arn:aws:iam:::role/... + std::string model_id; + std::string input_s3_uri; // s3://bucket/key.jsonl + std::string output_s3_uri; // s3://bucket/prefix/ + std::string invocation_type; // "" | InvokeModel | Converse + std::string client_request_token; // idempotency, optional + int timeout_hours = 0; // 0: service default, else 24..168 + std::map tags; // max 200 + void validate() const; + Json to_json() const; +}; + +class BatchJobs : public BedrockService { + public: + using BedrockService::BedrockService; + // Returns the job ARN. Costs money once the job runs. + std::string create(const BatchJobParams& params); + Json get(const std::string& job_identifier); + ListResult list(const std::string& status = "", const std::string& name_contains = "", int max_results = 100); + // Stops a running job. No default arguments on purpose. + void stop(const std::string& job_identifier); +}; + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/converse.cpp b/src/vendors/bedrock/converse.cpp new file mode 100644 index 0000000..ee17273 --- /dev/null +++ b/src/vendors/bedrock/converse.cpp @@ -0,0 +1,472 @@ +#include "vendors/bedrock/converse.h" + +#include + +#include "llm/error.h" +#include "security/crypto/Base64.h" +#include "vendors/bedrock/caps.h" + +namespace molo::bedrock { + +namespace { + +[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("bedrock", why); } + +bool one_of(const std::string& v, std::initializer_list allowed) { + for (const char* a : allowed) + if (v == a) return true; + return false; +} + +bool tool_name_ok(const std::string& n) { + if (n.empty() || n.size() > 64) return false; + for (char c : n) + if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-')) + return false; + return true; +} + +std::string str(const Json* v) { return v && v->is_string() ? v->as_string() : std::string(); } + +} // namespace + +// ---- Blocks --------------------------------------------------------------- + +Json Blocks::text(const std::string& t) { + Json b = Json::object(); + b.set("text", Json(t)); + return b; +} + +Json Blocks::image(const std::string& format, const std::string& bytes) { + if (!one_of(format, {"png", "jpeg", "gif", "webp"})) bad("image format must be png, jpeg, gif or webp"); + if (bytes.empty()) bad("image bytes are empty"); + Json source = Json::object(); + source.set("bytes", Json(security::crypto::Base64::encode(bytes))); + Json img = Json::object(); + img.set("format", Json(format)); + img.set("source", source); + Json b = Json::object(); + b.set("image", img); + return b; +} + +Json Blocks::document(const std::string& format, const std::string& name, const std::string& bytes) { + if (!one_of(format, {"pdf", "csv", "doc", "docx", "xls", "xlsx", "html", "txt", "md"})) + bad("document format must be pdf, csv, doc, docx, xls, xlsx, html, txt or md"); + if (name.empty() || name.size() > 200) bad("document name must be 1..200 characters"); + if (bytes.empty()) bad("document bytes are empty"); + Json source = Json::object(); + source.set("bytes", Json(security::crypto::Base64::encode(bytes))); + Json doc = Json::object(); + doc.set("format", Json(format)); + doc.set("name", Json(name)); + doc.set("source", source); + Json b = Json::object(); + b.set("document", doc); + return b; +} + +Json Blocks::tool_use(const std::string& id, const std::string& name, const Json& input) { + if (id.empty() || id.size() > 64) bad("toolUseId must be 1..64 characters"); + if (!tool_name_ok(name)) bad("tool name must be 1..64 of [A-Za-z0-9_-]"); + Json t = Json::object(); + t.set("toolUseId", Json(id)); + t.set("name", Json(name)); + t.set("input", input); + Json b = Json::object(); + b.set("toolUse", t); + return b; +} + +static Json tool_result_with(const std::string& id, const Json& content_block, bool is_error) { + if (id.empty() || id.size() > 64) bad("toolUseId must be 1..64 characters"); + Json content = Json::array(); + content.push(content_block); + Json r = Json::object(); + r.set("toolUseId", Json(id)); + r.set("content", content); + r.set("status", Json(is_error ? "error" : "success")); + Json b = Json::object(); + b.set("toolResult", r); + return b; +} + +Json Blocks::tool_result(const std::string& id, const std::string& text_value, bool is_error) { + return tool_result_with(id, text(text_value), is_error); +} + +Json Blocks::tool_result_json(const std::string& id, const Json& value, bool is_error) { + Json j = Json::object(); + j.set("json", value); + return tool_result_with(id, j, is_error); +} + +Json Blocks::cache_point() { + Json p = Json::object(); + p.set("type", Json("default")); + Json b = Json::object(); + b.set("cachePoint", p); + return b; +} + +Json Blocks::message(const std::string& role, const std::vector& content) { + if (role != "user" && role != "assistant") bad("message role must be user or assistant"); + if (content.empty()) bad("a message needs at least one content block"); + if (content.size() > BedrockCaps::kMaxContentBlocks) bad("too many content blocks in one message"); + Json arr = Json::array(); + for (const Json& c : content) arr.push(c); + Json m = Json::object(); + m.set("role", Json(role)); + m.set("content", arr); + return m; +} + +Json Blocks::user(const std::string& t) { return message("user", {text(t)}); } +Json Blocks::assistant(const std::string& t) { return message("assistant", {text(t)}); } + +Json Blocks::tool(const std::string& name, const std::string& description, const Json& json_schema, bool strict) { + if (!tool_name_ok(name)) bad("tool name must be 1..64 of [A-Za-z0-9_-]"); + if (!json_schema.is_object()) bad("tool input schema must be a JSON object"); + Json schema = Json::object(); + schema.set("json", json_schema); + Json spec = Json::object(); + spec.set("name", Json(name)); + if (!description.empty()) spec.set("description", Json(description)); + spec.set("inputSchema", schema); + if (strict) spec.set("strict", Json(true)); + Json b = Json::object(); + b.set("toolSpec", spec); + return b; +} + +// ---- usage ---------------------------------------------------------------- + +ConverseUsage ConverseUsage::from_json(const Json& u) { + ConverseUsage out; + auto num = [&](const char* key) { + const Json* v = u.is_object() ? u.find(key) : nullptr; + return v && v->is_number() ? static_cast(v->as_int()) : 0; + }; + out.input_tokens = num("inputTokens"); + out.output_tokens = num("outputTokens"); + out.total_tokens = num("totalTokens"); + out.cache_read_tokens = num("cacheReadInputTokens"); + out.cache_write_tokens = num("cacheWriteInputTokens"); + return out; +} + +// ---- request -------------------------------------------------------------- + +ConverseRequest& ConverseRequest::add(Json message) { + if (messages.size() >= BedrockCaps::kMaxMessages) bad("too many messages"); + messages.push_back(std::move(message)); + return *this; +} +ConverseRequest& ConverseRequest::add_user(const std::string& t) { return add(Blocks::user(t)); } +ConverseRequest& ConverseRequest::add_assistant(const std::string& t) { return add(Blocks::assistant(t)); } +ConverseRequest& ConverseRequest::add_system(const std::string& t) { + if (t.empty()) bad("system text is empty"); + if (system.size() >= BedrockCaps::kMaxSystemBlocks) bad("too many system blocks"); + system.push_back(Blocks::text(t)); + return *this; +} + +void ConverseRequest::validate(bool for_stream) const { + if (model_id.empty() || model_id.size() > BedrockCaps::kMaxModelIdChars) bad("modelId must be 1..2048 characters"); + if (messages.empty()) bad("at least one message is required"); + if (messages.size() > BedrockCaps::kMaxMessages) bad("too many messages"); + for (const Json& m : messages) { + if (!m.is_object()) bad("a message must be an object"); + std::string role = str(m.find("role")); + if (role != "user" && role != "assistant") bad("message role must be user or assistant"); + const Json* c = m.find("content"); + if (!c || !c->is_array() || c->as_array().empty()) bad("a message needs a non-empty content array"); + if (c->as_array().size() > BedrockCaps::kMaxContentBlocks) bad("too many content blocks in one message"); + } + if (system.size() > BedrockCaps::kMaxSystemBlocks) bad("too many system blocks"); + if (max_tokens && *max_tokens < 1) bad("maxTokens must be at least 1"); + if (temperature && (*temperature < 0.0 || *temperature > 1.0)) bad("temperature must be between 0 and 1"); + if (top_p && (*top_p < 0.0 || *top_p > 1.0)) bad("topP must be between 0 and 1"); + if (stop_sequences.size() > BedrockCaps::kMaxStopSequences) bad("too many stop sequences"); + for (const std::string& s : stop_sequences) + if (s.empty()) bad("a stop sequence must not be empty"); + if (tools.size() > BedrockCaps::kMaxTools) bad("too many tools"); + if (!tool_choice.empty() && tools.empty()) bad("toolChoice needs tools"); + if (!tool_choice.empty() && tool_choice != "auto" && tool_choice != "any" && !tool_name_ok(tool_choice)) + bad("toolChoice must be auto, any or a tool name"); + if (!guardrail_id.empty() && guardrail_version.empty()) bad("guardrailVersion is required with a guardrail"); + if (!guardrail_trace.empty() && !one_of(guardrail_trace, {"enabled", "disabled", "enabled_full"})) + bad("guardrail trace must be enabled, disabled or enabled_full"); + if (!guardrail_stream_mode.empty()) { + if (!for_stream) bad("streamProcessingMode applies to ConverseStream only"); + if (!one_of(guardrail_stream_mode, {"sync", "async"})) bad("streamProcessingMode must be sync or async"); + } + if (!latency.empty() && !one_of(latency, {"standard", "optimized"})) bad("latency must be standard or optimized"); + if (!service_tier.empty() && !one_of(service_tier, {"priority", "default", "flex", "reserved"})) + bad("service tier must be priority, default, flex or reserved"); + if (request_metadata.size() > 16) bad("requestMetadata holds at most 16 entries"); + if (additional_response_paths.size() > 10) bad("additionalModelResponseFieldPaths holds at most 10 entries"); + if (!output_schema.is_null() && !output_schema.is_object()) bad("output schema must be a JSON object"); + if (effort.size() > 16) bad("effort is at most 16 characters"); + if (!additional_fields.is_object()) bad("additionalModelRequestFields must be an object"); +} + +Json ConverseRequest::to_json(bool for_stream) const { + validate(for_stream); + Json body = Json::object(); + Json msgs = Json::array(); + for (const Json& m : messages) msgs.push(m); + body.set("messages", msgs); + if (!system.empty()) { + Json s = Json::array(); + for (const Json& b : system) s.push(b); + body.set("system", s); + } + Json inf = Json::object(); + if (max_tokens) inf.set("maxTokens", Json(*max_tokens)); + if (temperature) inf.set("temperature", Json(*temperature)); + if (top_p) inf.set("topP", Json(*top_p)); + if (!stop_sequences.empty()) { + Json s = Json::array(); + for (const std::string& x : stop_sequences) s.push(Json(x)); + inf.set("stopSequences", s); + } + if (!inf.as_object().empty()) body.set("inferenceConfig", inf); + if (!tools.empty()) { + Json cfg = Json::object(); + Json t = Json::array(); + for (const Json& x : tools) t.push(x); + cfg.set("tools", t); + if (!tool_choice.empty()) { + Json choice = Json::object(); + if (tool_choice == "auto") choice.set("auto", Json::object()); + else if (tool_choice == "any") choice.set("any", Json::object()); + else { + Json n = Json::object(); + n.set("name", Json(tool_choice)); + choice.set("tool", n); + } + cfg.set("toolChoice", choice); + } + body.set("toolConfig", cfg); + } + if (!guardrail_id.empty()) { + Json g = Json::object(); + g.set("guardrailIdentifier", Json(guardrail_id)); + g.set("guardrailVersion", Json(guardrail_version)); + if (!guardrail_trace.empty()) g.set("trace", Json(guardrail_trace)); + if (for_stream && !guardrail_stream_mode.empty()) g.set("streamProcessingMode", Json(guardrail_stream_mode)); + body.set("guardrailConfig", g); + } + if (!additional_fields.as_object().empty()) body.set("additionalModelRequestFields", additional_fields); + if (!additional_response_paths.empty()) { + Json p = Json::array(); + for (const std::string& x : additional_response_paths) p.push(Json(x)); + body.set("additionalModelResponseFieldPaths", p); + } + if (!request_metadata.empty()) { + Json m = Json::object(); + for (const auto& [k, v] : request_metadata) m.set(k, Json(v)); + body.set("requestMetadata", m); + } + if (!latency.empty()) { + Json p = Json::object(); + p.set("latency", Json(latency)); + body.set("performanceConfig", p); + } + if (!service_tier.empty()) { + Json t = Json::object(); + t.set("type", Json(service_tier)); + body.set("serviceTier", t); + } + Json out = Json::object(); + if (output_schema.is_object()) { + Json def = Json::object(); + def.set("schema", Json(output_schema.dump())); // the schema travels as a JSON string + def.set("name", Json(output_schema_name)); + Json structure = Json::object(); + structure.set("jsonSchema", def); + Json fmt = Json::object(); + fmt.set("type", Json("json_schema")); + fmt.set("structure", structure); + out.set("textFormat", fmt); + } + if (!effort.empty()) out.set("effort", Json(effort)); + if (!out.as_object().empty()) body.set("outputConfig", out); + return body; +} + +Json ConverseRequest::to_count_json() const { + Json full = to_json(false); + Json c = Json::object(); + for (const char* k : {"messages", "system", "toolConfig", "additionalModelRequestFields"}) + if (const Json* v = full.find(k)) c.set(k, *v); + Json converse = Json::object(); + converse.set("converse", c); + return converse; +} + +// ---- result --------------------------------------------------------------- + +ConverseResult::ConverseResult(Json raw) : raw_(std::move(raw)) { + if (const Json* u = raw_.is_object() ? raw_.find("usage") : nullptr) usage_ = ConverseUsage::from_json(*u); +} + +const Json* ConverseResult::content() const { + const Json* out = raw_.is_object() ? raw_.find("output") : nullptr; + const Json* msg = out && out->is_object() ? out->find("message") : nullptr; + const Json* c = msg && msg->is_object() ? msg->find("content") : nullptr; + return c && c->is_array() ? c : nullptr; +} + +std::string ConverseResult::stop_reason() const { return str(raw_.find("stopReason")); } + +long ConverseResult::latency_ms() const { + const Json* m = raw_.find("metrics"); + const Json* l = m && m->is_object() ? m->find("latencyMs") : nullptr; + return l && l->is_number() ? l->as_int() : 0; +} + +std::string ConverseResult::text() const { + std::string out; + if (const Json* c = content()) + for (const Json& b : c->as_array()) + if (const Json* t = b.find("text"); t && t->is_string()) out += t->as_string(); + return out; +} + +std::string ConverseResult::reasoning() const { + std::string out; + if (const Json* c = content()) + for (const Json& b : c->as_array()) + if (const Json* r = b.find("reasoningContent")) + if (const Json* rt = r->find("reasoningText")) out += str(rt->find("text")); + return out; +} + +std::string ConverseResult::reasoning_signature() const { + if (const Json* c = content()) + for (const Json& b : c->as_array()) + if (const Json* r = b.find("reasoningContent")) + if (const Json* rt = r->find("reasoningText")) + if (std::string s = str(rt->find("signature")); !s.empty()) return s; + return ""; +} + +std::vector ConverseResult::tool_uses() const { + std::vector out; + if (const Json* c = content()) + for (const Json& b : c->as_array()) + if (const Json* t = b.find("toolUse")) { + ConverseToolUse u; + u.id = str(t->find("toolUseId")); + u.name = str(t->find("name")); + if (const Json* in = t->find("input")) u.input = *in; + out.push_back(std::move(u)); + } + return out; +} + +Json ConverseResult::assistant_message() const { + if (const Json* out = raw_.find("output")) + if (const Json* m = out->find("message")) return *m; + return Json::object(); +} + +Json ConverseResult::trace() const { + const Json* t = raw_.find("trace"); + return t ? *t : Json(); +} + +// ---- stream --------------------------------------------------------------- + +StreamDelta ConverseStreamAccumulator::on_event(const std::string& event_type, const Json& p) { + ++events_; + StreamDelta d; + d.event = event_type; + const Json* idx = p.is_object() ? p.find("contentBlockIndex") : nullptr; + if (idx && idx->is_number()) d.block_index = static_cast(idx->as_int()); + if (event_type == "messageStart") { + if (std::string r = str(p.find("role")); !r.empty()) role_ = r; + } else if (event_type == "contentBlockStart") { + const Json* start = p.find("start"); + const Json* tu = start ? start->find("toolUse") : nullptr; + if (tu && d.block_index >= 0) { + Block& b = blocks_[d.block_index]; + b.kind = "tool"; + b.tool_id = d.tool_id = str(tu->find("toolUseId")); + b.tool_name = d.tool_name = str(tu->find("name")); + } + } else if (event_type == "contentBlockDelta") { + const Json* delta = p.find("delta"); + if (delta && d.block_index >= 0) { + Block& b = blocks_[d.block_index]; + if (const Json* t = delta->find("text")) { + if (b.kind.empty()) b.kind = "text"; + b.text += d.text = str(t); + } else if (const Json* tu = delta->find("toolUse")) { + b.kind = "tool"; + b.tool_args += d.tool_args = str(tu->find("input")); + } else if (const Json* rc = delta->find("reasoningContent")) { + b.kind = "reasoning"; + if (const Json* rt = rc->find("text")) b.text += d.reasoning = str(rt); + if (const Json* sg = rc->find("signature")) b.signature += d.reasoning_signature = str(sg); + } + } + } else if (event_type == "messageStop") { + stop_reason_ = d.stop_reason = str(p.find("stopReason")); + } else if (event_type == "metadata") { + if (const Json* u = p.find("usage")) { + usage_ = *u; + d.usage = ConverseUsage::from_json(*u); + } + if (const Json* m = p.find("metrics")) metrics_ = *m; + if (const Json* t = p.find("trace")) trace_ = *t; + } + return d; +} + +ConverseResult ConverseStreamAccumulator::result() const { + Json content = Json::array(); + for (const auto& [index, b] : blocks_) { + (void)index; + if (b.kind == "tool") { + Json input = Json::object(); + if (!b.tool_args.empty()) { + try { + input = Json::parse(b.tool_args); + } catch (const std::exception&) { + input = Json::object(); + input.set("_invalid_json", Json(b.tool_args)); + } + } + content.push(Blocks::tool_use(b.tool_id.empty() ? "tool" : b.tool_id, b.tool_name.empty() ? "tool" : b.tool_name, input)); + } else if (b.kind == "reasoning") { + Json rt = Json::object(); + rt.set("text", Json(b.text)); + if (!b.signature.empty()) rt.set("signature", Json(b.signature)); + Json rc = Json::object(); + rc.set("reasoningText", rt); + Json blk = Json::object(); + blk.set("reasoningContent", rc); + content.push(blk); + } else { + content.push(Blocks::text(b.text)); + } + } + Json msg = Json::object(); + msg.set("role", Json(role_)); + msg.set("content", content); + Json out = Json::object(); + out.set("message", msg); + Json raw = Json::object(); + raw.set("output", out); + raw.set("stopReason", Json(stop_reason_)); + raw.set("usage", usage_); + raw.set("metrics", metrics_); + if (!trace_.is_null()) raw.set("trace", trace_); + return ConverseResult(raw); +} + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/converse.h b/src/vendors/bedrock/converse.h new file mode 100644 index 0000000..7ec77ec --- /dev/null +++ b/src/vendors/bedrock/converse.h @@ -0,0 +1,149 @@ +#pragma once + +// Converse / ConverseStream: request builder, result, and the stream +// accumulator. Wire shapes are the bedrock-runtime service model +// (ConverseRequest, ConverseResponse, ConverseStreamOutput). + +#include +#include +#include +#include +#include + +#include "clients/body/json.h" + +namespace molo::bedrock { + +using Json = clients::body::Json; + +// Builders for the ContentBlock / SystemContentBlock / Tool unions. +struct Blocks { + static Json text(const std::string& text); + // format: png jpeg gif webp. Bytes are raw; they travel base64 encoded. + static Json image(const std::string& format, const std::string& bytes); + // format: pdf csv doc docx xls xlsx html txt md. name 1..200 chars. + static Json document(const std::string& format, const std::string& name, const std::string& bytes); + static Json tool_use(const std::string& id, const std::string& name, const Json& input); + static Json tool_result(const std::string& id, const std::string& text, bool is_error = false); + static Json tool_result_json(const std::string& id, const Json& value, bool is_error = false); + static Json cache_point(); + static Json message(const std::string& role, const std::vector& content); + static Json user(const std::string& text); + static Json assistant(const std::string& text); + // Tool union: {"toolSpec":{name, description, inputSchema:{json}}} + static Json tool(const std::string& name, const std::string& description, const Json& json_schema, + bool strict = false); +}; + +struct ConverseUsage { + int input_tokens = 0; + int output_tokens = 0; + int total_tokens = 0; + int cache_read_tokens = 0; + int cache_write_tokens = 0; + static ConverseUsage from_json(const Json& usage); +}; + +struct ConverseToolUse { + std::string id; + std::string name; + Json input = Json::object(); +}; + +class ConverseRequest { + public: + std::string model_id; // model id, inference profile id or ARN + std::vector messages; + std::vector system; + std::optional max_tokens; + std::optional temperature; // 0..1 + std::optional top_p; // 0..1 + std::vector stop_sequences; + std::vector tools; // Blocks::tool(...) + std::string tool_choice; // "", "auto", "any", or a tool name + Json additional_fields = Json::object(); // additionalModelRequestFields (model specific) + std::vector additional_response_paths; + std::string guardrail_id, guardrail_version, guardrail_trace; // trace: enabled disabled enabled_full + std::string guardrail_stream_mode; // stream only: sync | async + std::string latency; // "" | standard | optimized + std::string service_tier; // "" | priority | default | flex | reserved + std::map request_metadata; // max 16 + Json output_schema; // null, or a JSON Schema object: outputConfig.textFormat json_schema + std::string output_schema_name = "response"; + std::string effort; // outputConfig.effort (<=16 chars) + + ConverseRequest& add(Json message); + ConverseRequest& add_user(const std::string& text); + ConverseRequest& add_assistant(const std::string& text); + ConverseRequest& add_system(const std::string& text); + + // Throws ApiError::invalid (400 INVALID_ARGUMENT) naming the first rule broken. + void validate(bool for_stream = false) const; + // The JSON body (modelId is in the path, not here). + Json to_json(bool for_stream = false) const; + // The CountTokens `input.converse` body. + Json to_count_json() const; +}; + +class ConverseResult { + public: + ConverseResult() = default; + explicit ConverseResult(Json raw); + + const Json& raw() const { return raw_; } + std::string stop_reason() const; + ConverseUsage usage() const { return usage_; } + long latency_ms() const; + // All text blocks of the assistant message, concatenated. + std::string text() const; + std::string reasoning() const; + std::string reasoning_signature() const; + std::vector tool_uses() const; + // The assistant message as sent back in the next request (history). + Json assistant_message() const; + Json trace() const; + + private: + const Json* content() const; + Json raw_ = Json::object(); + ConverseUsage usage_; +}; + +// One decoded stream event, in neutral form. +struct StreamDelta { + std::string event; // messageStart contentBlockStart contentBlockDelta contentBlockStop messageStop metadata + int block_index = -1; + std::string text; + std::string reasoning; + std::string reasoning_signature; + std::string tool_id; // contentBlockStart for a tool + std::string tool_name; + std::string tool_args; // partial JSON text + std::string stop_reason; // messageStop + std::optional usage; // metadata +}; + +// Folds events into a ConverseResult that has the same shape as the +// non-streaming reply (output.message, stopReason, usage, metrics). +class ConverseStreamAccumulator { + public: + // `event_type` is the :event-type header, `payload` its JSON body. + StreamDelta on_event(const std::string& event_type, const Json& payload); + ConverseResult result() const; + std::size_t events() const { return events_; } + + private: + struct Block { + std::string kind; // text | tool | reasoning + std::string text, signature, tool_id, tool_name, tool_args; + }; + std::map blocks_; + std::string role_ = "assistant"; + std::string stop_reason_; + Json usage_ = Json::object(); + Json metrics_ = Json::object(); + Json trace_; + std::size_t events_ = 0; +}; + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/crc32.cpp b/src/vendors/bedrock/crc32.cpp new file mode 100644 index 0000000..013be2b --- /dev/null +++ b/src/vendors/bedrock/crc32.cpp @@ -0,0 +1,26 @@ +#include "vendors/bedrock/crc32.h" + +#include + +namespace molo::bedrock { + +namespace { +std::array make_table() { + std::array t{}; + for (std::uint32_t i = 0; i < 256; ++i) { + std::uint32_t c = i; + for (int k = 0; k < 8; ++k) c = (c & 1u) ? (0xEDB88320u ^ (c >> 1)) : (c >> 1); + t[i] = c; + } + return t; +} +} // namespace + +std::uint32_t Crc32::of(const std::uint8_t* data, std::size_t len, std::uint32_t seed) { + static const std::array table = make_table(); + std::uint32_t c = ~seed; + for (std::size_t i = 0; i < len; ++i) c = table[(c ^ data[i]) & 0xFFu] ^ (c >> 8); + return ~c; +} + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/crc32.h b/src/vendors/bedrock/crc32.h new file mode 100644 index 0000000..d7070e2 --- /dev/null +++ b/src/vendors/bedrock/crc32.h @@ -0,0 +1,22 @@ +#pragma once + +// CRC-32 (IEEE 802.3, reflected, polynomial 0xEDB88320): the checksum the +// vnd.amazon.eventstream framing uses for the prelude and for the message. + +#include +#include +#include + +namespace molo::bedrock { + +class Crc32 { + public: + // Check value: of("123456789") == 0xCBF43926. `seed` is a previous result, + // so a checksum can be built over several pieces. + static std::uint32_t of(const std::uint8_t* data, std::size_t len, std::uint32_t seed = 0); + static std::uint32_t of(const std::string& s, std::uint32_t seed = 0) { + return of(reinterpret_cast(s.data()), s.size(), seed); + } +}; + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/credentials.cpp b/src/vendors/bedrock/credentials.cpp new file mode 100644 index 0000000..c72d592 --- /dev/null +++ b/src/vendors/bedrock/credentials.cpp @@ -0,0 +1,95 @@ +#include "vendors/bedrock/credentials.h" + +#include "llm/config.h" +#include "llm/error.h" +#include "vendors/bedrock/caps.h" + +namespace molo::bedrock { + +const char* Endpoints::prefix(AwsService service) { + switch (service) { + case AwsService::kRuntime: return "bedrock-runtime"; + case AwsService::kControl: return "bedrock"; + case AwsService::kAgentRuntime: return "bedrock-agent-runtime"; + } + return "bedrock"; +} + +bool Endpoints::valid_region(const std::string& region) { + if (region.size() < 2 || region.size() > BedrockCaps::kMaxRegionChars) return false; + for (char c : region) + if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-')) return false; + return true; +} + +std::string Endpoints::host(AwsService service, const std::string& region) { + if (!valid_region(region)) throw llm::ApiError::invalid("bedrock", "bad region name"); + std::string suffix = region.rfind("cn-", 0) == 0 ? ".amazonaws.com.cn" : ".amazonaws.com"; + return std::string(prefix(service)) + "." + region + suffix; +} + +namespace { +std::string pick_region() { + std::string r = llm::Config::get(Credentials::kEnvRegion); + if (r.empty()) r = llm::Config::get(Credentials::kEnvDefaultRegion); + if (r.empty() || !Endpoints::valid_region(r)) r = Credentials::kDefaultRegion; + return r; +} +} // namespace + +Credentials Credentials::from_env() { + Credentials c; + c.region = pick_region(); + std::string bearer = llm::Config::get(kEnvBearer); + if (!bearer.empty()) { + c.mode = AuthMode::kBearer; + c.bearer = std::move(bearer); + // Keep SigV4 keys too: the agent runtime host does not take API keys. + } + AwsCredentials aws; + aws.access_key_id = llm::Config::get(kEnvAccessKey); + aws.secret_access_key = llm::Config::get(kEnvSecretKey); + aws.session_token = llm::Config::get(kEnvSessionToken); + if (aws.valid()) { + c.aws = std::move(aws); + if (c.mode == AuthMode::kNone) c.mode = AuthMode::kSigV4; + } + return c; +} + +Credentials Credentials::sigv4(AwsCredentials aws, std::string region) { + Credentials c; + c.mode = aws.valid() ? AuthMode::kSigV4 : AuthMode::kNone; + c.aws = std::move(aws); + c.region = std::move(region); + return c; +} + +Credentials Credentials::bearer_token(std::string token, std::string region) { + Credentials c; + c.mode = token.empty() ? AuthMode::kNone : AuthMode::kBearer; + c.bearer = std::move(token); + c.region = std::move(region); + return c; +} + +Credentials Credentials::none(std::string region) { + Credentials c; + c.region = std::move(region); + return c; +} + +const char* Credentials::mode_name() const { + switch (mode) { + case AuthMode::kNone: return "none"; + case AuthMode::kSigV4: return "sigv4"; + case AuthMode::kBearer: return "bearer"; + } + return "none"; +} + +std::string Credentials::env_hint() { + return std::string(kEnvBearer) + " or " + kEnvAccessKey + " and " + kEnvSecretKey; +} + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/credentials.h b/src/vendors/bedrock/credentials.h new file mode 100644 index 0000000..0b404e3 --- /dev/null +++ b/src/vendors/bedrock/credentials.h @@ -0,0 +1,60 @@ +#pragma once + +// Where the credential comes from and which host it talks to. +// +// AWS_BEARER_TOKEN_BEDROCK Bedrock API key, sent as "Authorization: Bearer" +// AWS_ACCESS_KEY_ID SigV4 access key +// AWS_SECRET_ACCESS_KEY SigV4 secret +// AWS_SESSION_TOKEN optional, signed as x-amz-security-token +// AWS_REGION region (then AWS_DEFAULT_REGION, then us-east-1) +// +// All are read through llm::Config (environment, then .env.json.local, then +// .env.json). When both a Bedrock API key and SigV4 keys exist the API key +// wins (the AWS SDKs do the same); the choice is reported by `mode_name()`. +// The secret never appears in an error, a log line or a URL. + +#include + +#include "vendors/bedrock/sigv4.h" + +namespace molo::bedrock { + +enum class AuthMode { kNone, kSigV4, kBearer }; + +// The three hosts the module talks to. Signing name is "bedrock" for all of +// them (service models: metadata.signingName). +enum class AwsService { kRuntime, kControl, kAgentRuntime }; + +struct Endpoints { + static const char* prefix(AwsService service); // bedrock-runtime, bedrock, bedrock-agent-runtime + static const char* signing_name() { return "bedrock"; } + static std::string host(AwsService service, const std::string& region); + static bool valid_region(const std::string& region); // [a-z0-9-], 2..32 +}; + +struct Credentials { + static constexpr const char* kEnvBearer = "AWS_BEARER_TOKEN_BEDROCK"; + static constexpr const char* kEnvAccessKey = "AWS_ACCESS_KEY_ID"; + static constexpr const char* kEnvSecretKey = "AWS_SECRET_ACCESS_KEY"; + static constexpr const char* kEnvSessionToken = "AWS_SESSION_TOKEN"; + static constexpr const char* kEnvRegion = "AWS_REGION"; + static constexpr const char* kEnvDefaultRegion = "AWS_DEFAULT_REGION"; + static constexpr const char* kDefaultRegion = "us-east-1"; + + AuthMode mode = AuthMode::kNone; + AwsCredentials aws; + std::string bearer; + std::string region = kDefaultRegion; + + static Credentials from_env(); + static Credentials sigv4(AwsCredentials aws, std::string region = kDefaultRegion); + static Credentials bearer_token(std::string token, std::string region = kDefaultRegion); + static Credentials none(std::string region = kDefaultRegion); + + bool configured() const { return mode != AuthMode::kNone; } + const char* mode_name() const; + // Human text naming every variable that would configure the module. + static std::string env_hint(); +}; + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/event_stream.cpp b/src/vendors/bedrock/event_stream.cpp new file mode 100644 index 0000000..90594c2 --- /dev/null +++ b/src/vendors/bedrock/event_stream.cpp @@ -0,0 +1,192 @@ +#include "vendors/bedrock/event_stream.h" + +#include "clients/body/json.h" +#include "llm/error.h" +#include "vendors/bedrock/caps.h" +#include "vendors/bedrock/crc32.h" + +namespace molo::bedrock { + +namespace { + +std::uint32_t be32(const std::string& s, std::size_t at) { + return (static_cast(static_cast(s[at])) << 24) | + (static_cast(static_cast(s[at + 1])) << 16) | + (static_cast(static_cast(s[at + 2])) << 8) | + static_cast(static_cast(s[at + 3])); +} + +std::uint64_t be_n(const std::string& s, std::size_t at, int n) { + std::uint64_t v = 0; + for (int i = 0; i < n; ++i) v = (v << 8) | static_cast(s[at + static_cast(i)]); + return v; +} + +void put32(std::string& out, std::uint32_t v) { + for (int shift = 24; shift >= 0; shift -= 8) out.push_back(static_cast((v >> shift) & 0xFF)); +} + +[[noreturn]] void fail(const std::string& vendor, const std::string& type, const std::string& why) { + throw llm::ApiError(vendor, 502, type, why); +} + +void parse_headers(const std::string& vendor, const std::string& block, EventMessage& out) { + std::size_t at = 0; + auto need = [&](std::size_t n) { + if (at + n > block.size()) fail(vendor, "EVENTSTREAM_HEADERS", "header block is truncated"); + }; + while (at < block.size()) { + need(1); + std::size_t name_len = static_cast(block[at++]); + need(name_len + 1); + std::string name = block.substr(at, name_len); + at += name_len; + int type = static_cast(block[at++]); + EventHeader h; + h.type = type; + switch (type) { + case 0: h.number = 1; break; + case 1: h.number = 0; break; + case 2: need(1); h.number = static_cast(block[at]); at += 1; break; + case 3: need(2); h.number = static_cast(be_n(block, at, 2)); at += 2; break; + case 4: need(4); h.number = static_cast(be_n(block, at, 4)); at += 4; break; + case 5: + case 8: need(8); h.number = static_cast(be_n(block, at, 8)); at += 8; break; + case 6: + case 7: { + need(2); + std::size_t len = static_cast(be_n(block, at, 2)); + at += 2; + need(len); + h.text = block.substr(at, len); + at += len; + break; + } + case 9: need(16); h.text = block.substr(at, 16); at += 16; break; + default: fail(vendor, "EVENTSTREAM_HEADERS", "unknown header value type " + std::to_string(type)); + } + out.headers[name] = std::move(h); + } +} + +} // namespace + +std::string EventMessage::header(const std::string& name) const { + auto it = headers.find(name); + return it == headers.end() ? std::string() : it->second.text; +} + +EventStreamDecoder::EventStreamDecoder(std::string vendor, Sink sink) + : vendor_(std::move(vendor)), sink_(std::move(sink)) {} + +bool EventStreamDecoder::feed(const std::string& chunk) { + if (stopped_) return false; + total_ += chunk.size(); + if (total_ > BedrockCaps::kMaxStreamBytes) fail(vendor_, "STREAM_TOO_LARGE", "stream over the byte cap"); + buffer_ += chunk; + if (!started_ && !buffer_.empty()) { + started_ = true; + // A real frame starts with a 4 byte length; a JSON error body starts with '{'. + // 0x7B in the first length byte would mean a message over 2 GiB, far past the cap. + if (buffer_[0] == '{') json_error_ = true; + } + if (json_error_) { + if (buffer_.size() > 1024u * 1024u) fail(vendor_, "BAD_RESPONSE", "error body over 1 MiB"); + return true; + } + return drain(); +} + +bool EventStreamDecoder::drain() { + while (buffer_.size() >= 12) { + std::uint32_t total = be32(buffer_, 0); + std::uint32_t headers_len = be32(buffer_, 4); + std::uint32_t prelude_crc = be32(buffer_, 8); + if (Crc32::of(buffer_.substr(0, 8)) != prelude_crc) + fail(vendor_, "EVENTSTREAM_CRC", "prelude CRC mismatch"); + if (total < 16 + headers_len) fail(vendor_, "EVENTSTREAM_LENGTH", "message shorter than its headers"); + if (total > BedrockCaps::kMaxEventMessageBytes) fail(vendor_, "EVENTSTREAM_LENGTH", "message over the size cap"); + if (headers_len > BedrockCaps::kMaxEventHeaderBytes) fail(vendor_, "EVENTSTREAM_LENGTH", "headers over the size cap"); + if (buffer_.size() < total) return true; + std::uint32_t message_crc = be32(buffer_, total - 4); + if (Crc32::of(buffer_.substr(0, total - 4)) != message_crc) + fail(vendor_, "EVENTSTREAM_CRC", "message CRC mismatch"); + EventMessage msg; + parse_headers(vendor_, buffer_.substr(12, headers_len), msg); + msg.payload = buffer_.substr(12 + headers_len, total - 16 - headers_len); + buffer_.erase(0, total); + if (++events_ > BedrockCaps::kMaxStreamEvents) fail(vendor_, "STREAM_TOO_LONG", "too many events"); + if (!sink_(msg)) { + stopped_ = true; + return false; + } + } + return true; +} + +void EventStreamDecoder::finish() { + if (stopped_) return; + if (json_error_) { + std::string message = "the server answered with an error document instead of a stream"; + std::string type = "STREAM_HTTP_ERROR"; + try { + clients::body::Json doc = clients::body::Json::parse(buffer_); + for (const char* key : {"message", "Message"}) + if (const auto* m = doc.find(key); m && m->is_string()) { + message = m->as_string(); + break; + } + if (const auto* t = doc.find("__type"); t && t->is_string()) type = t->as_string(); + } catch (const std::exception&) { + } + // The transport hides the HTTP status of a stream; 502 marks "not a stream". + throw llm::ApiError(vendor_, 502, type, message); + } + if (!buffer_.empty()) fail(vendor_, "EVENTSTREAM_TRUNCATED", "stream ended inside a message"); +} + +std::string EventStreamDecoder::encode(const std::map& string_headers, + const std::string& payload) { + std::string headers; + for (const auto& [name, value] : string_headers) { + headers.push_back(static_cast(name.size())); + headers += name; + headers.push_back(7); + headers.push_back(static_cast((value.size() >> 8) & 0xFF)); + headers.push_back(static_cast(value.size() & 0xFF)); + headers += value; + } + std::string out; + put32(out, static_cast(16 + headers.size() + payload.size())); + put32(out, static_cast(headers.size())); + put32(out, Crc32::of(out)); + out += headers; + out += payload; + put32(out, Crc32::of(out)); + return out; +} + +void EventStreamDecoder::raise_if_failure(const std::string& vendor, const EventMessage& message) { + if (!message.is_failure()) return; + std::string type = message.message_type() == "exception" ? message.exception_type() : message.header(":error-code"); + std::string text = message.header(":error-message"); + try { + clients::body::Json doc = clients::body::Json::parse(message.payload); + if (const auto* m = doc.find("message"); m && m->is_string()) text = m->as_string(); + } catch (const std::exception&) { + if (text.empty()) text = message.payload.substr(0, 512); + } + if (type.empty()) type = "EVENTSTREAM_ERROR"; + int status = 502; + if (type == "throttlingException") status = 429; + else if (type == "validationException") status = 400; + else if (type == "accessDeniedException") status = 403; + else if (type == "resourceNotFoundException") status = 404; + else if (type == "serviceUnavailableException") status = 503; + else if (type == "internalServerException") status = 500; + else if (type == "modelTimeoutException") status = 408; + else if (type == "modelStreamErrorException") status = 424; + throw llm::ApiError(vendor, status, type, text); +} + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/event_stream.h b/src/vendors/bedrock/event_stream.h new file mode 100644 index 0000000..f70cd4d --- /dev/null +++ b/src/vendors/bedrock/event_stream.h @@ -0,0 +1,77 @@ +#pragma once + +// Decoder for the application/vnd.amazon.eventstream binary framing used by +// ConverseStream, InvokeModelWithResponseStream and InvokeAgent. +// +// Message layout (all integers big endian): +// total length (4) | headers length (4) | prelude CRC32 (4, over the first 8 bytes) +// headers | payload | message CRC32 (4, over everything before it) +// Header: name length (1) | name | value type (1) | value +// types: 0 true, 1 false, 2 byte, 3 int16, 4 int32, 5 int64, 6 bytes (len16), +// 7 string (len16), 8 timestamp (ms, int64), 9 uuid (16 bytes) +// +// Both CRCs are checked before a message is delivered. A plain JSON body where +// a stream was expected (an HTTP error, the transport does not expose the +// status of a stream) is collected and thrown from finish() as an ApiError. + +#include +#include +#include +#include + +namespace molo::bedrock { + +struct EventHeader { + int type = 7; + std::string text; // type 7 (string), 6 (raw bytes), 9 (raw uuid) + std::int64_t number = 0; // types 0-5, 8 +}; + +struct EventMessage { + std::map headers; + std::string payload; + + std::string header(const std::string& name) const; + std::string message_type() const { return header(":message-type"); } // event | exception | error + std::string event_type() const { return header(":event-type"); } + std::string exception_type() const { return header(":exception-type"); } + bool is_event() const { return message_type() == "event"; } + bool is_failure() const { return message_type() == "exception" || message_type() == "error"; } +}; + +class EventStreamDecoder { + public: + using Sink = std::function; // false: stop + + EventStreamDecoder(std::string vendor, Sink sink); + + // Returns false when the sink asked to stop. Throws ApiError on a CRC + // mismatch, an impossible length, a cap hit or a corrupt header block. + bool feed(const std::string& chunk); + // Throws when bytes of a message are left over (truncated stream) or when + // the body was a JSON error document instead of a stream. + void finish(); + + std::size_t events() const { return events_; } + + // Encodes one message (used by tests and by anything that must build frames). + static std::string encode(const std::map& string_headers, + const std::string& payload); + + // Throws ApiError for an exception / error message; no-op for events. + static void raise_if_failure(const std::string& vendor, const EventMessage& message); + + private: + bool drain(); + + std::string vendor_; + Sink sink_; + std::string buffer_; + std::size_t total_ = 0; + std::size_t events_ = 0; + bool stopped_ = false; + bool json_error_ = false; + bool started_ = false; +}; + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/runtime.cpp b/src/vendors/bedrock/runtime.cpp new file mode 100644 index 0000000..23ec6df --- /dev/null +++ b/src/vendors/bedrock/runtime.cpp @@ -0,0 +1,242 @@ +#include "vendors/bedrock/runtime.h" + +#include "security/crypto/Base64.h" +#include "vendors/bedrock/caps.h" + +namespace molo::bedrock { + +namespace { + +[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("bedrock", why); } + +bool one_of(const std::string& v, std::initializer_list allowed) { + for (const char* a : allowed) + if (v == a) return true; + return false; +} + +// Drives an event-stream call: every decoded message goes to `on_message`. +void run_stream(BedrockClient& client, const CallInput& input, + const std::function& on_message) { + EventStreamDecoder decoder(BedrockClient::vendor(), [&](const EventMessage& m) { + EventStreamDecoder::raise_if_failure(BedrockClient::vendor(), m); + return on_message(m); + }); + client.stream(input, [&](const std::string& chunk) { return decoder.feed(chunk); }); + decoder.finish(); +} + +Json parse_payload(const EventMessage& m) { + if (m.payload.empty()) return Json::object(); + try { + return Json::parse(m.payload); + } catch (const std::exception& e) { + throw llm::ApiError(BedrockClient::vendor(), 502, "BAD_RESPONSE", std::string("event payload: ") + e.what()); + } +} + +void add_invoke_headers(CallInput& in, const InvokeOptions& o, bool stream) { + in.content_type = o.content_type; + in.headers.emplace_back("Content-Type", o.content_type); + if (!o.accept.empty()) in.headers.emplace_back(stream ? "X-Amzn-Bedrock-Accept" : "Accept", o.accept); + if (!o.trace.empty()) in.headers.emplace_back("X-Amzn-Bedrock-Trace", o.trace); + if (!o.guardrail_id.empty()) { + in.headers.emplace_back("X-Amzn-Bedrock-GuardrailIdentifier", o.guardrail_id); + in.headers.emplace_back("X-Amzn-Bedrock-GuardrailVersion", o.guardrail_version); + } + if (!o.latency.empty()) in.headers.emplace_back("X-Amzn-Bedrock-PerformanceConfig-Latency", o.latency); + if (!o.service_tier.empty()) in.headers.emplace_back("X-Amzn-Bedrock-Service-Tier", o.service_tier); +} + +} // namespace + +void InvokeOptions::validate() const { + if (content_type.empty() || content_type.find_first_of("\r\n") != std::string::npos) bad("bad content type"); + if (accept.find_first_of("\r\n") != std::string::npos) bad("bad accept"); + if (!trace.empty() && !one_of(trace, {"ENABLED", "DISABLED", "ENABLED_FULL"})) bad("trace must be ENABLED, DISABLED or ENABLED_FULL"); + if (!guardrail_id.empty() && guardrail_version.empty()) bad("guardrail version is required with a guardrail"); + if (guardrail_id.find_first_of("\r\n") != std::string::npos || guardrail_version.find_first_of("\r\n") != std::string::npos) + bad("bad guardrail header"); + if (!latency.empty() && !one_of(latency, {"standard", "optimized"})) bad("latency must be standard or optimized"); + if (!service_tier.empty() && !one_of(service_tier, {"priority", "default", "flex", "reserved"})) + bad("service tier must be priority, default, flex or reserved"); +} + +Json InvokeResult::json() const { + try { + return Json::parse(body); + } catch (const std::exception& e) { + throw llm::ApiError(BedrockClient::vendor(), 502, "BAD_RESPONSE", e.what()); + } +} + +bool GuardrailResult::intervened() const { return action() == "GUARDRAIL_INTERVENED"; } + +std::string GuardrailResult::action() const { + const Json* a = raw.is_object() ? raw.find("action") : nullptr; + return a && a->is_string() ? a->as_string() : ""; +} + +std::string GuardrailResult::output_text() const { + std::string out; + const Json* o = raw.is_object() ? raw.find("outputs") : nullptr; + if (o && o->is_array()) + for (const Json& x : o->as_array()) + if (const Json* t = x.find("text"); t && t->is_string()) out += t->as_string(); + return out; +} + +ConverseResult Runtime::converse(const ConverseRequest& request) { + Json body = request.to_json(false); + check_id("model", request.model_id); + CallInput in; + in.call = Call::kConverse; + in.path["modelId"] = request.model_id; + in.json = &body; + return ConverseResult(client_->json(in)); +} + +ConverseResult Runtime::converse_stream(const ConverseRequest& request, + const std::function& sink) { + Json body = request.to_json(true); + check_id("model", request.model_id); + CallInput in; + in.call = Call::kConverseStream; + in.path["modelId"] = request.model_id; + in.json = &body; + ConverseStreamAccumulator acc; + run_stream(*client_, in, [&](const EventMessage& m) { + if (!m.is_event()) return true; + return sink(acc.on_event(m.event_type(), parse_payload(m))); + }); + return acc.result(); +} + +InvokeResult Runtime::invoke_model_raw(const std::string& model_id, const std::string& body, const InvokeOptions& options) { + options.validate(); + check_id("model", model_id); + CallInput in; + in.call = Call::kInvokeModel; + in.path["modelId"] = model_id; + in.raw = &body; + add_invoke_headers(in, options, false); + Reply r = client_->raw(in); + InvokeResult out; + out.body = std::move(r.body); + out.content_type = r.content_type; + return out; +} + +InvokeResult Runtime::invoke_model(const std::string& model_id, const Json& body, const InvokeOptions& options) { + if (!body.is_object()) bad("an InvokeModel body must be a JSON object"); + return invoke_model_raw(model_id, body.dump(), options); +} + +std::size_t Runtime::invoke_model_stream(const std::string& model_id, const Json& body, + const std::function& sink, + const InvokeOptions& options) { + options.validate(); + if (!body.is_object()) bad("an InvokeModel body must be a JSON object"); + check_id("model", model_id); + std::string raw = body.dump(); + CallInput in; + in.call = Call::kInvokeModelStream; + in.path["modelId"] = model_id; + in.raw = &raw; + add_invoke_headers(in, options, true); + std::size_t chunks = 0; + run_stream(*client_, in, [&](const EventMessage& m) { + if (!m.is_event() || m.event_type() != "chunk") return true; + Json p = parse_payload(m); + const Json* b = p.find("bytes"); + std::string decoded; + if (!b || !b->is_string() || !security::crypto::Base64::decode(b->as_string(), decoded)) + throw llm::ApiError(BedrockClient::vendor(), 502, "BAD_RESPONSE", "chunk without valid base64 bytes"); + ++chunks; + return sink(decoded); + }); + return chunks; +} + +long Runtime::count_tokens(const ConverseRequest& request) { + Json input = request.to_count_json(); + check_id("model", request.model_id); + Json body = Json::object(); + body.set("input", input); + CallInput in; + in.call = Call::kCountTokens; + in.path["modelId"] = request.model_id; + in.json = &body; + Json r = client_->json(in); + const Json* n = r.find("inputTokens"); + if (!n || !n->is_number()) throw llm::ApiError(BedrockClient::vendor(), 502, "BAD_RESPONSE", "no inputTokens in reply"); + return n->as_int(); +} + +long Runtime::count_tokens_invoke(const std::string& model_id, const Json& model_body) { + check_id("model", model_id); + if (!model_body.is_object()) bad("an InvokeModel body must be a JSON object"); + Json invoke = Json::object(); + // The blob travels base64 encoded in this JSON field. + invoke.set("body", Json(security::crypto::Base64::encode(model_body.dump()))); + Json input = Json::object(); + input.set("invokeModel", invoke); + Json body = Json::object(); + body.set("input", input); + CallInput in; + in.call = Call::kCountTokens; + in.path["modelId"] = model_id; + in.json = &body; + Json r = client_->json(in); + const Json* n = r.find("inputTokens"); + if (!n || !n->is_number()) throw llm::ApiError(BedrockClient::vendor(), 502, "BAD_RESPONSE", "no inputTokens in reply"); + return n->as_int(); +} + +Json Runtime::guardrail_text(const std::string& text, const std::vector& qualifiers) { + Json t = Json::object(); + t.set("text", Json(text)); + if (!qualifiers.empty()) { + Json q = Json::array(); + for (const std::string& x : qualifiers) { + if (!one_of(x, {"grounding_source", "query", "guard_content"})) bad("qualifier must be grounding_source, query or guard_content"); + q.push(Json(x)); + } + t.set("qualifiers", q); + } + Json b = Json::object(); + b.set("text", t); + return b; +} + +GuardrailResult Runtime::apply_guardrail(const std::string& guardrail_id, const std::string& version, + const std::string& source, const std::vector& blocks, + const std::string& scope) { + check_id("guardrail", guardrail_id); + check_id("guardrail version", version); + if (!one_of(source, {"INPUT", "OUTPUT"})) bad("source must be INPUT or OUTPUT"); + if (blocks.empty()) bad("at least one content block is required"); + if (blocks.size() > BedrockCaps::kMaxGuardrailBlocks) bad("too many content blocks"); + if (!scope.empty() && !one_of(scope, {"INTERVENTIONS", "FULL"})) bad("outputScope must be INTERVENTIONS or FULL"); + Json arr = Json::array(); + for (const Json& b : blocks) { + if (const Json* t = b.find("text"); t && t->is_object()) + if (const Json* s = t->find("text"); s && s->is_string() && s->as_string().size() > BedrockCaps::kMaxGuardrailTextChars) + bad("guardrail text too long"); + arr.push(b); + } + Json body = Json::object(); + body.set("source", Json(source)); + body.set("content", arr); + if (!scope.empty()) body.set("outputScope", Json(scope)); + CallInput in; + in.call = Call::kApplyGuardrail; + in.path["guardrailIdentifier"] = guardrail_id; + in.path["guardrailVersion"] = version; + in.json = &body; + GuardrailResult r; + r.raw = client_->json(in); + return r; +} + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/runtime.h b/src/vendors/bedrock/runtime.h new file mode 100644 index 0000000..da70223 --- /dev/null +++ b/src/vendors/bedrock/runtime.h @@ -0,0 +1,67 @@ +#pragma once + +// bedrock-runtime: Converse, ConverseStream, InvokeModel, +// InvokeModelWithResponseStream, CountTokens, ApplyGuardrail. + +#include +#include +#include + +#include "vendors/bedrock/client.h" +#include "vendors/bedrock/converse.h" + +namespace molo::bedrock { + +struct InvokeOptions { + std::string content_type = "application/json"; + std::string accept = "application/json"; // "" leaves it out + std::string trace; // ENABLED | DISABLED | ENABLED_FULL + std::string guardrail_id, guardrail_version; + std::string latency; // standard | optimized + std::string service_tier; // priority | default | flex | reserved + void validate() const; +}; + +struct InvokeResult { + std::string body; // the model's own format + std::string content_type; + Json json() const; // body parsed (throws ApiError BAD_RESPONSE when it is not JSON) +}; + +struct GuardrailResult { + Json raw; + bool intervened() const; + std::string action() const; + std::string output_text() const; // outputs[].text joined +}; + +class Runtime : public BedrockService { + public: + using BedrockService::BedrockService; + + ConverseResult converse(const ConverseRequest& request); + // sink returns false to stop early. Returns the accumulated result (complete + // only when the stream ran to its end). + ConverseResult converse_stream(const ConverseRequest& request, + const std::function& sink); + + InvokeResult invoke_model(const std::string& model_id, const Json& body, const InvokeOptions& options = {}); + InvokeResult invoke_model_raw(const std::string& model_id, const std::string& body, const InvokeOptions& options); + // Each chunk is decoded from {"bytes": base64} to the model's own streamed JSON text. + // Returns the number of chunks delivered. + std::size_t invoke_model_stream(const std::string& model_id, const Json& body, + const std::function& sink, + const InvokeOptions& options = {}); + + // Input tokens for a Converse-shaped request (no inference, no cost). + long count_tokens(const ConverseRequest& request); + long count_tokens_invoke(const std::string& model_id, const Json& body); + + // source: INPUT | OUTPUT. blocks: GuardrailContentBlock unions. scope: "" | INTERVENTIONS | FULL. + GuardrailResult apply_guardrail(const std::string& guardrail_id, const std::string& version, + const std::string& source, const std::vector& blocks, + const std::string& scope = ""); + static Json guardrail_text(const std::string& text, const std::vector& qualifiers = {}); +}; + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/sigv4.cpp b/src/vendors/bedrock/sigv4.cpp new file mode 100644 index 0000000..8afe3d0 --- /dev/null +++ b/src/vendors/bedrock/sigv4.cpp @@ -0,0 +1,237 @@ +#include "vendors/bedrock/sigv4.h" + +#include +#include +#include + +#include "llm/error.h" +#include "security/crypto/Hmac.h" +#include "security/crypto/Sha256.h" +#include "vendors/bedrock/caps.h" + +namespace molo::bedrock { + +namespace crypto = molo::security::crypto; + +namespace { + +std::string lower(std::string s) { + for (char& c : s) c = static_cast(std::tolower(static_cast(c))); + return s; +} + +bool unreserved(unsigned char c) { + return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || + c == '-' || c == '_' || c == '.' || c == '~'; +} + +int hex_val(char c) { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + return -1; +} + +// Percent-decoding without throwing; a stray '%' stays literal, '+' stays '+'. +std::string decode(const std::string& text) { + std::string out; + out.reserve(text.size()); + for (std::size_t i = 0; i < text.size(); ++i) { + if (text[i] == '%' && i + 2 < text.size() + 0 && hex_val(text[i + 1]) >= 0 && + hex_val(text[i + 2]) >= 0) { + out.push_back(static_cast((hex_val(text[i + 1]) << 4) | hex_val(text[i + 2]))); + i += 2; + } else { + out.push_back(text[i]); + } + } + return out; +} + +std::string raw_string(const std::array& d) { + return std::string(reinterpret_cast(d.data()), d.size()); +} + +std::string sha_hex(const std::string& s) { return crypto::Sha256::hex(crypto::Sha256::hash(s)); } + +std::string trim_collapse(const std::string& v) { + std::string out; + bool space = false; + for (char c : v) { + if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { + space = !out.empty(); + continue; + } + if (space) out.push_back(' '); + space = false; + out.push_back(c); + } + return out; +} + +// Removes "." and ".." segments and repeated slashes; keeps a trailing slash. +std::string normalize_path(const std::string& path) { + if (path.empty()) return "/"; + std::vector parts; + std::string cur; + auto flush = [&] { + if (cur == "..") { + if (!parts.empty()) parts.pop_back(); + } else if (!cur.empty() && cur != ".") { + parts.push_back(cur); + } + cur.clear(); + }; + for (char c : path) { + if (c == '/') flush(); + else cur.push_back(c); + } + bool trailing = cur.empty() || cur == "." || cur == ".."; + flush(); + std::string out = "/"; + for (std::size_t i = 0; i < parts.size(); ++i) { + if (i) out.push_back('/'); + out += parts[i]; + } + if (trailing && out.size() > 1 && path.back() == '/') out.push_back('/'); + return out; +} + +} // namespace + +SigV4Signer::SigV4Signer(AwsCredentials credentials, std::string region, std::string service) + : credentials_(std::move(credentials)), region_(std::move(region)), service_(std::move(service)) { + if (!credentials_.valid()) + throw llm::ApiError::invalid("bedrock", "SigV4 needs an access key id and a secret access key"); +} + +std::string SigV4Signer::uri_encode(const std::string& text, bool keep_slash) { + static const char* kHex = "0123456789ABCDEF"; + std::string out; + out.reserve(text.size()); + for (char ch : text) { + unsigned char c = static_cast(ch); + if (unreserved(c) || (keep_slash && c == '/')) { + out.push_back(ch); + } else { + out.push_back('%'); + out.push_back(kHex[c >> 4]); + out.push_back(kHex[c & 0xF]); + } + } + return out; +} + +std::string SigV4Signer::canonical_uri(const std::string& path) { + return uri_encode(normalize_path(path), true); +} + +std::string SigV4Signer::canonical_query(const std::string& query) { + std::vector> params; + std::size_t pos = 0; + while (pos <= query.size() && !query.empty()) { + std::size_t amp = query.find('&', pos); + std::string pair = query.substr(pos, amp == std::string::npos ? std::string::npos : amp - pos); + pos = amp == std::string::npos ? query.size() + 1 : amp + 1; + if (pair.empty()) continue; + std::size_t eq = pair.find('='); + std::string k = eq == std::string::npos ? pair : pair.substr(0, eq); + std::string v = eq == std::string::npos ? "" : pair.substr(eq + 1); + params.emplace_back(uri_encode(decode(k), false), uri_encode(decode(v), false)); + if (params.size() > BedrockCaps::kMaxQueryParams) + throw llm::ApiError::invalid("bedrock", "too many query parameters to sign"); + } + std::sort(params.begin(), params.end()); + std::string out; + for (const auto& [k, v] : params) { + if (!out.empty()) out.push_back('&'); + out += k + "=" + v; + } + return out; +} + +std::string SigV4Signer::format_amz_date(std::time_t when) { + std::tm tm{}; + gmtime_r(&when, &tm); + char buf[32]; + std::strftime(buf, sizeof(buf), "%Y%m%dT%H%M%SZ", &tm); + return buf; +} + +std::string SigV4Signer::signing_key(const std::string& secret, const std::string& date8, + const std::string& region, const std::string& service) { + std::string k = raw_string(crypto::Hmac::sha256("AWS4" + secret, date8)); + k = raw_string(crypto::Hmac::sha256(k, region)); + k = raw_string(crypto::Hmac::sha256(k, service)); + return raw_string(crypto::Hmac::sha256(k, "aws4_request")); +} + +SigV4Result SigV4Signer::sign(const SigV4Request& request, const std::string& amz_date) const { + if (request.headers.size() > BedrockCaps::kMaxSignedHeaders) + throw llm::ApiError::invalid("bedrock", "too many headers to sign"); + if (request.path.size() > BedrockCaps::kMaxPathChars) + throw llm::ApiError::invalid("bedrock", "path too long to sign"); + + HeaderList headers = request.headers; + HeaderList added; + auto has = [&](const std::string& name) { + return std::any_of(headers.begin(), headers.end(), + [&](const auto& h) { return lower(h.first) == name; }); + }; + auto value_of = [&](const std::string& name) { + for (const auto& h : headers) + if (lower(h.first) == name) return trim_collapse(h.second); + return std::string(); + }; + if (!has("host")) throw llm::ApiError::invalid("bedrock", "SigV4 needs a host header"); + std::string date = amz_date; + if (has("x-amz-date")) { + date = value_of("x-amz-date"); + } else { + headers.emplace_back("x-amz-date", date); + added.emplace_back("x-amz-date", date); + } + if (date.size() != 16 || date[8] != 'T' || date.back() != 'Z') + throw llm::ApiError::invalid("bedrock", "x-amz-date must look like 20150830T123600Z"); + if (!credentials_.session_token.empty() && !has("x-amz-security-token")) { + headers.emplace_back("x-amz-security-token", credentials_.session_token); + added.emplace_back("x-amz-security-token", credentials_.session_token); + } + + // Canonical headers: repeated names are joined in order of appearance. + std::map canon; + for (const auto& [name, value] : headers) { + std::string n = lower(name); + std::string v = trim_collapse(value); + auto it = canon.find(n); + if (it == canon.end()) canon.emplace(n, v); + else it->second += "," + v; + } + std::string canonical_headers, signed_headers; + for (const auto& [n, v] : canon) { + canonical_headers += n + ":" + v + "\n"; + if (!signed_headers.empty()) signed_headers.push_back(';'); + signed_headers += n; + } + + SigV4Result r; + r.amz_date = date; + std::string date8 = date.substr(0, 8); + r.credential_scope = date8 + "/" + region_ + "/" + service_ + "/aws4_request"; + r.signed_headers = signed_headers; + r.canonical_request = request.method + "\n" + canonical_uri(request.path) + "\n" + + canonical_query(request.query) + "\n" + canonical_headers + "\n" + + signed_headers + "\n" + sha_hex(request.body); + r.string_to_sign = "AWS4-HMAC-SHA256\n" + date + "\n" + r.credential_scope + "\n" + + sha_hex(r.canonical_request); + std::string key = signing_key(credentials_.secret_access_key, date8, region_, service_); + r.signature = crypto::Hmac::hex(crypto::Hmac::sha256(key, r.string_to_sign)); + r.authorization = "AWS4-HMAC-SHA256 Credential=" + credentials_.access_key_id + "/" + + r.credential_scope + ", SignedHeaders=" + signed_headers + + ", Signature=" + r.signature; + added.emplace_back("Authorization", r.authorization); + r.added_headers = std::move(added); + return r; +} + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/sigv4.h b/src/vendors/bedrock/sigv4.h new file mode 100644 index 0000000..5562248 --- /dev/null +++ b/src/vendors/bedrock/sigv4.h @@ -0,0 +1,78 @@ +#pragma once + +// AWS Signature Version 4 (header signing), our own implementation over +// security::crypto Sha256 and Hmac. Verified against AWS's published SigV4 +// test suite (see SPEC.md ## Verified against spec). +// +// Rules implemented (docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv4-create-signed-request.html): +// - canonical URI: the path exactly as sent, dot segments and repeated +// slashes normalized, every byte outside the unreserved set (and '/') +// percent-encoded. A path that is already encoded is therefore encoded a +// second time, which is what every non-S3 service expects. +// - canonical query: names and values decoded, re-encoded, sorted by encoded +// name then value. A literal '+' is data, not a space. +// - canonical headers: lower-case names, values trimmed with inner runs of +// spaces collapsed, repeated names joined with ",", sorted by name. +// - every header in the request is signed; `host` is required; x-amz-date +// and x-amz-security-token are added when missing. + +#include +#include +#include +#include + +namespace molo::bedrock { + +struct AwsCredentials { + std::string access_key_id; + std::string secret_access_key; + std::string session_token; // optional + bool valid() const { return !access_key_id.empty() && !secret_access_key.empty(); } +}; + +using HeaderList = std::vector>; + +struct SigV4Request { + std::string method; // "GET", "POST", ... + std::string path; // as sent on the wire (already percent-encoded for real calls) + std::string query; // raw query string without '?' + HeaderList headers; // everything that will be sent and must be signed + std::string body; +}; + +struct SigV4Result { + std::string amz_date; // 20150830T123600Z + std::string credential_scope; // 20150830/us-east-1/service/aws4_request + std::string signed_headers; // host;x-amz-date + std::string canonical_request; + std::string string_to_sign; + std::string signature; // hex + std::string authorization; // full Authorization header value + HeaderList added_headers; // x-amz-date, x-amz-security-token, authorization (those not already present) +}; + +class SigV4Signer { + public: + SigV4Signer(AwsCredentials credentials, std::string region, std::string service); + + // `amz_date` is "YYYYMMDDTHHMMSSZ" and wins over an x-amz-date header only + // when the request has none; when the header is present its value is used. + SigV4Result sign(const SigV4Request& request, const std::string& amz_date) const; + + static std::string format_amz_date(std::time_t when); + static std::string signing_key(const std::string& secret, const std::string& date8, + const std::string& region, const std::string& service); + static std::string canonical_uri(const std::string& path); + static std::string canonical_query(const std::string& query); + static std::string uri_encode(const std::string& text, bool keep_slash); + + const std::string& region() const { return region_; } + const std::string& service() const { return service_; } + + private: + AwsCredentials credentials_; + std::string region_; + std::string service_; +}; + +} // namespace molo::bedrock diff --git a/src/vendors/bedrock/tools/check_spec.py b/src/vendors/bedrock/tools/check_spec.py new file mode 100755 index 0000000..a2e6fe2 --- /dev/null +++ b/src/vendors/bedrock/tools/check_spec.py @@ -0,0 +1,341 @@ +#!/usr/bin/env python3 +"""Check src/vendors/bedrock/calls.cpp against the AWS service models. + +AWS publishes the service definitions that generate its SDKs as JSON; the +botocore copy (boto/botocore, botocore/data///service-2.json) +is the pinned source here. Services read: bedrock (control plane), +bedrock-runtime, bedrock-agent-runtime. Exit 1 when: + + row a declared call's operation, HTTP method or request URI differs from + the model, or a path / query / header / body member it names is not + a member of the operation input (by location and location name); + required a required input member of a declared call is not covered by the row; + response the row's response schema is not the operation output (or, for an + event stream row, not the event-stream union of its output); + auth bearer_ok disagrees with `httpBearerAuth` in the model's auth list; + stream event_stream disagrees with the model (an eventstream output member); + schema a member we read (dotted paths descend) is absent from its shape; + coverage an operation of the three services is neither in the table nor in + EXCLUDED below, or an EXCLUDED entry names an operation that does + not exist or is also in the table. + +Python 3 stdlib only, no credentials, network required (or --dir DIR holding +bedrock.json, bedrock-runtime.json, bedrock-agent-runtime.json). + + python3 -I src/vendors/bedrock/tools/check_spec.py [--dir DIR] [alternative calls.cpp] +""" +import json +import os +import re +import sys +import urllib.request + +HERE = os.path.dirname(os.path.abspath(__file__)) +CALLS = os.path.join(HERE, "..", "calls.cpp") +BASE = "https://raw.githubusercontent.com/boto/botocore/develop/botocore/data/" +SERVICES = { + "bedrock": "bedrock/2023-04-20/service-2.json", + "bedrock-runtime": "bedrock-runtime/2023-09-30/service-2.json", + "bedrock-agent-runtime": "bedrock-agent-runtime/2023-07-26/service-2.json", +} +SERVICE_OF = {"R": "bedrock-runtime", "C": "bedrock", "A": "bedrock-agent-runtime"} + +# Operations the module does not wrap, each with the reason. +EXCLUDED = { + "bedrock-runtime": { + "GetAsyncInvoke": "async invoke (S3 output, Nova Reel video): not wrapped, see SPEC Gaps", + "InvokeGuardrailChecks": "newer guardrail checks API, shape not read: not wrapped, see SPEC Gaps", + "InvokeModelWithBidirectionalStream": "HTTP/2 bidirectional event stream (Nova Sonic): needs an h2 transport, not wrapped", + "ListAsyncInvokes": "async invoke (S3 output, Nova Reel video): not wrapped, see SPEC Gaps", + "StartAsyncInvoke": "async invoke (S3 output, Nova Reel video): not wrapped, see SPEC Gaps", + }, + "bedrock-agent-runtime": { + "AgenticRetrieveStream": "other agent-runtime inference calls: not wrapped, see SPEC Gaps", + "CheckIngestedDocumentAcl": "knowledge base document ACL and content: not wrapped", + "CreateInvocation": "sessions API (create, list, steps, end): not wrapped, see SPEC Gaps", + "CreateSession": "sessions API (create, list, steps, end): not wrapped, see SPEC Gaps", + "DeleteAgentMemory": "agent memory: not wrapped, see SPEC Gaps", + "DeleteSession": "sessions API (create, list, steps, end): not wrapped, see SPEC Gaps", + "EndSession": "sessions API (create, list, steps, end): not wrapped, see SPEC Gaps", + "GenerateQuery": "other agent-runtime inference calls: not wrapped, see SPEC Gaps", + "GetAgentMemory": "agent memory: not wrapped, see SPEC Gaps", + "GetDocumentContent": "knowledge base document ACL and content: not wrapped", + "GetExecutionFlowSnapshot": "flows runtime (invoke, executions, snapshots): not wrapped, see SPEC Gaps", + "GetFlowExecution": "flows runtime (invoke, executions, snapshots): not wrapped, see SPEC Gaps", + "GetIngestedDocumentAcl": "knowledge base document ACL and content: not wrapped", + "GetInvocationStep": "sessions API (create, list, steps, end): not wrapped, see SPEC Gaps", + "GetSession": "sessions API (create, list, steps, end): not wrapped, see SPEC Gaps", + "InvokeFlow": "flows runtime (invoke, executions, snapshots): not wrapped, see SPEC Gaps", + "InvokeInlineAgent": "other agent-runtime inference calls: not wrapped, see SPEC Gaps", + "ListFlowExecutionEvents": "flows runtime (invoke, executions, snapshots): not wrapped, see SPEC Gaps", + "ListFlowExecutions": "flows runtime (invoke, executions, snapshots): not wrapped, see SPEC Gaps", + "ListInvocationSteps": "sessions API (create, list, steps, end): not wrapped, see SPEC Gaps", + "ListInvocations": "sessions API (create, list, steps, end): not wrapped, see SPEC Gaps", + "ListSessions": "sessions API (create, list, steps, end): not wrapped, see SPEC Gaps", + "ListTagsForResource": "resource tags: not wrapped", + "OptimizePrompt": "other agent-runtime inference calls: not wrapped, see SPEC Gaps", + "PutInvocationStep": "sessions API (create, list, steps, end): not wrapped, see SPEC Gaps", + "Rerank": "other agent-runtime inference calls: not wrapped, see SPEC Gaps", + "RetrieveAndGenerate": "other agent-runtime inference calls: not wrapped, see SPEC Gaps", + "RetrieveAndGenerateStream": "other agent-runtime inference calls: not wrapped, see SPEC Gaps", + "StartFlowExecution": "flows runtime (invoke, executions, snapshots): not wrapped, see SPEC Gaps", + "StopFlowExecution": "flows runtime (invoke, executions, snapshots): not wrapped, see SPEC Gaps", + "TagResource": "resource tags: not wrapped", + "UntagResource": "resource tags: not wrapped", + "UpdateSession": "sessions API (create, list, steps, end): not wrapped, see SPEC Gaps", + }, + "bedrock": { + "BatchDeleteAdvancedPromptOptimizationJob": "prompt optimization jobs: out of scope", + "BatchDeleteEvaluationJob": "model evaluation jobs: out of scope", + "CancelAutomatedReasoningPolicyBuildWorkflow": "automated reasoning policies: management plane, out of scope", + "CreateAdvancedPromptOptimizationJob": "prompt optimization jobs: out of scope", + "CreateAutomatedReasoningPolicy": "automated reasoning policies: management plane, out of scope", + "CreateAutomatedReasoningPolicyTestCase": "automated reasoning policies: management plane, out of scope", + "CreateAutomatedReasoningPolicyVersion": "automated reasoning policies: management plane, out of scope", + "CreateCustomModel": "model customization, import and copy: training workflows, out of scope", + "CreateCustomModelDeployment": "model customization, import and copy: training workflows, out of scope", + "CreateEvaluationJob": "model evaluation jobs: out of scope", + "CreateFoundationModelAgreement": "model access and agreements: console workflow, out of scope", + "CreateGuardrail": "guardrail writes and enforced configuration: out of scope (list and get are wrapped)", + "CreateGuardrailVersion": "guardrail writes and enforced configuration: out of scope (list and get are wrapped)", + "CreateInferenceProfile": "inference profile writes: out of scope (list and get are wrapped)", + "CreateMarketplaceModelEndpoint": "marketplace model endpoints: out of scope", + "CreateModelCopyJob": "model customization, import and copy: training workflows, out of scope", + "CreateModelCustomizationJob": "model customization, import and copy: training workflows, out of scope", + "CreateModelImportJob": "model customization, import and copy: training workflows, out of scope", + "CreatePromptRouter": "prompt routers: out of scope", + "CreateProvisionedModelThroughput": "provisioned throughput purchases: out of scope", + "DeleteAutomatedReasoningPolicy": "automated reasoning policies: management plane, out of scope", + "DeleteAutomatedReasoningPolicyBuildWorkflow": "automated reasoning policies: management plane, out of scope", + "DeleteAutomatedReasoningPolicyTestCase": "automated reasoning policies: management plane, out of scope", + "DeleteCustomModel": "model customization, import and copy: training workflows, out of scope", + "DeleteCustomModelDeployment": "model customization, import and copy: training workflows, out of scope", + "DeleteEnforcedGuardrailConfiguration": "guardrail writes and enforced configuration: out of scope (list and get are wrapped)", + "DeleteFoundationModelAgreement": "model access and agreements: console workflow, out of scope", + "DeleteGuardrail": "guardrail writes and enforced configuration: out of scope (list and get are wrapped)", + "DeleteImportedModel": "model customization, import and copy: training workflows, out of scope", + "DeleteInferenceProfile": "inference profile writes: out of scope (list and get are wrapped)", + "DeleteMarketplaceModelEndpoint": "marketplace model endpoints: out of scope", + "DeleteModelInvocationLoggingConfiguration": "account configuration, policies and tags: out of scope", + "DeletePromptRouter": "prompt routers: out of scope", + "DeleteProvisionedModelThroughput": "provisioned throughput purchases: out of scope", + "DeleteResourcePolicy": "account configuration, policies and tags: out of scope", + "DeregisterMarketplaceModelEndpoint": "marketplace model endpoints: out of scope", + "ExportAutomatedReasoningPolicyVersion": "automated reasoning policies: management plane, out of scope", + "GetAccountDataRetention": "account configuration, policies and tags: out of scope", + "GetAdvancedPromptOptimizationJob": "prompt optimization jobs: out of scope", + "GetAutomatedReasoningPolicy": "automated reasoning policies: management plane, out of scope", + "GetAutomatedReasoningPolicyAnnotations": "automated reasoning policies: management plane, out of scope", + "GetAutomatedReasoningPolicyBuildWorkflow": "automated reasoning policies: management plane, out of scope", + "GetAutomatedReasoningPolicyBuildWorkflowResultAssets": "automated reasoning policies: management plane, out of scope", + "GetAutomatedReasoningPolicyNextScenario": "automated reasoning policies: management plane, out of scope", + "GetAutomatedReasoningPolicyTestCase": "automated reasoning policies: management plane, out of scope", + "GetAutomatedReasoningPolicyTestResult": "automated reasoning policies: management plane, out of scope", + "GetCustomModel": "model customization, import and copy: training workflows, out of scope", + "GetCustomModelDeployment": "model customization, import and copy: training workflows, out of scope", + "GetEvaluationJob": "model evaluation jobs: out of scope", + "GetFoundationModelAvailability": "model access and agreements: console workflow, out of scope", + "GetImportedModel": "model customization, import and copy: training workflows, out of scope", + "GetMarketplaceModelEndpoint": "marketplace model endpoints: out of scope", + "GetModelCopyJob": "model customization, import and copy: training workflows, out of scope", + "GetModelCustomizationJob": "model customization, import and copy: training workflows, out of scope", + "GetModelImportJob": "model customization, import and copy: training workflows, out of scope", + "GetModelInvocationLoggingConfiguration": "account configuration, policies and tags: out of scope", + "GetPromptRouter": "prompt routers: out of scope", + "GetProvisionedModelThroughput": "provisioned throughput purchases: out of scope", + "GetResourcePolicy": "account configuration, policies and tags: out of scope", + "GetUseCaseForModelAccess": "model access and agreements: console workflow, out of scope", + "ListAdvancedPromptOptimizationJobs": "prompt optimization jobs: out of scope", + "ListAutomatedReasoningPolicies": "automated reasoning policies: management plane, out of scope", + "ListAutomatedReasoningPolicyBuildWorkflows": "automated reasoning policies: management plane, out of scope", + "ListAutomatedReasoningPolicyTestCases": "automated reasoning policies: management plane, out of scope", + "ListAutomatedReasoningPolicyTestResults": "automated reasoning policies: management plane, out of scope", + "ListCustomModelDeployments": "model customization, import and copy: training workflows, out of scope", + "ListCustomModels": "model customization, import and copy: training workflows, out of scope", + "ListEnforcedGuardrailsConfiguration": "guardrail writes and enforced configuration: out of scope (list and get are wrapped)", + "ListEvaluationJobs": "model evaluation jobs: out of scope", + "ListFoundationModelAgreementOffers": "model access and agreements: console workflow, out of scope", + "ListImportedModels": "model customization, import and copy: training workflows, out of scope", + "ListMarketplaceModelEndpoints": "marketplace model endpoints: out of scope", + "ListModelCopyJobs": "model customization, import and copy: training workflows, out of scope", + "ListModelCustomizationJobs": "model customization, import and copy: training workflows, out of scope", + "ListModelImportJobs": "model customization, import and copy: training workflows, out of scope", + "ListPromptRouters": "prompt routers: out of scope", + "ListProvisionedModelThroughputs": "provisioned throughput purchases: out of scope", + "ListTagsForResource": "account configuration, policies and tags: out of scope", + "PutAccountDataRetention": "account configuration, policies and tags: out of scope", + "PutEnforcedGuardrailConfiguration": "guardrail writes and enforced configuration: out of scope (list and get are wrapped)", + "PutModelInvocationLoggingConfiguration": "account configuration, policies and tags: out of scope", + "PutResourcePolicy": "account configuration, policies and tags: out of scope", + "PutUseCaseForModelAccess": "model access and agreements: console workflow, out of scope", + "RegisterMarketplaceModelEndpoint": "marketplace model endpoints: out of scope", + "StartAutomatedReasoningPolicyBuildWorkflow": "automated reasoning policies: management plane, out of scope", + "StartAutomatedReasoningPolicyTestWorkflow": "automated reasoning policies: management plane, out of scope", + "StopAdvancedPromptOptimizationJob": "prompt optimization jobs: out of scope", + "StopEvaluationJob": "model evaluation jobs: out of scope", + "StopModelCustomizationJob": "model customization, import and copy: training workflows, out of scope", + "TagResource": "account configuration, policies and tags: out of scope", + "UntagResource": "account configuration, policies and tags: out of scope", + "UpdateAutomatedReasoningPolicy": "automated reasoning policies: management plane, out of scope", + "UpdateAutomatedReasoningPolicyAnnotations": "automated reasoning policies: management plane, out of scope", + "UpdateAutomatedReasoningPolicyTestCase": "automated reasoning policies: management plane, out of scope", + "UpdateCustomModelDeployment": "model customization, import and copy: training workflows, out of scope", + "UpdateGuardrail": "guardrail writes and enforced configuration: out of scope (list and get are wrapped)", + "UpdateMarketplaceModelEndpoint": "marketplace model endpoints: out of scope", + "UpdateProvisionedModelThroughput": "provisioned throughput purchases: out of scope", + }, +} + +ROW = re.compile( + r'\{Call::(\w+), (\w), ' + r', '.join(['"([^"]*)"'] * 8) + r', (true|false), (true|false), (true|false)\}' +) +SCHEMA = re.compile(r'\{"(\w+)", "([\w-]+)", "([^"]*)"\}') + + +def load(svc, directory): + if directory: + with open(os.path.join(directory, svc + ".json"), encoding="utf-8") as f: + return json.load(f) + req = urllib.request.Request(BASE + SERVICES[svc], headers={"User-Agent": "molo-bedrock-spec-check/1"}) + with urllib.request.urlopen(req, timeout=120) as r: + return json.loads(r.read().decode("utf-8")) + + +def csv(text): + return [x for x in text.split(",") if x] + + +def members(model, shape_name): + return model["shapes"][shape_name].get("members", {}) + + +def main(argv): + directory = None + args = argv[1:] + if args[:1] == ["--dir"]: + directory, args = args[1], args[2:] + calls_path = args[0] if args else CALLS + text = open(calls_path, encoding="utf-8").read() + rows = ROW.findall(text) + schemas = SCHEMA.findall(text) + models = {svc: load(svc, directory) for svc in SERVICES} + problems = [] + + def bad(kind, msg): + problems.append(f"{kind}: {msg}") + + used = {svc: set() for svc in SERVICES} + for (name, svc_letter, op, method, uri, path_p, query_p, header_p, body_p, response, bearer, stream, _state) in rows: + svc = SERVICE_OF[svc_letter] + model = models[svc] + used[svc].add(op) + operation = model["operations"].get(op) + if not operation: + bad("row", f"{name}: operation {op} not in {svc}") + continue + http = operation["http"] + if http["method"] != method: + bad("row", f"{op}: method {method} != model {http['method']}") + if http["requestUri"] != uri: + bad("row", f"{op}: uri {uri} != model {http['requestUri']}") + input_members = members(model, operation["input"]["shape"]) if "input" in operation else {} + required = set(model["shapes"][operation["input"]["shape"]].get("required", [])) if "input" in operation else set() + covered = set() + + def find(location, wanted): + for m, spec in input_members.items(): + loc = spec.get("location") + if loc == location and spec.get("locationName", m).lower() == wanted.lower(): + return m + return None + + for p in csv(path_p): + m = find("uri", p) + if not m: + bad("row", f"{op}: path param {p} is not a uri member") + else: + covered.add(m) + placeholders = re.findall(r"\{(\w+)\+?\}", uri) + if sorted(placeholders) != sorted(csv(path_p)): + bad("row", f"{op}: uri placeholders {placeholders} != path params {csv(path_p)}") + for q in csv(query_p): + m = find("querystring", q) + if not m: + bad("row", f"{op}: query param {q} is not a querystring member") + else: + covered.add(m) + for h in csv(header_p): + m = find("header", h) + if not m: + bad("row", f"{op}: header {h} is not a header member") + else: + covered.add(m) + for b in csv(body_p): + spec = input_members.get(b) + if not spec or spec.get("location"): + bad("row", f"{op}: body field {b} is not a body member") + else: + covered.add(b) + for r in sorted(required - covered): + bad("required", f"{op}: required member {r} is not covered by the row") + + out = operation.get("output", {}).get("shape") + out_members = members(model, out) if out else {} + event_member = None + for m, spec in out_members.items(): + if model["shapes"][spec["shape"]].get("eventstream"): + event_member = spec["shape"] + if bool(event_member) != (stream == "true"): + bad("stream", f"{op}: event_stream={stream} but model eventstream member is {event_member}") + expected = event_member if event_member else out + if response != expected: + bad("response", f"{op}: response schema {response} != model {expected}") + auth = model["metadata"].get("auth", []) + if ("smithy.api#httpBearerAuth" in auth) != (bearer == "true"): + bad("auth", f"{op}: bearer_ok={bearer} but model auth is {auth}") + + for (schema, svc, fields) in schemas: + model = models.get(svc) + if not model or schema not in model["shapes"]: + bad("schema", f"{svc}/{schema}: shape not found") + continue + for path in csv(fields): + shape = schema + for part in path.split("."): + mem = members(model, shape) + if part not in mem: + bad("schema", f"{svc}/{schema}: {path}: {part} is not a member of {shape}") + break + shape = mem[part]["shape"] + # Lists of structures: descend through the member shape. + spec = model["shapes"][shape] + if spec["type"] == "list": + shape = spec["member"]["shape"] + + for svc, model in models.items(): + for op in sorted(model["operations"]): + if op not in used[svc] and op not in EXCLUDED.get(svc, {}): + bad("coverage", f"{svc}/{op} is neither in the call table nor in EXCLUDED") + for op in EXCLUDED.get(svc, {}): + if op not in model["operations"]: + bad("coverage", f"EXCLUDED {svc}/{op} is not an operation of the model") + if op in used[svc]: + bad("coverage", f"EXCLUDED {svc}/{op} is also in the call table") + + total_ops = sum(len(m["operations"]) for m in models.values()) + print(f"rows {len(rows)}, schemas {len(schemas)}, operations in models {total_ops}, " + f"excluded {sum(len(v) for v in EXCLUDED.values())}") + for svc, model in models.items(): + print(f" {svc}: {len(model['operations'])} operations, apiVersion {model['metadata']['apiVersion']}, " + f"auth {model['metadata'].get('auth', ['(none listed)'])}") + if problems: + print(f"{len(problems)} problem(s):") + for p in problems: + print(" " + p) + return 1 + print("OK") + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/tests/example/ai/bedrock/page_test.cpp b/tests/example/ai/bedrock/page_test.cpp new file mode 100644 index 0000000..8aa96f6 --- /dev/null +++ b/tests/example/ai/bedrock/page_test.cpp @@ -0,0 +1,42 @@ +// Routes: GET /ai/bedrock (page), /status, /calls (no credential needed) +#include + +#include "support/Assert.hpp" + +using namespace molo::example::test; + +int main() { + setenv("MOLO_ENV_FILES", "off", 1); + for (const char* v : {"AWS_BEARER_TOKEN_BEDROCK", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN", "AWS_REGION", "AWS_DEFAULT_REGION"}) + unsetenv(v); + Fixture f; + auto page = f.client->get("/ai/bedrock"); + check(page.status == 200, "page 200"); + check(contains(page.headers.get("Content-Type"), "text/html"), "html"); + check(contains(page.body, "Amazon Bedrock") && contains(page.body, "viewport"), "title and mobile viewport"); + check(contains(page.body, "/embedded/ai_bedrock.js"), "script wired"); + check(contains(page.body, "post("/ai/bedrock", "{}", "application/json").status == 405, "post page is 405"); + + auto script = f.client->get("/embedded/ai_bedrock.js"); + check(script.status == 200 && contains(script.body, "/ai/bedrock/"), "script is embedded"); + + auto st = f.client->get("/ai/bedrock/status"); + check(st.status == 200 && contains(st.body, "\"configured\":false"), "status without a credential"); + check(contains(st.body, "\"auth_mode\":\"none\"") && contains(st.body, "\"region\":\"us-east-1\""), "status names mode and default region"); + check(contains(st.body, "AWS_BEARER_TOKEN_BEDROCK") && contains(st.body, "AWS_ACCESS_KEY_ID") && + contains(st.body, "AWS_SECRET_ACCESS_KEY") && contains(st.body, "AWS_SESSION_TOKEN") && contains(st.body, "AWS_REGION"), + "status names the credential env vars"); + check(contains(st.body, "bedrock-runtime.us-east-1.amazonaws.com") && contains(st.body, "bedrock.us-east-1.amazonaws.com") && + contains(st.body, "bedrock-agent-runtime.us-east-1.amazonaws.com"), + "status lists the three hosts"); + check(f.client->post("/ai/bedrock/status", "{}", "application/json").status == 405, "post status is 405"); + + auto calls = f.client->get("/ai/bedrock/calls"); + check(calls.status == 200 && contains(calls.body, "Converse") && contains(calls.body, "/model/{modelId}/converse-stream") && + contains(calls.body, "ApplyGuardrail") && contains(calls.body, "/knowledgebases/{knowledgeBaseId}/retrieve") && + contains(calls.body, "CreateModelInvocationJob"), + "call table listed"); + check(contains(calls.body, "\"bearer_ok\":false"), "agent runtime marked as SigV4 only"); + return finish("ai/bedrock/page"); +} diff --git a/tests/example/ai/bedrock/routes_test.cpp b/tests/example/ai/bedrock/routes_test.cpp new file mode 100644 index 0000000..350b6f2 --- /dev/null +++ b/tests/example/ai/bedrock/routes_test.cpp @@ -0,0 +1,101 @@ +// Routes: POST /ai/bedrock/* (405, 422, dry runs, 503 without a credential) +#include + +#include "support/Assert.hpp" + +using namespace molo::example::test; + +int main() { + setenv("MOLO_ENV_FILES", "off", 1); + for (const char* v : {"AWS_BEARER_TOKEN_BEDROCK", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN", "AWS_REGION", "AWS_DEFAULT_REGION"}) + unsetenv(v); + Fixture f; + auto post = [&](const std::string& route, const std::string& body) { + return f.client->post("/ai/bedrock/" + route, body, "application/json"); + }; + const char* all[] = {"converse", "converse-stream", "invoke", "invoke-stream", "count-tokens", "guardrail", + "agent", "retrieve", "models", "profiles", "guardrails", "batch", "sign"}; + for (const char* r : all) { + std::string route = std::string("/ai/bedrock/") + r; + check(f.client->get(route).status == 405, route + " get is 405"); + check(post(r, "not json").status == 422, route + " bad json is 422"); + check(post(r, "[1]").status == 422, route + " non-object is 422"); + } + + // 422: input rules run before anything else (also without a credential) + check(post("converse", "{}").status == 422, "converse needs prompt"); + check(post("converse", "{\"prompt\":\"hi\",\"temperature\":1.5}").status == 422, "temperature over 1"); + check(post("converse", "{\"prompt\":\"hi\",\"top_p\":-1}").status == 422, "top_p under 0"); + check(post("converse", "{\"prompt\":\"hi\",\"max_tokens\":0}").status == 422, "max_tokens 0"); + check(post("converse", "{\"prompt\":\"hi\",\"model\":\"a b\"}").status == 422, "model with a space"); + check(post("converse", "{\"prompt\":\"hi\",\"model\":\"../x\"}").status == 422, "model with dots"); + check(post("converse", "{\"prompt\":\"hi\",\"service_tier\":\"gold\"}").status == 422, "bad service tier"); + check(post("converse", "{\"prompt\":\"hi\",\"guardrail_id\":\"g\"}").status == 422, "guardrail needs a version"); + check(post("converse", "{\"prompt\":\"hi\",\"stop\":[1]}").status == 422, "stop must be strings"); + check(post("converse-stream", "{\"prompt\":\"hi\",\"latency\":\"warp\"}").status == 422, "bad latency"); + check(post("invoke", "{\"model\":\"m\"}").status == 422, "invoke needs a body"); + check(post("invoke", "{\"model\":\"m\",\"body\":{},\"trace\":\"LOUD\"}").status == 422, "bad trace"); + check(post("invoke-stream", "{\"body\":{}}").status == 422, "invoke-stream needs a model"); + check(post("count-tokens", "{}").status == 422, "count-tokens needs a prompt"); + check(post("guardrail", "{\"text\":\"x\"}").status == 422, "guardrail needs an id"); + check(post("guardrail", "{\"guardrail_id\":\"g\",\"text\":\"x\",\"source\":\"BOTH\"}").status == 422, "bad source"); + check(post("guardrail", "{\"guardrail_id\":\"g\",\"text\":\"x\",\"qualifiers\":[\"weird\"]}").status == 422, "bad qualifier"); + check(post("agent", "{\"agent_id\":\"A\",\"alias_id\":\"B\",\"session_id\":\"x\",\"prompt\":\"hi\"}").status == 422, "session id under 2 chars"); + check(post("agent", "{\"agent_id\":\"WAYTOOLONGAGENT\",\"alias_id\":\"B\",\"session_id\":\"sess-1\",\"prompt\":\"hi\"}").status == 422, "agent id over 10"); + check(post("retrieve", "{\"knowledge_base_id\":\"short\",\"query\":\"q\"}").status == 422, "kb id must be 10 chars"); + check(post("retrieve", "{\"knowledge_base_id\":\"KB12345678\",\"query\":\"q\",\"number_of_results\":101}").status == 422, "over 100 results"); + check(post("retrieve", "{\"knowledge_base_id\":\"KB12345678\",\"query\":\"q\",\"search_type\":\"FUZZY\"}").status == 422, "bad search type"); + check(post("models", "{\"action\":\"fly\"}").status == 422, "bad models action"); + check(post("models", "{\"action\":\"get\"}").status == 422, "models get needs id"); + check(post("models", "{\"action\":\"get\",\"id\":\"../x\"}").status == 422, "bad model id"); + check(post("profiles", "{\"max_results\":0}").status == 422, "max_results 0"); + check(post("profiles", "{\"max_results\":1001}").status == 422, "max_results over 1000"); + check(post("guardrails", "{\"action\":\"get\"}").status == 422, "guardrails get needs id"); + check(post("batch", "{}").status == 422, "batch needs an action"); + check(post("batch", "{\"action\":\"nope\"}").status == 422, "bad batch action"); + check(post("batch", "{\"action\":\"stop\",\"id\":\"../x\"}").status == 422, "bad job id"); + check(post("batch", "{\"action\":\"create\",\"job_name\":\"j\",\"role_arn\":\"role\",\"model\":\"m\",\"input_s3_uri\":\"s3://b/i\",\"output_s3_uri\":\"s3://b/o\"}").status == 422, + "bad role arn"); + check(post("batch", "{\"action\":\"create\",\"job_name\":\"j\",\"role_arn\":\"arn:aws:iam::123456789012:role/R\",\"model\":\"m\",\"input_s3_uri\":\"s3://b/i\",\"output_s3_uri\":\"s3://b/o\",\"timeout_hours\":12}").status == 422, + "timeout under 24 hours"); + + // dry runs: paid or state-changing routes show the exact request + auto dry = post("converse", "{\"prompt\":\"hi\",\"model\":\"anthropic.claude-3-sonnet-20240229-v1:0\",\"max_tokens\":50}"); + check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && contains(dry.body, "\"maxTokens\":50") && + contains(dry.body, "POST /model/anthropic.claude-3-sonnet-20240229-v1%3A0/converse"), + "converse is a dry run without confirm"); + check(contains(post("converse-stream", "{\"prompt\":\"hi\"}").body, "/converse-stream"), "converse-stream dry run"); + check(contains(post("invoke", "{\"model\":\"amazon.titan-embed-text-v2:0\",\"body\":{\"inputText\":\"x\"}}").body, "\"dry_run\":true"), "invoke dry run"); + check(contains(post("invoke-stream", "{\"model\":\"m\",\"body\":{}}").body, "invoke-with-response-stream"), "invoke-stream dry run"); + check(contains(post("guardrail", "{\"guardrail_id\":\"gr1\",\"text\":\"x\"}").body, "/guardrail/gr1/version/DRAFT/apply"), "guardrail dry run defaults to DRAFT"); + check(contains(post("agent", "{\"agent_id\":\"AGENT12345\",\"alias_id\":\"ALIAS12345\",\"session_id\":\"sess-1\",\"prompt\":\"hi\"}").body, + "/agents/AGENT12345/agentAliases/ALIAS12345/sessions/sess-1/text"), + "agent dry run"); + check(contains(post("retrieve", "{\"knowledge_base_id\":\"KB12345678\",\"query\":\"What is AWS?\"}").body, "\"retrievalQuery\":{\"text\":\"What is AWS?\"}"), + "retrieve dry run shows the documented body"); + check(contains(post("batch", "{\"action\":\"stop\",\"id\":\"abcdefghijkl\"}").body, "\"dry_run\":true"), "batch stop is a dry run"); + check(contains(post("batch", "{\"action\":\"create\",\"job_name\":\"nightly\",\"role_arn\":\"arn:aws:iam::123456789012:role/R\",\"model\":\"amazon.nova-micro-v1:0\",\"input_s3_uri\":\"s3://b/i.jsonl\",\"output_s3_uri\":\"s3://b/o/\"}").body, + "\"dry_run\":true"), + "batch create is a dry run"); + + // 503: free calls and confirmed calls need the credential + for (const char* r : {"count-tokens"}) { + auto res = post(r, "{\"prompt\":\"hi\"}"); + check(res.status == 503 && contains(res.body, "not_configured"), std::string(r) + " without a credential is 503"); + } + check(post("models", "{}").status == 503, "models list without a credential is 503"); + check(post("models", "{\"action\":\"get\",\"id\":\"amazon.nova-micro-v1:0\"}").status == 503, "models get without a credential is 503"); + check(post("profiles", "{}").status == 503, "profiles without a credential is 503"); + check(post("guardrails", "{}").status == 503, "guardrails without a credential is 503"); + check(post("batch", "{\"action\":\"list\"}").status == 503, "batch list without a credential is 503"); + check(post("batch", "{\"action\":\"get\",\"id\":\"abcdefghijkl\"}").status == 503, "batch get without a credential is 503"); + check(post("converse", "{\"prompt\":\"hi\",\"confirm\":true}").status == 503, "confirmed converse without a credential is 503"); + check(post("converse-stream", "{\"prompt\":\"hi\",\"confirm\":true}").status == 503, "confirmed converse-stream is 503"); + check(post("invoke", "{\"model\":\"m\",\"body\":{},\"confirm\":true}").status == 503, "confirmed invoke is 503"); + check(post("invoke-stream", "{\"model\":\"m\",\"body\":{},\"confirm\":true}").status == 503, "confirmed invoke-stream is 503"); + check(post("guardrail", "{\"guardrail_id\":\"g\",\"text\":\"x\",\"confirm\":true}").status == 503, "confirmed guardrail is 503"); + check(post("agent", "{\"agent_id\":\"A1\",\"alias_id\":\"B1\",\"session_id\":\"sess-1\",\"prompt\":\"hi\",\"confirm\":true}").status == 503, "confirmed agent is 503"); + check(post("retrieve", "{\"knowledge_base_id\":\"KB12345678\",\"query\":\"q\",\"confirm\":true}").status == 503, "confirmed retrieve is 503"); + check(post("batch", "{\"action\":\"stop\",\"id\":\"abcdefghijkl\",\"confirm\":true}").status == 503, "confirmed batch stop is 503"); + return finish("ai/bedrock/routes"); +} diff --git a/tests/example/ai/bedrock/sign_test.cpp b/tests/example/ai/bedrock/sign_test.cpp new file mode 100644 index 0000000..a204abe --- /dev/null +++ b/tests/example/ai/bedrock/sign_test.cpp @@ -0,0 +1,34 @@ +// Route: POST /ai/bedrock/sign is local (no credential, no network). With the +// request of AWS's get-vanilla test case it must give the suite's signature. +#include + +#include "support/Assert.hpp" + +using namespace molo::example::test; + +int main() { + setenv("MOLO_ENV_FILES", "off", 1); + for (const char* v : {"AWS_BEARER_TOKEN_BEDROCK", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN"}) unsetenv(v); + Fixture f; + auto sign = [&](const std::string& body) { return f.client->post("/ai/bedrock/sign", body, "application/json"); }; + + // aws4_testsuite get-vanilla: GET /, host example.amazonaws.com, 20150830T123600Z, us-east-1, service "service". + auto r = sign("{\"service\":\"service\",\"region\":\"us-east-1\"}"); + check(r.status == 200, "sign answers without a credential"); + check(contains(r.body, "5fa00fa31553b73ebf1942676e86291e8372ff2a2260956d9b8aae1d763fbf31"), "signature equals AWS's get-vanilla vector"); + check(contains(r.body, "AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request"), "authorization header shape"); + check(contains(r.body, "documentation credentials"), "says whose credentials it used"); + check(!contains(r.body, "wJalrXUtnFEMI"), "the secret is not echoed"); + + auto q = sign("{\"service\":\"service\",\"query\":\"Param-3=Value3&Param=Value2\"}"); + check(q.status == 200 && contains(q.body, "Param=Value2&Param-3=Value3"), "query is sorted in the canonical request"); + + auto bedrock = sign("{\"method\":\"POST\",\"path\":\"/model/anthropic.claude-3-sonnet-20240229-v1%3A0/converse\",\"host\":\"bedrock-runtime.us-east-1.amazonaws.com\",\"body\":\"{}\"}"); + check(bedrock.status == 200 && contains(bedrock.body, "v1%253A0"), "a Bedrock model path is encoded twice in the canonical request"); + + check(sign("{\"method\":\"TRACE\"}").status == 422, "unsupported method"); + check(sign("{\"path\":\"no-slash\"}").status == 422, "path must start with /"); + check(sign("{\"region\":\"US EAST\"}").status == 422, "bad region"); + check(sign("{\"date\":\"yesterday\"}").status == 422, "bad date"); + return finish("ai/bedrock/sign"); +} diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-header-key-duplicate/get-header-key-duplicate.authz b/tests/src/vendors/bedrock/sigv4_suite/get-header-key-duplicate/get-header-key-duplicate.authz new file mode 100644 index 0000000..ade3ec7 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-header-key-duplicate/get-header-key-duplicate.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;my-header1;x-amz-date, Signature=c9d5ea9f3f72853aea855b47ea873832890dbdd183b4468f858259531a5138ea \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-header-key-duplicate/get-header-key-duplicate.creq b/tests/src/vendors/bedrock/sigv4_suite/get-header-key-duplicate/get-header-key-duplicate.creq new file mode 100644 index 0000000..fa8f49a --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-header-key-duplicate/get-header-key-duplicate.creq @@ -0,0 +1,9 @@ +GET +/ + +host:example.amazonaws.com +my-header1:value2,value2,value1 +x-amz-date:20150830T123600Z + +host;my-header1;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-header-key-duplicate/get-header-key-duplicate.req b/tests/src/vendors/bedrock/sigv4_suite/get-header-key-duplicate/get-header-key-duplicate.req new file mode 100644 index 0000000..08a0364 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-header-key-duplicate/get-header-key-duplicate.req @@ -0,0 +1,6 @@ +GET / HTTP/1.1 +Host:example.amazonaws.com +My-Header1:value2 +My-Header1:value2 +My-Header1:value1 +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-header-key-duplicate/get-header-key-duplicate.sts b/tests/src/vendors/bedrock/sigv4_suite/get-header-key-duplicate/get-header-key-duplicate.sts new file mode 100644 index 0000000..48a135e --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-header-key-duplicate/get-header-key-duplicate.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +dc7f04a3abfde8d472b0ab1a418b741b7c67174dad1551b4117b15527fbe966c \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-header-value-multiline/get-header-value-multiline.authz b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-multiline/get-header-value-multiline.authz new file mode 100644 index 0000000..9f45569 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-multiline/get-header-value-multiline.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;my-header1;x-amz-date, Signature=cfd34249e4b1c8d6b91ef74165d41a32e5fab3306300901bb65a51a73575eefd \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-header-value-multiline/get-header-value-multiline.creq b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-multiline/get-header-value-multiline.creq new file mode 100644 index 0000000..8cb5476 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-multiline/get-header-value-multiline.creq @@ -0,0 +1,9 @@ +GET +/ + +host:example.amazonaws.com +my-header1:value1 value2 value3 +x-amz-date:20150830T123600Z + +host;my-header1;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-header-value-multiline/get-header-value-multiline.req b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-multiline/get-header-value-multiline.req new file mode 100644 index 0000000..7caa6ac --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-multiline/get-header-value-multiline.req @@ -0,0 +1,6 @@ +GET / HTTP/1.1 +Host:example.amazonaws.com +My-Header1:value1 + value2 + value3 +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-header-value-multiline/get-header-value-multiline.sts b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-multiline/get-header-value-multiline.sts new file mode 100644 index 0000000..97c7430 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-multiline/get-header-value-multiline.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +e99419459a677bc11de234014be3c4e72c1ea5b454ceb58b613061f5d7a162e8 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-header-value-order/get-header-value-order.authz b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-order/get-header-value-order.authz new file mode 100644 index 0000000..c0409ab --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-order/get-header-value-order.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;my-header1;x-amz-date, Signature=08c7e5a9acfcfeb3ab6b2185e75ce8b1deb5e634ec47601a50643f830c755c01 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-header-value-order/get-header-value-order.creq b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-order/get-header-value-order.creq new file mode 100644 index 0000000..e336bc9 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-order/get-header-value-order.creq @@ -0,0 +1,9 @@ +GET +/ + +host:example.amazonaws.com +my-header1:value4,value1,value3,value2 +x-amz-date:20150830T123600Z + +host;my-header1;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-header-value-order/get-header-value-order.req b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-order/get-header-value-order.req new file mode 100644 index 0000000..f7bd9e6 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-order/get-header-value-order.req @@ -0,0 +1,7 @@ +GET / HTTP/1.1 +Host:example.amazonaws.com +My-Header1:value4 +My-Header1:value1 +My-Header1:value3 +My-Header1:value2 +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-header-value-order/get-header-value-order.sts b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-order/get-header-value-order.sts new file mode 100644 index 0000000..711a8d4 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-order/get-header-value-order.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +31ce73cd3f3d9f66977ad3dd957dc47af14df92fcd8509f59b349e9137c58b86 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-header-value-trim/get-header-value-trim.authz b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-trim/get-header-value-trim.authz new file mode 100644 index 0000000..4874ac0 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-trim/get-header-value-trim.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;my-header1;my-header2;x-amz-date, Signature=acc3ed3afb60bb290fc8d2dd0098b9911fcaa05412b367055dee359757a9c736 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-header-value-trim/get-header-value-trim.creq b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-trim/get-header-value-trim.creq new file mode 100644 index 0000000..a59087c --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-trim/get-header-value-trim.creq @@ -0,0 +1,10 @@ +GET +/ + +host:example.amazonaws.com +my-header1:value1 +my-header2:"a b c" +x-amz-date:20150830T123600Z + +host;my-header1;my-header2;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-header-value-trim/get-header-value-trim.req b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-trim/get-header-value-trim.req new file mode 100644 index 0000000..901f36c --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-trim/get-header-value-trim.req @@ -0,0 +1,5 @@ +GET / HTTP/1.1 +Host:example.amazonaws.com +My-Header1: value1 +My-Header2: "a b c" +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-header-value-trim/get-header-value-trim.sts b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-trim/get-header-value-trim.sts new file mode 100644 index 0000000..a0b15cc --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-header-value-trim/get-header-value-trim.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +a726db9b0df21c14f559d0a978e563112acb1b9e05476f0a6a1c7d68f28605c7 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-unreserved/get-unreserved.authz b/tests/src/vendors/bedrock/sigv4_suite/get-unreserved/get-unreserved.authz new file mode 100644 index 0000000..2943ec8 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-unreserved/get-unreserved.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=07ef7494c76fa4850883e2b006601f940f8a34d404d0cfa977f52a65bbf5f24f \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-unreserved/get-unreserved.creq b/tests/src/vendors/bedrock/sigv4_suite/get-unreserved/get-unreserved.creq new file mode 100644 index 0000000..8af54df --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-unreserved/get-unreserved.creq @@ -0,0 +1,8 @@ +GET +/-._~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz + +host:example.amazonaws.com +x-amz-date:20150830T123600Z + +host;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-unreserved/get-unreserved.req b/tests/src/vendors/bedrock/sigv4_suite/get-unreserved/get-unreserved.req new file mode 100644 index 0000000..da760cd --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-unreserved/get-unreserved.req @@ -0,0 +1,3 @@ +GET /-._~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz HTTP/1.1 +Host:example.amazonaws.com +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-unreserved/get-unreserved.sts b/tests/src/vendors/bedrock/sigv4_suite/get-unreserved/get-unreserved.sts new file mode 100644 index 0000000..e9dc541 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-unreserved/get-unreserved.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +6a968768eefaa713e2a6b16b589a8ea192661f098f37349f4e2c0082757446f9 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-utf8/get-utf8.authz b/tests/src/vendors/bedrock/sigv4_suite/get-utf8/get-utf8.authz new file mode 100644 index 0000000..738b3fb --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-utf8/get-utf8.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=8318018e0b0f223aa2bbf98705b62bb787dc9c0e678f255a891fd03141be5d85 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-utf8/get-utf8.creq b/tests/src/vendors/bedrock/sigv4_suite/get-utf8/get-utf8.creq new file mode 100644 index 0000000..5d4b9f6 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-utf8/get-utf8.creq @@ -0,0 +1,8 @@ +GET +/%E1%88%B4 + +host:example.amazonaws.com +x-amz-date:20150830T123600Z + +host;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-utf8/get-utf8.req b/tests/src/vendors/bedrock/sigv4_suite/get-utf8/get-utf8.req new file mode 100644 index 0000000..da4808d --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-utf8/get-utf8.req @@ -0,0 +1,3 @@ +GET /ሴ HTTP/1.1 +Host:example.amazonaws.com +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-utf8/get-utf8.sts b/tests/src/vendors/bedrock/sigv4_suite/get-utf8/get-utf8.sts new file mode 100644 index 0000000..5edc8f4 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-utf8/get-utf8.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +2a0a97d02205e45ce2e994789806b19270cfbbb0921b278ccf58f5249ac42102 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-empty-query-key/get-vanilla-empty-query-key.authz b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-empty-query-key/get-vanilla-empty-query-key.authz new file mode 100644 index 0000000..65b5c7c --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-empty-query-key/get-vanilla-empty-query-key.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=a67d582fa61cc504c4bae71f336f98b97f1ea3c7a6bfe1b6e45aec72011b9aeb \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-empty-query-key/get-vanilla-empty-query-key.creq b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-empty-query-key/get-vanilla-empty-query-key.creq new file mode 100644 index 0000000..c6cdced --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-empty-query-key/get-vanilla-empty-query-key.creq @@ -0,0 +1,8 @@ +GET +/ +Param1=value1 +host:example.amazonaws.com +x-amz-date:20150830T123600Z + +host;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-empty-query-key/get-vanilla-empty-query-key.req b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-empty-query-key/get-vanilla-empty-query-key.req new file mode 100644 index 0000000..970d0a0 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-empty-query-key/get-vanilla-empty-query-key.req @@ -0,0 +1,3 @@ +GET /?Param1=value1 HTTP/1.1 +Host:example.amazonaws.com +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-empty-query-key/get-vanilla-empty-query-key.sts b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-empty-query-key/get-vanilla-empty-query-key.sts new file mode 100644 index 0000000..c4ed216 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-empty-query-key/get-vanilla-empty-query-key.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +1e24db194ed7d0eec2de28d7369675a243488e08526e8c1c73571282f7c517ab \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-encoded/get-vanilla-query-order-encoded.authz b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-encoded/get-vanilla-query-order-encoded.authz new file mode 100644 index 0000000..99e9725 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-encoded/get-vanilla-query-order-encoded.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=371d3713e185cc334048618a97f809c9ffe339c62934c032af5a0e595648fcac \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-encoded/get-vanilla-query-order-encoded.creq b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-encoded/get-vanilla-query-order-encoded.creq new file mode 100644 index 0000000..0c8ba21 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-encoded/get-vanilla-query-order-encoded.creq @@ -0,0 +1,8 @@ +GET +/ +%E1%88%B4=Value1&Param=Value2&Param-3=Value3 +host:example.amazonaws.com +x-amz-date:20150830T123600Z + +host;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-encoded/get-vanilla-query-order-encoded.req b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-encoded/get-vanilla-query-order-encoded.req new file mode 100644 index 0000000..c539437 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-encoded/get-vanilla-query-order-encoded.req @@ -0,0 +1,3 @@ +GET /?Param-3=Value3&Param=Value2&%E1%88%B4=Value1 HTTP/1.1 +Host:example.amazonaws.com +X-Amz-Date:20150830T123600Z diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-encoded/get-vanilla-query-order-encoded.sts b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-encoded/get-vanilla-query-order-encoded.sts new file mode 100644 index 0000000..bf674ad --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-encoded/get-vanilla-query-order-encoded.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +868294f5c38bd141c4972a373a76654f1418a8e4fc18b2e7903ae45e8ae0ec71 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key-case/get-vanilla-query-order-key-case.authz b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key-case/get-vanilla-query-order-key-case.authz new file mode 100644 index 0000000..c781fe6 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key-case/get-vanilla-query-order-key-case.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=b97d918cfa904a5beff61c982a1b6f458b799221646efd99d3219ec94cdf2500 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key-case/get-vanilla-query-order-key-case.creq b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key-case/get-vanilla-query-order-key-case.creq new file mode 100644 index 0000000..8ae02cd --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key-case/get-vanilla-query-order-key-case.creq @@ -0,0 +1,8 @@ +GET +/ +Param1=value1&Param2=value2 +host:example.amazonaws.com +x-amz-date:20150830T123600Z + +host;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key-case/get-vanilla-query-order-key-case.req b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key-case/get-vanilla-query-order-key-case.req new file mode 100644 index 0000000..8a56f15 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key-case/get-vanilla-query-order-key-case.req @@ -0,0 +1,3 @@ +GET /?Param2=value2&Param1=value1 HTTP/1.1 +Host:example.amazonaws.com +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key-case/get-vanilla-query-order-key-case.sts b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key-case/get-vanilla-query-order-key-case.sts new file mode 100644 index 0000000..f773de5 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key-case/get-vanilla-query-order-key-case.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +816cd5b414d056048ba4f7c5386d6e0533120fb1fcfa93762cf0fc39e2cf19e0 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key/get-vanilla-query-order-key.authz b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key/get-vanilla-query-order-key.authz new file mode 100644 index 0000000..812cd3f --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key/get-vanilla-query-order-key.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=eedbc4e291e521cf13422ffca22be7d2eb8146eecf653089df300a15b2382bd1 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key/get-vanilla-query-order-key.creq b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key/get-vanilla-query-order-key.creq new file mode 100644 index 0000000..36c3cdf --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key/get-vanilla-query-order-key.creq @@ -0,0 +1,8 @@ +GET +/ +Param1=Value1&Param1=value2 +host:example.amazonaws.com +x-amz-date:20150830T123600Z + +host;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key/get-vanilla-query-order-key.req b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key/get-vanilla-query-order-key.req new file mode 100644 index 0000000..375a496 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key/get-vanilla-query-order-key.req @@ -0,0 +1,3 @@ +GET /?Param1=value2&Param1=Value1 HTTP/1.1 +Host:example.amazonaws.com +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key/get-vanilla-query-order-key.sts b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key/get-vanilla-query-order-key.sts new file mode 100644 index 0000000..fd43a41 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-key/get-vanilla-query-order-key.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +704b4cef673542d84cdff252633f065e8daeba5f168b77116f8b1bcaf3d38f89 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-value/get-vanilla-query-order-value.authz b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-value/get-vanilla-query-order-value.authz new file mode 100644 index 0000000..b8ad91f --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-value/get-vanilla-query-order-value.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=5772eed61e12b33fae39ee5e7012498b51d56abc0abb7c60486157bd471c4694 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-value/get-vanilla-query-order-value.creq b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-value/get-vanilla-query-order-value.creq new file mode 100644 index 0000000..26898eb --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-value/get-vanilla-query-order-value.creq @@ -0,0 +1,8 @@ +GET +/ +Param1=value1&Param1=value2 +host:example.amazonaws.com +x-amz-date:20150830T123600Z + +host;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-value/get-vanilla-query-order-value.req b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-value/get-vanilla-query-order-value.req new file mode 100644 index 0000000..9255bee --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-value/get-vanilla-query-order-value.req @@ -0,0 +1,3 @@ +GET /?Param1=value2&Param1=value1 HTTP/1.1 +Host:example.amazonaws.com +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-value/get-vanilla-query-order-value.sts b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-value/get-vanilla-query-order-value.sts new file mode 100644 index 0000000..90e66b8 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-order-value/get-vanilla-query-order-value.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +c968629d70850097a2d8781c9bf7edcb988b04cac14cca9be4acc3595f884606 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-unreserved/get-vanilla-query-unreserved.authz b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-unreserved/get-vanilla-query-unreserved.authz new file mode 100644 index 0000000..a44ca5b --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-unreserved/get-vanilla-query-unreserved.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=9c3e54bfcdf0b19771a7f523ee5669cdf59bc7cc0884027167c21bb143a40197 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-unreserved/get-vanilla-query-unreserved.creq b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-unreserved/get-vanilla-query-unreserved.creq new file mode 100644 index 0000000..5249be3 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-unreserved/get-vanilla-query-unreserved.creq @@ -0,0 +1,8 @@ +GET +/ +-._~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz=-._~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz +host:example.amazonaws.com +x-amz-date:20150830T123600Z + +host;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-unreserved/get-vanilla-query-unreserved.req b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-unreserved/get-vanilla-query-unreserved.req new file mode 100644 index 0000000..d2833b3 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-unreserved/get-vanilla-query-unreserved.req @@ -0,0 +1,3 @@ +GET /?-._~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz=-._~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz HTTP/1.1 +Host:example.amazonaws.com +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-unreserved/get-vanilla-query-unreserved.sts b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-unreserved/get-vanilla-query-unreserved.sts new file mode 100644 index 0000000..24a97d2 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query-unreserved/get-vanilla-query-unreserved.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +c30d4703d9f799439be92736156d47ccfb2d879ddf56f5befa6d1d6aab979177 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query/get-vanilla-query.authz b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query/get-vanilla-query.authz new file mode 100644 index 0000000..551c027 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query/get-vanilla-query.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=5fa00fa31553b73ebf1942676e86291e8372ff2a2260956d9b8aae1d763fbf31 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query/get-vanilla-query.creq b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query/get-vanilla-query.creq new file mode 100644 index 0000000..ed91561 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query/get-vanilla-query.creq @@ -0,0 +1,8 @@ +GET +/ + +host:example.amazonaws.com +x-amz-date:20150830T123600Z + +host;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query/get-vanilla-query.req b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query/get-vanilla-query.req new file mode 100644 index 0000000..0f7a9bf --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query/get-vanilla-query.req @@ -0,0 +1,3 @@ +GET / HTTP/1.1 +Host:example.amazonaws.com +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query/get-vanilla-query.sts b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query/get-vanilla-query.sts new file mode 100644 index 0000000..b187649 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-query/get-vanilla-query.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +bb579772317eb040ac9ed261061d46c1f17a8133879d6129b6e1c25292927e63 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-utf8-query/get-vanilla-utf8-query.authz b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-utf8-query/get-vanilla-utf8-query.authz new file mode 100644 index 0000000..e016c3d --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-utf8-query/get-vanilla-utf8-query.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=2cdec8eed098649ff3a119c94853b13c643bcf08f8b0a1d91e12c9027818dd04 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-utf8-query/get-vanilla-utf8-query.creq b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-utf8-query/get-vanilla-utf8-query.creq new file mode 100644 index 0000000..a835c9e --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-utf8-query/get-vanilla-utf8-query.creq @@ -0,0 +1,8 @@ +GET +/ +%E1%88%B4=bar +host:example.amazonaws.com +x-amz-date:20150830T123600Z + +host;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-utf8-query/get-vanilla-utf8-query.req b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-utf8-query/get-vanilla-utf8-query.req new file mode 100644 index 0000000..cc2757e --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-utf8-query/get-vanilla-utf8-query.req @@ -0,0 +1,3 @@ +GET /?ሴ=bar HTTP/1.1 +Host:example.amazonaws.com +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-utf8-query/get-vanilla-utf8-query.sts b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-utf8-query/get-vanilla-utf8-query.sts new file mode 100644 index 0000000..51ee71b --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-utf8-query/get-vanilla-utf8-query.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +eb30c5bed55734080471a834cc727ae56beb50e5f39d1bff6d0d38cb192a7073 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-with-session-token/get-vanilla-with-session-token.authz b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-with-session-token/get-vanilla-with-session-token.authz new file mode 100644 index 0000000..cb7ae61 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-with-session-token/get-vanilla-with-session-token.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date;x-amz-security-token, Signature=07ec1639c89043aa0e3e2de82b96708f198cceab042d4a97044c66dd9f74e7f8 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-with-session-token/get-vanilla-with-session-token.creq b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-with-session-token/get-vanilla-with-session-token.creq new file mode 100644 index 0000000..ccacdeb --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-with-session-token/get-vanilla-with-session-token.creq @@ -0,0 +1,9 @@ +GET +/ + +host:example.amazonaws.com +x-amz-date:20150830T123600Z +x-amz-security-token:6e86291e8372ff2a2260956d9b8aae1d763fbf315fa00fa31553b73ebf194267 + +host;x-amz-date;x-amz-security-token +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-with-session-token/get-vanilla-with-session-token.req b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-with-session-token/get-vanilla-with-session-token.req new file mode 100644 index 0000000..0f7a9bf --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-with-session-token/get-vanilla-with-session-token.req @@ -0,0 +1,3 @@ +GET / HTTP/1.1 +Host:example.amazonaws.com +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-with-session-token/get-vanilla-with-session-token.sts b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-with-session-token/get-vanilla-with-session-token.sts new file mode 100644 index 0000000..742b880 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla-with-session-token/get-vanilla-with-session-token.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +067b36aa60031588cea4a4cde1f21215227a047690c72247f1d70b32fbbfad2b \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla/get-vanilla.authz b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla/get-vanilla.authz new file mode 100644 index 0000000..551c027 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla/get-vanilla.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=5fa00fa31553b73ebf1942676e86291e8372ff2a2260956d9b8aae1d763fbf31 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla/get-vanilla.creq b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla/get-vanilla.creq new file mode 100644 index 0000000..ed91561 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla/get-vanilla.creq @@ -0,0 +1,8 @@ +GET +/ + +host:example.amazonaws.com +x-amz-date:20150830T123600Z + +host;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla/get-vanilla.req b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla/get-vanilla.req new file mode 100644 index 0000000..0f7a9bf --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla/get-vanilla.req @@ -0,0 +1,3 @@ +GET / HTTP/1.1 +Host:example.amazonaws.com +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/get-vanilla/get-vanilla.sts b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla/get-vanilla.sts new file mode 100644 index 0000000..b187649 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/get-vanilla/get-vanilla.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +bb579772317eb040ac9ed261061d46c1f17a8133879d6129b6e1c25292927e63 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-header-key-case/post-header-key-case.authz b/tests/src/vendors/bedrock/sigv4_suite/post-header-key-case/post-header-key-case.authz new file mode 100644 index 0000000..89e572e --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-header-key-case/post-header-key-case.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=5da7c1a2acd57cee7505fc6676e4e544621c30862966e37dddb68e92efbe5d6b \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-header-key-case/post-header-key-case.creq b/tests/src/vendors/bedrock/sigv4_suite/post-header-key-case/post-header-key-case.creq new file mode 100644 index 0000000..5c3a943 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-header-key-case/post-header-key-case.creq @@ -0,0 +1,8 @@ +POST +/ + +host:example.amazonaws.com +x-amz-date:20150830T123600Z + +host;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-header-key-case/post-header-key-case.req b/tests/src/vendors/bedrock/sigv4_suite/post-header-key-case/post-header-key-case.req new file mode 100644 index 0000000..3dc4179 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-header-key-case/post-header-key-case.req @@ -0,0 +1,3 @@ +POST / HTTP/1.1 +Host:example.amazonaws.com +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-header-key-case/post-header-key-case.sts b/tests/src/vendors/bedrock/sigv4_suite/post-header-key-case/post-header-key-case.sts new file mode 100644 index 0000000..a636703 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-header-key-case/post-header-key-case.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +553f88c9e4d10fc9e109e2aeb65f030801b70c2f6468faca261d401ae622fc87 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-header-key-sort/post-header-key-sort.authz b/tests/src/vendors/bedrock/sigv4_suite/post-header-key-sort/post-header-key-sort.authz new file mode 100644 index 0000000..a62589f --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-header-key-sort/post-header-key-sort.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;my-header1;x-amz-date, Signature=c5410059b04c1ee005303aed430f6e6645f61f4dc9e1461ec8f8916fdf18852c \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-header-key-sort/post-header-key-sort.creq b/tests/src/vendors/bedrock/sigv4_suite/post-header-key-sort/post-header-key-sort.creq new file mode 100644 index 0000000..ebe943e --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-header-key-sort/post-header-key-sort.creq @@ -0,0 +1,9 @@ +POST +/ + +host:example.amazonaws.com +my-header1:value1 +x-amz-date:20150830T123600Z + +host;my-header1;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-header-key-sort/post-header-key-sort.req b/tests/src/vendors/bedrock/sigv4_suite/post-header-key-sort/post-header-key-sort.req new file mode 100644 index 0000000..0253f19 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-header-key-sort/post-header-key-sort.req @@ -0,0 +1,4 @@ +POST / HTTP/1.1 +Host:example.amazonaws.com +My-Header1:value1 +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-header-key-sort/post-header-key-sort.sts b/tests/src/vendors/bedrock/sigv4_suite/post-header-key-sort/post-header-key-sort.sts new file mode 100644 index 0000000..eb66362 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-header-key-sort/post-header-key-sort.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +9368318c2967cf6de74404b30c65a91e8f6253e0a8659d6d5319f1a812f87d65 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-header-value-case/post-header-value-case.authz b/tests/src/vendors/bedrock/sigv4_suite/post-header-value-case/post-header-value-case.authz new file mode 100644 index 0000000..d9e52a3 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-header-value-case/post-header-value-case.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;my-header1;x-amz-date, Signature=cdbc9802e29d2942e5e10b5bccfdd67c5f22c7c4e8ae67b53629efa58b974b7d \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-header-value-case/post-header-value-case.creq b/tests/src/vendors/bedrock/sigv4_suite/post-header-value-case/post-header-value-case.creq new file mode 100644 index 0000000..af824c8 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-header-value-case/post-header-value-case.creq @@ -0,0 +1,9 @@ +POST +/ + +host:example.amazonaws.com +my-header1:VALUE1 +x-amz-date:20150830T123600Z + +host;my-header1;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-header-value-case/post-header-value-case.req b/tests/src/vendors/bedrock/sigv4_suite/post-header-value-case/post-header-value-case.req new file mode 100644 index 0000000..3f9987a --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-header-value-case/post-header-value-case.req @@ -0,0 +1,4 @@ +POST / HTTP/1.1 +Host:example.amazonaws.com +My-Header1:VALUE1 +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-header-value-case/post-header-value-case.sts b/tests/src/vendors/bedrock/sigv4_suite/post-header-value-case/post-header-value-case.sts new file mode 100644 index 0000000..40062c7 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-header-value-case/post-header-value-case.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +d51ced243e649e3de6ef63afbbdcbca03131a21a7103a1583706a64618606a93 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-empty-query-value/post-vanilla-empty-query-value.authz b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-empty-query-value/post-vanilla-empty-query-value.authz new file mode 100644 index 0000000..44280cd --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-empty-query-value/post-vanilla-empty-query-value.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=28038455d6de14eafc1f9222cf5aa6f1a96197d7deb8263271d420d138af7f11 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-empty-query-value/post-vanilla-empty-query-value.creq b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-empty-query-value/post-vanilla-empty-query-value.creq new file mode 100644 index 0000000..f5058d4 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-empty-query-value/post-vanilla-empty-query-value.creq @@ -0,0 +1,8 @@ +POST +/ +Param1=value1 +host:example.amazonaws.com +x-amz-date:20150830T123600Z + +host;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-empty-query-value/post-vanilla-empty-query-value.req b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-empty-query-value/post-vanilla-empty-query-value.req new file mode 100644 index 0000000..9157bc7 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-empty-query-value/post-vanilla-empty-query-value.req @@ -0,0 +1,3 @@ +POST /?Param1=value1 HTTP/1.1 +Host:example.amazonaws.com +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-empty-query-value/post-vanilla-empty-query-value.sts b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-empty-query-value/post-vanilla-empty-query-value.sts new file mode 100644 index 0000000..ca7cc66 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-empty-query-value/post-vanilla-empty-query-value.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +9d659678c1756bb3113e2ce898845a0a79dbbc57b740555917687f1b3340fbbd \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-query/post-vanilla-query.authz b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-query/post-vanilla-query.authz new file mode 100644 index 0000000..44280cd --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-query/post-vanilla-query.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=28038455d6de14eafc1f9222cf5aa6f1a96197d7deb8263271d420d138af7f11 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-query/post-vanilla-query.creq b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-query/post-vanilla-query.creq new file mode 100644 index 0000000..f5058d4 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-query/post-vanilla-query.creq @@ -0,0 +1,8 @@ +POST +/ +Param1=value1 +host:example.amazonaws.com +x-amz-date:20150830T123600Z + +host;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-query/post-vanilla-query.req b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-query/post-vanilla-query.req new file mode 100644 index 0000000..9157bc7 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-query/post-vanilla-query.req @@ -0,0 +1,3 @@ +POST /?Param1=value1 HTTP/1.1 +Host:example.amazonaws.com +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-query/post-vanilla-query.sts b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-query/post-vanilla-query.sts new file mode 100644 index 0000000..ca7cc66 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla-query/post-vanilla-query.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +9d659678c1756bb3113e2ce898845a0a79dbbc57b740555917687f1b3340fbbd \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-vanilla/post-vanilla.authz b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla/post-vanilla.authz new file mode 100644 index 0000000..89e572e --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla/post-vanilla.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=5da7c1a2acd57cee7505fc6676e4e544621c30862966e37dddb68e92efbe5d6b \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-vanilla/post-vanilla.creq b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla/post-vanilla.creq new file mode 100644 index 0000000..5c3a943 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla/post-vanilla.creq @@ -0,0 +1,8 @@ +POST +/ + +host:example.amazonaws.com +x-amz-date:20150830T123600Z + +host;x-amz-date +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-vanilla/post-vanilla.req b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla/post-vanilla.req new file mode 100644 index 0000000..3dc4179 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla/post-vanilla.req @@ -0,0 +1,3 @@ +POST / HTTP/1.1 +Host:example.amazonaws.com +X-Amz-Date:20150830T123600Z \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-vanilla/post-vanilla.sts b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla/post-vanilla.sts new file mode 100644 index 0000000..a636703 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-vanilla/post-vanilla.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +553f88c9e4d10fc9e109e2aeb65f030801b70c2f6468faca261d401ae622fc87 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded-parameters/post-x-www-form-urlencoded-parameters.authz b/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded-parameters/post-x-www-form-urlencoded-parameters.authz new file mode 100644 index 0000000..531b89b --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded-parameters/post-x-www-form-urlencoded-parameters.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=1a72ec8f64bd914b0e42e42607c7fbce7fb2c7465f63e3092b3b0d39fa77a6fe \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded-parameters/post-x-www-form-urlencoded-parameters.creq b/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded-parameters/post-x-www-form-urlencoded-parameters.creq new file mode 100644 index 0000000..8ec0d6c --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded-parameters/post-x-www-form-urlencoded-parameters.creq @@ -0,0 +1,9 @@ +POST +/ + +content-type:application/x-www-form-urlencoded; charset=utf8 +host:example.amazonaws.com +x-amz-date:20150830T123600Z + +content-type;host;x-amz-date +9095672bbd1f56dfc5b65f3e153adc8731a4a654192329106275f4c7b24d0b6e \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded-parameters/post-x-www-form-urlencoded-parameters.req b/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded-parameters/post-x-www-form-urlencoded-parameters.req new file mode 100644 index 0000000..5ce537e --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded-parameters/post-x-www-form-urlencoded-parameters.req @@ -0,0 +1,6 @@ +POST / HTTP/1.1 +Content-Type:application/x-www-form-urlencoded; charset=utf8 +Host:example.amazonaws.com +X-Amz-Date:20150830T123600Z + +Param1=value1 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded-parameters/post-x-www-form-urlencoded-parameters.sts b/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded-parameters/post-x-www-form-urlencoded-parameters.sts new file mode 100644 index 0000000..3e83c52 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded-parameters/post-x-www-form-urlencoded-parameters.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +2e1cf7ed91881a30569e46552437e4156c823447bf1781b921b5d486c568dd1c \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded/post-x-www-form-urlencoded.authz b/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded/post-x-www-form-urlencoded.authz new file mode 100644 index 0000000..d7baf53 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded/post-x-www-form-urlencoded.authz @@ -0,0 +1 @@ +AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=ff11897932ad3f4e8b18135d722051e5ac45fc38421b1da7b9d196a0fe09473a \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded/post-x-www-form-urlencoded.creq b/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded/post-x-www-form-urlencoded.creq new file mode 100644 index 0000000..d7197f1 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded/post-x-www-form-urlencoded.creq @@ -0,0 +1,9 @@ +POST +/ + +content-type:application/x-www-form-urlencoded +host:example.amazonaws.com +x-amz-date:20150830T123600Z + +content-type;host;x-amz-date +9095672bbd1f56dfc5b65f3e153adc8731a4a654192329106275f4c7b24d0b6e \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded/post-x-www-form-urlencoded.req b/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded/post-x-www-form-urlencoded.req new file mode 100644 index 0000000..ada7f87 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded/post-x-www-form-urlencoded.req @@ -0,0 +1,6 @@ +POST / HTTP/1.1 +Content-Type:application/x-www-form-urlencoded +Host:example.amazonaws.com +X-Amz-Date:20150830T123600Z + +Param1=value1 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded/post-x-www-form-urlencoded.sts b/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded/post-x-www-form-urlencoded.sts new file mode 100644 index 0000000..65ab663 --- /dev/null +++ b/tests/src/vendors/bedrock/sigv4_suite/post-x-www-form-urlencoded/post-x-www-form-urlencoded.sts @@ -0,0 +1,4 @@ +AWS4-HMAC-SHA256 +20150830T123600Z +20150830/us-east-1/service/aws4_request +42a5e5bb34198acb3e84da4f085bb7927f2bc277ca766e6d19c73c2154021281 \ No newline at end of file diff --git a/tests/src/vendors/bedrock/support.h b/tests/src/vendors/bedrock/support.h new file mode 100644 index 0000000..5774af8 --- /dev/null +++ b/tests/src/vendors/bedrock/support.h @@ -0,0 +1,95 @@ +#pragma once + +// Shared rig for the Bedrock tests: a BedrockClient over a recording +// MockTransport, a fixed clock (2015-08-30T12:36:00Z, the date of AWS's SigV4 +// suite) and AWS's documentation credentials. The mock only records the +// requests the real client builds; replies are either examples taken from the +// AWS API reference (labelled DOC) or documents shaped by the service model +// (labelled MODEL). Nothing here verifies vendor behavior: see +// src/vendors/bedrock/SPEC.md ## Unverified. + +#include +#include +#include + +#include "clients/transports/mock_transport.h" +#include "framework.h" +#include "llm/error.h" +#include "security/crypto/Base64.h" +#include "vendors/bedrock/agent.h" +#include "vendors/bedrock/caps.h" +#include "vendors/bedrock/chat_model.h" +#include "vendors/bedrock/client.h" +#include "vendors/bedrock/control.h" +#include "vendors/bedrock/event_stream.h" +#include "vendors/bedrock/runtime.h" + +namespace bedrock_test { + +using molo::clients::ClientRequest; +using molo::clients::ClientResponse; +using molo::clients::HeaderMap; +using molo::clients::Method; +using molo::clients::transports::MockExpectation; +using molo::clients::transports::MockTransport; +using Json = molo::clients::body::Json; +using namespace molo::bedrock; + +inline 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); +} + +inline ClientResponse stream_reply(const std::string& body) { + return reply(body, 200, "application/vnd.amazon.eventstream"); +} + +inline bool has(const std::string& hay, const std::string& needle) { return hay.find(needle) != std::string::npos; } + +constexpr const char* kAccess = "AKIDEXAMPLE"; +constexpr const char* kSecret = "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"; +constexpr const char* kBearer = "bedrock-api-key-TESTKEY-DO-NOT-LEAK"; +constexpr long kFixedTime = 1440938160; // 20150830T123600Z + +inline Credentials sigv4_credentials(const std::string& region = "us-east-1") { + AwsCredentials a; + a.access_key_id = kAccess; + a.secret_access_key = kSecret; + return Credentials::sigv4(a, region); +} + +struct Rig { + MockTransport* mock = nullptr; + std::shared_ptr client; + + explicit Rig(Credentials credentials = sigv4_credentials()) { + auto t = std::make_unique(); + mock = t.get(); + client = std::make_shared(std::move(t), std::move(credentials), [] { return std::time_t(kFixedTime); }); + } + Runtime runtime() const { return Runtime(client); } + Models models() const { return Models(client); } + Guardrails guardrails() const { return Guardrails(client); } + BatchJobs jobs() const { return BatchJobs(client); } + AgentRuntime agents() const { return AgentRuntime(client); } + + void expect(Method m, const std::string& path, ClientResponse r, int times = 1) { + MockExpectation e; + e.method = m; + e.path = path; + e.response = std::move(r); + e.times = times; + mock->expect(e); + } + // Frame builders (the documented layout; see event_stream.h). + static std::string event(const std::string& type, const std::string& json) { + return EventStreamDecoder::encode({{":message-type", "event"}, {":event-type", type}, {":content-type", "application/json"}}, json); + } + static std::string exception(const std::string& type, const std::string& json) { + return EventStreamDecoder::encode({{":message-type", "exception"}, {":exception-type", type}, {":content-type", "application/json"}}, json); + } +}; + +} // namespace bedrock_test diff --git a/tests/src/vendors/bedrock/test_calls.cpp b/tests/src/vendors/bedrock/test_calls.cpp new file mode 100644 index 0000000..a43bb8f --- /dev/null +++ b/tests/src/vendors/bedrock/test_calls.cpp @@ -0,0 +1,117 @@ +// The call table, credentials, endpoints and caps. +#include +#include + +#include "support.h" + +using namespace bedrock_test; + +TEST(bedrock_calls, table_rows_are_unique_and_self_consistent) { + std::set ops; + std::set kinds; + for (std::size_t i = 0; i < Calls::count(); ++i) { + const CallSpec& s = Calls::at(i); + ASSERT_TRUE(ops.insert(s.op).second); + ASSERT_TRUE(kinds.insert(static_cast(s.call)).second); + ASSERT_TRUE(std::string(s.http_method) == "GET" || std::string(s.http_method) == "POST"); + ASSERT_EQ(s.uri[0], '/'); + // Every {placeholder} is a declared path param and vice versa. + std::string uri = s.uri, params = std::string(",") + s.path_params + ","; + std::size_t placeholders = 0; + for (std::size_t at = uri.find('{'); at != std::string::npos; at = uri.find('{', at + 1)) { + std::string name = uri.substr(at + 1, uri.find('}', at) - at - 1); + ASSERT_TRUE(params.find("," + name + ",") != std::string::npos); + ++placeholders; + } + std::size_t declared = std::string(s.path_params).empty() ? 0 : 1; + for (const char* c = s.path_params; *c; ++c) declared += (*c == ','); + ASSERT_EQ(placeholders, declared); + ASSERT_EQ(&Calls::spec_of(s.call), &s); + // A GET never has a body; event-stream rows are POST. + if (std::string(s.http_method) == "GET") ASSERT_EQ(std::string(s.body_fields), ""); + if (s.event_stream) ASSERT_EQ(std::string(s.http_method), "POST"); + } + ASSERT_EQ(Calls::count(), 18u); + std::set names; + for (std::size_t i = 0; i < Calls::schema_count(); ++i) ASSERT_TRUE(names.insert(std::string(Calls::schema_at(i).service) + "/" + Calls::schema_at(i).schema).second); + // Every response schema named by a row exists in the schema table of its service. + for (std::size_t i = 0; i < Calls::count(); ++i) { + const CallSpec& s = Calls::at(i); + const char* svc = s.service == AwsService::kRuntime ? "bedrock-runtime" : s.service == AwsService::kControl ? "bedrock" : "bedrock-agent-runtime"; + if (!names.count(std::string(svc) + "/" + s.response)) FAIL(s.op << ": no schema row " << s.response << " for " << svc); + } +} + +TEST(bedrock_calls, only_the_agent_runtime_refuses_bedrock_api_keys) { + for (std::size_t i = 0; i < Calls::count(); ++i) { + const CallSpec& s = Calls::at(i); + ASSERT_EQ(s.bearer_ok, s.service != AwsService::kAgentRuntime); + } +} + +TEST(bedrock_calls, render_encodes_ids_and_refuses_odd_ones) { + ASSERT_EQ(Calls::render(Call::kConverse, {{"modelId", "anthropic.claude-3-sonnet-20240229-v1:0"}}), + "/model/anthropic.claude-3-sonnet-20240229-v1%3A0/converse"); + ASSERT_EQ(Calls::render(Call::kConverse, {{"modelId", "arn:aws:bedrock:us-east-1:123456789012:inference-profile/us.x-v1:0"}}), + "/model/arn%3Aaws%3Abedrock%3Aus-east-1%3A123456789012%3Ainference-profile%2Fus.x-v1%3A0/converse"); + ASSERT_THROW(Calls::render(Call::kConverse, {{"modelId", "a b"}}), molo::llm::ApiError); + ASSERT_THROW(Calls::render(Call::kConverse, {{"modelId", "a?b=c"}}), molo::llm::ApiError); + ASSERT_THROW(Calls::render(Call::kConverse, {{"modelId", "%2e%2e"}}), molo::llm::ApiError); + ASSERT_THROW(Calls::render(Call::kConverse, {{"modelId", ".."}}), molo::llm::ApiError); + ASSERT_THROW(Calls::render(Call::kConverse, {{"modelId", std::string(2049, 'a')}}), molo::llm::ApiError); + ASSERT_THROW(Calls::render(Call::kConverse, {}), molo::llm::ApiError); + ASSERT_EQ(Calls::render(Call::kApplyGuardrail, {{"guardrailIdentifier", "abc123"}, {"guardrailVersion", "DRAFT"}}), + "/guardrail/abc123/version/DRAFT/apply"); +} + +TEST(bedrock_endpoints, hosts_follow_the_service_prefixes_and_regions_are_validated) { + ASSERT_EQ(Endpoints::host(AwsService::kRuntime, "us-west-2"), "bedrock-runtime.us-west-2.amazonaws.com"); + ASSERT_EQ(Endpoints::host(AwsService::kControl, "eu-central-1"), "bedrock.eu-central-1.amazonaws.com"); + ASSERT_EQ(Endpoints::host(AwsService::kAgentRuntime, "ap-southeast-2"), "bedrock-agent-runtime.ap-southeast-2.amazonaws.com"); + ASSERT_EQ(Endpoints::host(AwsService::kRuntime, "cn-north-1"), "bedrock-runtime.cn-north-1.amazonaws.com.cn"); + ASSERT_THROW(Endpoints::host(AwsService::kRuntime, "us-east-1.evil.com/"), molo::llm::ApiError); + ASSERT_THROW(Endpoints::host(AwsService::kRuntime, "US-EAST-1"), molo::llm::ApiError); + ASSERT_THROW(Endpoints::host(AwsService::kRuntime, ""), molo::llm::ApiError); + ASSERT_THROW(Endpoints::host(AwsService::kRuntime, std::string(33, 'a')), molo::llm::ApiError); +} + +TEST(bedrock_credentials, environment_selects_the_mode_and_bearer_wins) { + setenv("MOLO_ENV_FILES", "off", 1); + for (const char* v : {"AWS_BEARER_TOKEN_BEDROCK", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN", "AWS_REGION", "AWS_DEFAULT_REGION"}) unsetenv(v); + Credentials none = Credentials::from_env(); + ASSERT_FALSE(none.configured()); + ASSERT_EQ(std::string(none.mode_name()), "none"); + ASSERT_EQ(none.region, "us-east-1"); + + setenv("AWS_ACCESS_KEY_ID", "AKIDTEST", 1); + ASSERT_FALSE(Credentials::from_env().configured()); // a key id alone is not a credential + setenv("AWS_SECRET_ACCESS_KEY", "secret", 1); + setenv("AWS_SESSION_TOKEN", "tok", 1); + setenv("AWS_DEFAULT_REGION", "eu-west-3", 1); + Credentials sig = Credentials::from_env(); + ASSERT_EQ(std::string(sig.mode_name()), "sigv4"); + ASSERT_EQ(sig.aws.session_token, "tok"); + ASSERT_EQ(sig.region, "eu-west-3"); + setenv("AWS_REGION", "us-west-2", 1); + ASSERT_EQ(Credentials::from_env().region, "us-west-2"); // AWS_REGION beats AWS_DEFAULT_REGION + setenv("AWS_REGION", "not a region", 1); + ASSERT_EQ(Credentials::from_env().region, "us-east-1"); // an invalid name falls back loudly in status, never into a host + + setenv("AWS_BEARER_TOKEN_BEDROCK", "key", 1); + Credentials both = Credentials::from_env(); + ASSERT_EQ(std::string(both.mode_name()), "bearer"); + ASSERT_TRUE(both.aws.valid()); // kept for the agent runtime host + for (const char* v : {"AWS_BEARER_TOKEN_BEDROCK", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN", "AWS_REGION", "AWS_DEFAULT_REGION"}) unsetenv(v); +} + +TEST(bedrock_caps, no_cap_is_unbounded) { + ASSERT_EQ(BedrockCaps::kMaxInvokeBodyBytes, 25000000u); + ASSERT_EQ(BedrockCaps::kMaxEventMessageBytes, 16u * 1024u * 1024u); + ASSERT_EQ(BedrockCaps::kMaxPages, 20u); + // An oversized InvokeModel body is refused before signing. + Rig rig; + std::string big(BedrockCaps::kMaxInvokeBodyBytes + 1, 'x'); + InvokeOptions o; + ASSERT_THROW(rig.runtime().invoke_model_raw("m", big, o), molo::llm::ApiError); + ASSERT_EQ(rig.mock->send_count(), 0u); +} diff --git a/tests/src/vendors/bedrock/test_converse.cpp b/tests/src/vendors/bedrock/test_converse.cpp new file mode 100644 index 0000000..f3c9259 --- /dev/null +++ b/tests/src/vendors/bedrock/test_converse.cpp @@ -0,0 +1,308 @@ +// Converse request rules, result parsing, stream accumulation and the +// ChatModel mapping. DOC fixtures come from the AWS API reference; MODEL ones +// are shaped by the bedrock-runtime service model (ConverseStreamOutput). +#include "support.h" + +using namespace bedrock_test; + +namespace { + +ConverseRequest base() { + ConverseRequest r; + r.model_id = "us.amazon.nova-micro-v1:0"; + r.add_user("hi"); + return r; +} + +// DOC: API_runtime_Converse "Send a message with additional model fields", response. +const char* kDocAdditionalFieldsResponse = + "{\"output\":{\"message\":{\"content\":[{\"text\":\"\"}],\"role\":\"assistant\"}}," + "\"additionalModelResponseFields\":{\"stop_sequence\":\"SUCCESS\"},\"stopReason\":\"stop_sequence\"," + "\"usage\":{\"inputTokens\":51,\"outputTokens\":442,\"totalTokens\":493},\"metrics\":{\"latencyMs\":7944}}"; + +void expect_invalid(const std::function& f, const std::string& needle) { + try { + f(); + } catch (const molo::llm::ApiError& e) { + ASSERT_EQ(e.type(), "INVALID_ARGUMENT"); + if (!has(e.message(), needle)) FAIL("message '" << e.message() << "' lacks '" << needle << "'"); + return; + } + FAIL("expected INVALID_ARGUMENT mentioning " << needle); +} + +} // namespace + +TEST(bedrock_converse, request_rules_are_enforced_before_any_call) { + expect_invalid([] { ConverseRequest r; r.add_user("x"); r.validate(); }, "modelId"); + expect_invalid([] { ConverseRequest r; r.model_id = "m"; r.validate(); }, "message"); + expect_invalid([] { auto r = base(); r.temperature = -0.1; r.validate(); }, "temperature"); + expect_invalid([] { auto r = base(); r.top_p = 1.1; r.validate(); }, "topP"); + expect_invalid([] { auto r = base(); r.max_tokens = 0; r.validate(); }, "maxTokens"); + expect_invalid([] { auto r = base(); r.tool_choice = "auto"; r.validate(); }, "needs tools"); + expect_invalid([] { auto r = base(); r.guardrail_id = "g"; r.validate(); }, "guardrailVersion"); + expect_invalid([] { auto r = base(); r.guardrail_stream_mode = "sync"; r.validate(false); }, "ConverseStream only"); + expect_invalid([] { auto r = base(); r.latency = "turbo"; r.validate(); }, "latency"); + expect_invalid([] { auto r = base(); r.service_tier = "gold"; r.validate(); }, "service tier"); + expect_invalid([] { auto r = base(); for (int i = 0; i < 17; ++i) r.request_metadata["k" + std::to_string(i)] = "v"; r.validate(); }, "16"); + expect_invalid([] { auto r = base(); r.stop_sequences = {""}; r.validate(); }, "stop sequence"); + expect_invalid([] { Blocks::message("system", {Blocks::text("x")}); }, "role"); + expect_invalid([] { Blocks::message("user", {}); }, "content block"); + expect_invalid([] { Blocks::image("bmp", "x"); }, "png"); + expect_invalid([] { Blocks::tool("bad name!", "d", Json::object()); }, "tool name"); + expect_invalid([] { Blocks::tool_use("", "t", Json::object()); }, "toolUseId"); + expect_invalid([] { Blocks::document("pdf", "", "x"); }, "name"); + auto many = base(); + for (std::size_t i = 0; i + 1 < BedrockCaps::kMaxMessages; ++i) many.add_user("x"); + ASSERT_THROW(many.add_user("one too many"), molo::llm::ApiError); +} + +TEST(bedrock_converse, full_request_maps_every_documented_member) { + ConverseRequest r = base(); + r.add_system("be brief"); + r.max_tokens = 100; + r.temperature = 0.2; + r.top_p = 0.9; + r.stop_sequences = {"END"}; + r.tools.push_back(Blocks::tool("get_weather", "Weather", Json::parse("{\"type\":\"object\",\"properties\":{\"city\":{\"type\":\"string\"}}}"))); + r.tool_choice = "get_weather"; + r.additional_fields = Json::parse("{\"top_k\":200}"); + r.additional_response_paths = {"/stop_sequence"}; + r.guardrail_id = "gr1"; + r.guardrail_version = "2"; + r.guardrail_trace = "enabled"; + r.guardrail_stream_mode = "async"; + r.latency = "optimized"; + r.service_tier = "priority"; + r.request_metadata["team"] = "a"; + r.output_schema = Json::parse("{\"type\":\"object\"}"); + r.effort = "high"; + Json j = r.to_json(true); + ASSERT_EQ(j.find("inferenceConfig")->find("maxTokens")->as_int(), 100); + ASSERT_EQ(j.find("inferenceConfig")->find("stopSequences")->as_array()[0].as_string(), "END"); + ASSERT_EQ(j.find("toolConfig")->find("toolChoice")->find("tool")->find("name")->as_string(), "get_weather"); + ASSERT_EQ(j.find("toolConfig")->find("tools")->as_array()[0].find("toolSpec")->find("inputSchema")->find("json")->find("type")->as_string(), "object"); + ASSERT_EQ(j.find("guardrailConfig")->find("streamProcessingMode")->as_string(), "async"); + ASSERT_EQ(j.find("additionalModelRequestFields")->find("top_k")->as_int(), 200); + ASSERT_EQ(j.find("performanceConfig")->find("latency")->as_string(), "optimized"); + ASSERT_EQ(j.find("serviceTier")->find("type")->as_string(), "priority"); + ASSERT_EQ(j.find("requestMetadata")->find("team")->as_string(), "a"); + const Json* tf = j.find("outputConfig")->find("textFormat"); + ASSERT_EQ(tf->find("type")->as_string(), "json_schema"); + // The schema travels as a JSON string (JsonSchemaDefinition.schema is a string). + ASSERT_EQ(tf->find("structure")->find("jsonSchema")->find("schema")->as_string(), "{\"type\":\"object\"}"); + ASSERT_EQ(j.find("outputConfig")->find("effort")->as_string(), "high"); + // The non-stream body omits the stream-only member. + r.guardrail_stream_mode.clear(); + ASSERT_FALSE(r.to_json(false).find("guardrailConfig")->has("streamProcessingMode")); + // CountTokens' converse input holds only messages, system, toolConfig, additional fields. + Json c = r.to_count_json(); + ASSERT_TRUE(c.find("converse")->has("toolConfig")); + ASSERT_FALSE(c.find("converse")->has("inferenceConfig")); +} + +TEST(bedrock_converse, image_and_document_bytes_travel_as_base64) { + Json img = Blocks::image("png", std::string("\x89PNG", 4)); + ASSERT_EQ(img.find("image")->find("source")->find("bytes")->as_string(), "iVBORw=="); + Json doc = Blocks::document("txt", "notes", "hello"); + ASSERT_EQ(doc.find("document")->find("source")->find("bytes")->as_string(), "aGVsbG8="); + ASSERT_EQ(doc.find("document")->find("name")->as_string(), "notes"); +} + +TEST(bedrock_converse, documented_response_with_additional_fields_parses) { + ConverseResult r(Json::parse(kDocAdditionalFieldsResponse)); + ASSERT_EQ(r.stop_reason(), "stop_sequence"); + ASSERT_EQ(r.usage().total_tokens, 493); + ASSERT_EQ(r.latency_ms(), 7944); + ASSERT_EQ(r.raw().find("additionalModelResponseFields")->find("stop_sequence")->as_string(), "SUCCESS"); + ASSERT_EQ(r.assistant_message().find("role")->as_string(), "assistant"); + ASSERT_TRUE(r.tool_uses().empty()); +} + +TEST(bedrock_converse, tool_use_and_reasoning_blocks_are_extracted) { + // MODEL: ContentBlock union members reasoningContent / toolUse. + ConverseResult r(Json::parse( + "{\"output\":{\"message\":{\"role\":\"assistant\",\"content\":[" + "{\"reasoningContent\":{\"reasoningText\":{\"text\":\"think\",\"signature\":\"sig==\"}}}," + "{\"text\":\"Checking.\"}," + "{\"toolUse\":{\"toolUseId\":\"tu1\",\"name\":\"get_weather\",\"input\":{\"city\":\"Oslo\"}}}]}}," + "\"stopReason\":\"tool_use\",\"usage\":{\"inputTokens\":1,\"outputTokens\":2,\"totalTokens\":3,\"cacheReadInputTokens\":4},\"metrics\":{\"latencyMs\":5}}")); + ASSERT_EQ(r.text(), "Checking."); + ASSERT_EQ(r.reasoning(), "think"); + ASSERT_EQ(r.reasoning_signature(), "sig=="); + auto uses = r.tool_uses(); + ASSERT_EQ(uses.size(), 1u); + ASSERT_EQ(uses[0].name, "get_weather"); + ASSERT_EQ(uses[0].input.find("city")->as_string(), "Oslo"); + ASSERT_EQ(r.usage().cache_read_tokens, 4); + auto resp = BedrockChatModel::to_response(r, "m"); + ASSERT_EQ(resp.finish_reason, "tool_calls"); + ASSERT_EQ(resp.tool_calls().size(), 1u); + ASSERT_EQ(resp.usage.cached_tokens, 4); + ASSERT_EQ(resp.text(), "Checking."); +} + +TEST(bedrock_converse, stream_events_accumulate_into_the_same_shape_as_a_plain_reply) { + Rig rig; + // MODEL: one event per ConverseStreamOutput member, payload = the event structure. + std::string frames = + Rig::event("messageStart", "{\"role\":\"assistant\"}") + + Rig::event("contentBlockDelta", "{\"contentBlockIndex\":0,\"delta\":{\"text\":\"Hel\"}}") + + Rig::event("contentBlockDelta", "{\"contentBlockIndex\":0,\"delta\":{\"text\":\"lo\"}}") + + Rig::event("contentBlockStop", "{\"contentBlockIndex\":0}") + + Rig::event("contentBlockStart", "{\"contentBlockIndex\":1,\"start\":{\"toolUse\":{\"toolUseId\":\"tu9\",\"name\":\"lookup\"}}}") + + Rig::event("contentBlockDelta", "{\"contentBlockIndex\":1,\"delta\":{\"toolUse\":{\"input\":\"{\\\"q\\\":\"}}}") + + Rig::event("contentBlockDelta", "{\"contentBlockIndex\":1,\"delta\":{\"toolUse\":{\"input\":\"\\\"x\\\"}\"}}}") + + Rig::event("contentBlockStop", "{\"contentBlockIndex\":1}") + + Rig::event("messageStop", "{\"stopReason\":\"tool_use\"}") + + Rig::event("metadata", "{\"usage\":{\"inputTokens\":7,\"outputTokens\":9,\"totalTokens\":16},\"metrics\":{\"latencyMs\":321}}"); + rig.expect(Method::kPost, "/model/us.amazon.nova-micro-v1%3A0/converse-stream", stream_reply(frames)); + std::string live; + int events = 0; + ConverseResult r = rig.runtime().converse_stream(base(), [&](const StreamDelta& d) { + ++events; + live += d.text; + return true; + }); + ASSERT_EQ(events, 10); + ASSERT_EQ(live, "Hello"); + ASSERT_EQ(r.text(), "Hello"); + ASSERT_EQ(r.stop_reason(), "tool_use"); + ASSERT_EQ(r.usage().total_tokens, 16); + ASSERT_EQ(r.latency_ms(), 321); + auto uses = r.tool_uses(); + ASSERT_EQ(uses.size(), 1u); + ASSERT_EQ(uses[0].id, "tu9"); + ASSERT_EQ(uses[0].input.find("q")->as_string(), "x"); +} + +TEST(bedrock_converse, a_stream_exception_frame_becomes_an_api_error_and_early_stop_is_quiet) { + Rig rig; + rig.expect(Method::kPost, "/model/us.amazon.nova-micro-v1%3A0/converse-stream", + stream_reply(Rig::event("messageStart", "{\"role\":\"assistant\"}") + + Rig::exception("throttlingException", "{\"message\":\"Too many requests, please wait\"}"))); + try { + rig.runtime().converse_stream(base(), [](const StreamDelta&) { return true; }); + FAIL("expected throttling"); + } catch (const molo::llm::ApiError& e) { + ASSERT_EQ(e.http_status(), 429); + ASSERT_EQ(e.type(), "throttlingException"); + } + Rig rig2; + rig2.expect(Method::kPost, "/model/us.amazon.nova-micro-v1%3A0/converse-stream", + stream_reply(Rig::event("messageStart", "{\"role\":\"assistant\"}") + Rig::event("messageStop", "{\"stopReason\":\"end_turn\"}"))); + int n = 0; + rig2.runtime().converse_stream(base(), [&](const StreamDelta&) { return ++n < 1; }); + ASSERT_EQ(n, 1); +} + +TEST(bedrock_converse, a_json_error_in_place_of_a_stream_is_thrown) { + Rig rig; + rig.expect(Method::kPost, "/model/us.amazon.nova-micro-v1%3A0/converse-stream", reply("{\"message\":\"Could not resolve the foundation model\"}", 200)); + try { + rig.runtime().converse_stream(base(), [](const StreamDelta&) { return true; }); + FAIL("expected the error document"); + } catch (const molo::llm::ApiError& e) { + ASSERT_TRUE(has(e.message(), "foundation model")); + } +} + +TEST(bedrock_chat_model, neutral_request_maps_to_converse_and_back) { + Rig rig; + auto model = std::make_shared(std::make_shared(rig.client)); + molo::llm::ChatRequest req; + req.system = "be brief"; + req.messages.push_back(molo::llm::Message::user("weather?")); + molo::llm::Message a; + a.role = molo::llm::Role::kAssistant; + a.parts.push_back(molo::llm::ContentPart::of_text("Let me look.")); + molo::llm::ToolCall call; + call.id = "tu1"; + call.name = "get_weather"; + call.arguments = Json::parse("{\"city\":\"Oslo\"}"); + a.parts.push_back(molo::llm::ContentPart::of_call(call)); + req.messages.push_back(a); + // Two tool results in two Tool messages merge into one user message. + for (const char* id : {"tu1", "tu2"}) { + molo::llm::Message t; + t.role = molo::llm::Role::kTool; + t.parts.push_back(molo::llm::ContentPart::of_result({id, "sunny", false})); + req.messages.push_back(t); + } + molo::llm::ToolSpec spec; + spec.name = "get_weather"; + spec.description = "Weather"; + req.tools.push_back(spec); + req.tool_choice = "required"; + req.max_tokens = 64; + req.temperature = 0.3; + ConverseRequest wire = model->to_converse(req); + ASSERT_EQ(wire.model_id, BedrockChatModel::kDefaultModel); + ASSERT_EQ(wire.messages.size(), 3u); + ASSERT_EQ(wire.messages[2].find("content")->as_array().size(), 2u); + ASSERT_EQ(wire.messages[2].find("role")->as_string(), "user"); + ASSERT_EQ(wire.tool_choice, "any"); + ASSERT_EQ(wire.system.size(), 1u); + + req.tool_choice = "none"; + ASSERT_TRUE(model->to_converse(req).tools.empty()); + req.tool_choice = ""; + req.json_mode = true; + expect_invalid([&] { model->to_converse(req); }, "response_schema"); + req.json_mode = false; + req.extra = Json::parse("{\"mystery\":1}"); + expect_invalid([&] { model->to_converse(req); }, "unsupported extra key"); + req.extra = Json::parse("{\"guardrailIdentifier\":\"g\",\"guardrailVersion\":\"1\",\"serviceTier\":\"flex\"}"); + ASSERT_EQ(model->to_converse(req).service_tier, "flex"); + molo::llm::ChatRequest img; + molo::llm::Message m = molo::llm::Message::user("look"); + molo::llm::ImageData id; + id.mime = "image/png"; + id.url = "https://x/y.png"; + m.parts.push_back(molo::llm::ContentPart::of_image(id)); + img.messages.push_back(m); + expect_invalid([&] { model->to_converse(img); }, "image bytes"); + + // generate() end to end over the mock. + Rig rig2; + auto model2 = std::make_shared(std::make_shared(rig2.client), "anthropic.claude-3-sonnet-20240229-v1:0"); + rig2.expect(Method::kPost, "/model/anthropic.claude-3-sonnet-20240229-v1%3A0/converse", + reply("{\"output\":{\"message\":{\"content\":[{\"text\":\"ok\"}],\"role\":\"assistant\"}},\"stopReason\":\"max_tokens\",\"usage\":{\"inputTokens\":3,\"outputTokens\":4,\"totalTokens\":7},\"metrics\":{\"latencyMs\":1}}")); + molo::llm::ChatRequest simple; + simple.messages.push_back(molo::llm::Message::user("hi")); + molo::llm::ChatResponse resp = model2->generate(simple); + ASSERT_EQ(resp.text(), "ok"); + ASSERT_EQ(resp.finish_reason, "length"); + ASSERT_EQ(resp.usage.total_tokens, 7); + ASSERT_EQ(resp.model, "anthropic.claude-3-sonnet-20240229-v1:0"); + ASSERT_EQ(model2->vendor(), "bedrock"); +} + +TEST(bedrock_chat_model, stream_maps_deltas_tool_starts_stop_and_usage) { + Rig rig; + auto model = std::make_shared(std::make_shared(rig.client)); + rig.expect(Method::kPost, "/model/us.amazon.nova-micro-v1%3A0/converse-stream", + stream_reply(Rig::event("messageStart", "{\"role\":\"assistant\"}") + + Rig::event("contentBlockDelta", "{\"contentBlockIndex\":0,\"delta\":{\"text\":\"Hi\"}}") + + Rig::event("contentBlockStart", "{\"contentBlockIndex\":1,\"start\":{\"toolUse\":{\"toolUseId\":\"t\",\"name\":\"n\"}}}") + + Rig::event("contentBlockDelta", "{\"contentBlockIndex\":1,\"delta\":{\"toolUse\":{\"input\":\"{}\"}}}") + + Rig::event("messageStop", "{\"stopReason\":\"end_turn\"}") + + Rig::event("metadata", "{\"usage\":{\"inputTokens\":1,\"outputTokens\":2,\"totalTokens\":3},\"metrics\":{\"latencyMs\":1}}"))); + molo::llm::ChatRequest req; + req.messages.push_back(molo::llm::Message::user("x")); + std::string text, tool, args, finish; + int total = 0; + model->stream(req, [&](const molo::llm::ChatChunk& c) { + text += c.text_delta; + tool += c.tool_call_name; + args += c.tool_args_delta; + if (!c.finish_reason.empty()) finish = c.finish_reason; + if (c.usage) total = c.usage->total_tokens; + return true; + }); + ASSERT_EQ(text, "Hi"); + ASSERT_EQ(tool, "n"); + ASSERT_EQ(args, "{}"); + ASSERT_EQ(finish, "stop"); + ASSERT_EQ(total, 3); +} diff --git a/tests/src/vendors/bedrock/test_eventstream.cpp b/tests/src/vendors/bedrock/test_eventstream.cpp new file mode 100644 index 0000000..75592b0 --- /dev/null +++ b/tests/src/vendors/bedrock/test_eventstream.cpp @@ -0,0 +1,191 @@ +// vnd.amazon.eventstream framing: CRC32 check value, round trips, one byte at +// a time feeding, corruption, truncation, caps, error documents. +#include "support.h" +#include "vendors/bedrock/crc32.h" + +using namespace bedrock_test; + +TEST(bedrock_eventstream, crc32_check_value_is_the_standard_one) { + // CRC-32/ISO-HDLC check value of "123456789". + ASSERT_EQ(Crc32::of(std::string("123456789")), 0xCBF43926u); + ASSERT_EQ(Crc32::of(std::string("")), 0u); + // Piecewise equals whole. + std::uint32_t part = Crc32::of(std::string("12345")); + ASSERT_EQ(Crc32::of(std::string("6789"), part), 0xCBF43926u); +} + +TEST(bedrock_eventstream, round_trip_with_headers_and_payload) { + std::string frame = Rig::event("contentBlockDelta", "{\"contentBlockIndex\":0,\"delta\":{\"text\":\"Hi\"}}"); + std::vector got; + EventStreamDecoder d("bedrock", [&](const EventMessage& m) { + got.push_back(m); + return true; + }); + d.feed(frame); + d.finish(); + ASSERT_EQ(got.size(), 1u); + ASSERT_EQ(got[0].message_type(), "event"); + ASSERT_EQ(got[0].event_type(), "contentBlockDelta"); + ASSERT_EQ(got[0].header(":content-type"), "application/json"); + ASSERT_TRUE(has(got[0].payload, "\"text\":\"Hi\"")); +} + +TEST(bedrock_eventstream, one_byte_at_a_time_and_two_messages_in_one_chunk) { + std::string a = Rig::event("messageStart", "{\"role\":\"assistant\"}"); + std::string b = Rig::event("messageStop", "{\"stopReason\":\"end_turn\"}"); + std::vector types; + EventStreamDecoder d("bedrock", [&](const EventMessage& m) { + types.push_back(m.event_type()); + return true; + }); + for (char c : a + b) d.feed(std::string(1, c)); + d.finish(); + ASSERT_EQ(types.size(), 2u); + ASSERT_EQ(types[1], "messageStop"); + types.clear(); + EventStreamDecoder d2("bedrock", [&](const EventMessage& m) { + types.push_back(m.event_type()); + return true; + }); + d2.feed(a + b); + d2.finish(); + ASSERT_EQ(types.size(), 2u); +} + +TEST(bedrock_eventstream, every_header_value_type_is_parsed) { + // Hand-built header block: true, false, byte, int16, int32, int64, bytes, string, timestamp, uuid. + std::string h; + auto name = [&](const char* n) { + h.push_back(static_cast(std::string(n).size())); + h += n; + }; + name("t"); h.push_back(0); + name("f"); h.push_back(1); + name("b"); h.push_back(2); h.push_back(static_cast(0xFE)); + name("s"); h.push_back(3); h.push_back(0x01); h.push_back(0x02); + name("i"); h.push_back(4); h.append("\x00\x00\x01\x00", 4); + name("l"); h.push_back(5); h.append("\x00\x00\x00\x00\x00\x00\x00\x07", 8); + name("y"); h.push_back(6); h.append("\x00\x02xy", 4); + name("z"); h.push_back(7); h.append("\x00\x03" "abc", 5); + name("m"); h.push_back(8); h.append("\x00\x00\x00\x00\x00\x00\x00\x09", 8); + name("u"); h.push_back(9); h.append(16, 'U'); + std::string out; + auto put32 = [&](std::uint32_t v) { for (int s = 24; s >= 0; s -= 8) out.push_back(static_cast((v >> s) & 0xFF)); }; + put32(static_cast(16 + h.size() + 2)); + put32(static_cast(h.size())); + put32(Crc32::of(out)); + out += h; + out += "{}"; + put32(Crc32::of(out)); + EventMessage got; + EventStreamDecoder d("bedrock", [&](const EventMessage& m) { got = m; return true; }); + d.feed(out); + d.finish(); + ASSERT_EQ(got.headers.at("t").number, 1); + ASSERT_EQ(got.headers.at("f").number, 0); + ASSERT_EQ(got.headers.at("b").number, -2); + ASSERT_EQ(got.headers.at("s").number, 258); + ASSERT_EQ(got.headers.at("i").number, 256); + ASSERT_EQ(got.headers.at("l").number, 7); + ASSERT_EQ(got.headers.at("y").text, "xy"); + ASSERT_EQ(got.headers.at("z").text, "abc"); + ASSERT_EQ(got.headers.at("m").number, 9); + ASSERT_EQ(got.headers.at("u").text.size(), 16u); + ASSERT_EQ(got.payload, "{}"); +} + +TEST(bedrock_eventstream, a_flipped_bit_is_a_crc_error_in_prelude_or_message) { + std::string frame = Rig::event("chunk", "{\"bytes\":\"AAAA\"}"); + auto run = [](std::string f) { + EventStreamDecoder d("bedrock", [](const EventMessage&) { return true; }); + try { + d.feed(f); + d.finish(); + } catch (const molo::llm::ApiError& e) { + return e.type(); + } + return std::string("no error"); + }; + std::string a = frame; + a[5] ^= 0x01; // headers length: prelude CRC breaks + ASSERT_EQ(run(a), "EVENTSTREAM_CRC"); + std::string b = frame; + b[b.size() - 6] ^= 0x01; // payload: message CRC breaks + ASSERT_EQ(run(b), "EVENTSTREAM_CRC"); + std::string c = frame; + c[c.size() - 1] ^= 0x01; // trailing CRC itself + ASSERT_EQ(run(c), "EVENTSTREAM_CRC"); +} + +TEST(bedrock_eventstream, truncation_is_loud_and_a_stop_is_quiet) { + std::string frame = Rig::event("chunk", "{}"); + EventStreamDecoder d("bedrock", [](const EventMessage&) { return true; }); + d.feed(frame.substr(0, frame.size() - 3)); + ASSERT_THROW(d.finish(), molo::llm::ApiError); + int seen = 0; + EventStreamDecoder stop("bedrock", [&](const EventMessage&) { ++seen; return false; }); + ASSERT_FALSE(stop.feed(frame + frame)); + ASSERT_EQ(seen, 1); + stop.finish(); // stopped by the sink: no truncation complaint +} + +TEST(bedrock_eventstream, impossible_lengths_are_refused_before_buffering) { + std::string out; + auto put32 = [&](std::uint32_t v) { for (int s = 24; s >= 0; s -= 8) out.push_back(static_cast((v >> s) & 0xFF)); }; + put32(0x02000000u); // 32 MiB total: over the 16 MiB cap + put32(0); + put32(Crc32::of(out)); + EventStreamDecoder d("bedrock", [](const EventMessage&) { return true; }); + try { + d.feed(out); + FAIL("expected the size cap"); + } catch (const molo::llm::ApiError& e) { + ASSERT_EQ(e.type(), "EVENTSTREAM_LENGTH"); + } + std::string small; + auto put = [&](std::uint32_t v) { for (int s = 24; s >= 0; s -= 8) small.push_back(static_cast((v >> s) & 0xFF)); }; + put(10); // shorter than the 16 byte minimum + put(0); + put(Crc32::of(small)); + EventStreamDecoder d2("bedrock", [](const EventMessage&) { return true; }); + ASSERT_THROW(d2.feed(small), molo::llm::ApiError); +} + +TEST(bedrock_eventstream, a_json_error_body_in_place_of_a_stream_throws_its_message) { + EventStreamDecoder d("bedrock", [](const EventMessage&) { return true; }); + d.feed("{\"message\":\"The security token "); + d.feed("included in the request is invalid.\"}"); + try { + d.finish(); + FAIL("expected the error document"); + } catch (const molo::llm::ApiError& e) { + ASSERT_TRUE(has(e.message(), "security token")); + ASSERT_EQ(e.type(), "STREAM_HTTP_ERROR"); + } +} + +TEST(bedrock_eventstream, exception_frames_map_to_api_errors) { + std::string frame = Rig::exception("throttlingException", "{\"message\":\"Too many requests\"}"); + EventMessage m; + EventStreamDecoder d("bedrock", [&](const EventMessage& x) { m = x; return true; }); + d.feed(frame); + ASSERT_TRUE(m.is_failure()); + try { + EventStreamDecoder::raise_if_failure("bedrock", m); + FAIL("expected throw"); + } catch (const molo::llm::ApiError& e) { + ASSERT_EQ(e.http_status(), 429); + ASSERT_EQ(e.type(), "throttlingException"); + ASSERT_EQ(e.message(), "Too many requests"); + } + // modelStreamErrorException carries a status of 424 in the service model. + EventMessage ms; + EventStreamDecoder d2("bedrock", [&](const EventMessage& x) { ms = x; return true; }); + d2.feed(Rig::exception("modelStreamErrorException", "{\"message\":\"x\",\"originalStatusCode\":500}")); + try { + EventStreamDecoder::raise_if_failure("bedrock", ms); + FAIL("expected throw"); + } catch (const molo::llm::ApiError& e) { + ASSERT_EQ(e.http_status(), 424); + } +} diff --git a/tests/src/vendors/bedrock/test_live.cpp b/tests/src/vendors/bedrock/test_live.cpp new file mode 100644 index 0000000..a3ae719 --- /dev/null +++ b/tests/src/vendors/bedrock/test_live.cpp @@ -0,0 +1,104 @@ +// Opt-in live checks. MOLO_LIVE_NET=1 enables the network. The probes with a +// deliberately invalid credential need no account. The account tests also need +// AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY (+ AWS_SESSION_TOKEN) or +// AWS_BEARER_TOKEN_BEDROCK (via llm::Config) and are read only, plus ONE tiny +// Converse call that additionally needs MOLO_LIVE_PAID=1 and model access in +// the region. Without them each test prints a loud SKIP. Nothing is created, +// changed or deleted. +#include +#include + +#include "clients/transports/http_transport.h" +#include "support.h" + +using namespace bedrock_test; + +namespace { +// Read once at load: other suites in the same binary unset MOLO_LIVE_NET. +const bool kNet = std::getenv("MOLO_LIVE_NET") != nullptr; +const bool kPaid = std::getenv("MOLO_LIVE_PAID") != nullptr; +bool net() { return kNet; } +bool paid() { return kPaid; } + +std::shared_ptr real(Credentials c) { + return std::make_shared(std::make_unique(), std::move(c)); +} +} // namespace + +// No account needed: the host resolves, TLS works, an invalid credential gets +// a 403 whose error shape the client can read. +TEST(bedrock_live, invalid_bearer_key_is_refused_with_a_readable_error) { + if (!net()) { + std::cout << "SKIP bedrock_live.invalid_bearer_key_is_refused_with_a_readable_error (set MOLO_LIVE_NET=1)\n"; + return; + } + auto c = real(Credentials::bearer_token("invalid-molo-probe-key", "us-east-1")); + try { + Models(c).list(); + FAIL("expected a refusal"); + } catch (const molo::llm::ApiError& e) { + std::cout << " GET /foundation-models with an invalid API key: " << e.http_status() << " " << e.type() << ": " << e.message() << "\n"; + ASSERT_TRUE(e.http_status() == 401 || e.http_status() == 403); + ASSERT_TRUE(!e.type().empty() && e.type() != "UNKNOWN"); + } +} + +TEST(bedrock_live, invalid_sigv4_key_is_refused_on_all_three_hosts) { + if (!net()) { + std::cout << "SKIP bedrock_live.invalid_sigv4_key_is_refused_on_all_three_hosts (set MOLO_LIVE_NET=1)\n"; + return; + } + AwsCredentials a{"AKIDEXAMPLE", "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY", ""}; + auto c = real(Credentials::sigv4(a, "us-east-1")); + auto probe = [&](const char* what, const std::function& f) { + try { + f(); + FAIL(what << ": expected a refusal"); + } catch (const molo::llm::ApiError& e) { + std::cout << " " << what << ": " << e.http_status() << " " << e.type() << ": " << e.message() << "\n"; + ASSERT_TRUE(e.http_status() == 400 || e.http_status() == 401 || e.http_status() == 403 || e.http_status() == 502); + } + }; + probe("control GET /foundation-models", [&] { Models(c).list(); }); + ConverseRequest r; + r.model_id = "amazon.nova-micro-v1:0"; + r.add_user("hi"); + probe("runtime POST converse", [&] { Runtime(c).converse(r); }); + // A stream call: the transport hides the HTTP status, so the error document + // arrives as a non-stream body and is reported as STREAM_HTTP_ERROR (502). + probe("runtime POST converse-stream", [&] { Runtime(c).converse_stream(r, [](const StreamDelta&) { return true; }); }); + RetrieveParams p; + p.knowledge_base_id = "KB12345678"; + p.query = "q"; + probe("agent-runtime POST retrieve", [&] { AgentRuntime(c).retrieve(p); }); +} + +TEST(bedrock_live, models_profiles_guardrails_and_one_tiny_converse) { + Credentials creds = Credentials::from_env(); + if (!net() || !creds.configured()) { + std::cout << "SKIP bedrock_live.models_profiles_guardrails_and_one_tiny_converse (set MOLO_LIVE_NET=1 and AWS_BEARER_TOKEN_BEDROCK or AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY)\n"; + return; + } + auto c = real(creds); + ListResult models = Models(c).list(); + std::cout << " foundation models in " << creds.region << ": " << models.items.as_array().size() << "\n"; + ASSERT_TRUE(!models.items.as_array().empty()); + ListResult profiles = Models(c).list_profiles("SYSTEM_DEFINED", 50); + std::cout << " system inference profiles: " << profiles.items.as_array().size() << (profiles.truncated ? " (truncated)" : "") << "\n"; + ListResult g = Guardrails(c).list(); + std::cout << " guardrails: " << g.items.as_array().size() << "\n"; + ListResult jobs = BatchJobs(c).list(); + std::cout << " batch jobs: " << jobs.items.as_array().size() << "\n"; + if (!paid()) { + std::cout << "SKIP bedrock_live.tiny_converse (set MOLO_LIVE_PAID=1 and BEDROCK_LIVE_MODEL, default " << BedrockChatModel::kDefaultModel << ")\n"; + return; + } + const char* m = std::getenv("BEDROCK_LIVE_MODEL"); + ConverseRequest r; + r.model_id = m ? m : BedrockChatModel::kDefaultModel; + r.add_user("Reply with the single word: ok"); + r.max_tokens = 16; + ConverseResult res = Runtime(c).converse(r); + std::cout << " converse stop=" << res.stop_reason() << " tokens=" << res.usage().total_tokens << "\n"; + ASSERT_TRUE(res.usage().total_tokens > 0); +} diff --git a/tests/src/vendors/bedrock/test_sigv4.cpp b/tests/src/vendors/bedrock/test_sigv4.cpp new file mode 100644 index 0000000..7f30a3f --- /dev/null +++ b/tests/src/vendors/bedrock/test_sigv4.cpp @@ -0,0 +1,169 @@ +// SigV4 signer against AWS's published SigV4 test suite (the "aws4_testsuite" +// directory: .req request, .creq canonical request, .sts string to sign, +// .authz Authorization header). The 96 files in sigv4_suite/ are copied +// verbatim from the botocore mirror of that suite; they are a documented spec, +// not scripted vendor replies. Credentials of the suite: AKIDEXAMPLE / +// wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY, region us-east-1, service "service". +#include + +#include +#include +#include +#include + +#include "framework.h" +#include "llm/error.h" +#include "vendors/bedrock/sigv4.h" + +using namespace molo::bedrock; + +namespace { + +const char* kSuiteDir = "tests/src/vendors/bedrock/sigv4_suite"; +const char* kSecret = "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"; + +std::string slurp(const std::string& path) { + std::ifstream in(path, std::ios::binary); + if (!in) FAIL("cannot read fixture " << path << " (tests run from the repo root)"); + std::ostringstream s; + s << in.rdbuf(); + return s.str(); +} + +std::string rtrim(std::string s) { + while (!s.empty() && (s.back() == '\n' || s.back() == '\r' || s.back() == ' ')) s.pop_back(); + return s; +} + +std::vector suite_cases() { + std::vector names; + DIR* d = opendir(kSuiteDir); + if (!d) FAIL("suite directory missing: " << kSuiteDir); + while (dirent* e = readdir(d)) { + std::string n = e->d_name; + if (n != "." && n != "..") names.push_back(n); + } + closedir(d); + std::sort(names.begin(), names.end()); + return names; +} + +SigV4Request parse_req(const std::string& text) { + SigV4Request r; + std::istringstream in(text); + std::string line; + std::getline(in, line); + if (!line.empty() && line.back() == '\r') line.pop_back(); + std::size_t sp1 = line.find(' '); + std::size_t sp2 = line.rfind(" HTTP/1.1"); + r.method = line.substr(0, sp1); + std::string target = line.substr(sp1 + 1, sp2 - sp1 - 1); + std::size_t q = target.find('?'); + r.path = target.substr(0, q); + if (q != std::string::npos) r.query = target.substr(q + 1); + bool in_body = false; + while (std::getline(in, line)) { + if (!line.empty() && line.back() == '\r') line.pop_back(); + if (in_body) { + r.body += line; + continue; + } + if (line.empty()) { + in_body = true; + continue; + } + if ((line[0] == ' ' || line[0] == '\t') && !r.headers.empty()) { + r.headers.back().second += "\n" + line; // folded header value + continue; + } + std::size_t colon = line.find(':'); + r.headers.emplace_back(line.substr(0, colon), line.substr(colon + 1)); + } + return r; +} + +std::string hex_of(const std::string& raw) { + static const char* kHex = "0123456789abcdef"; + std::string out; + for (unsigned char c : raw) { + out.push_back(kHex[c >> 4]); + out.push_back(kHex[c & 15]); + } + return out; +} + +} // namespace + +TEST(bedrock_sigv4, every_case_of_the_aws_test_suite_matches) { + std::vector names = suite_cases(); + ASSERT_EQ(names.size(), 24u); + for (const std::string& name : names) { + std::string base = std::string(kSuiteDir) + "/" + name + "/" + name; + SigV4Request req = parse_req(slurp(base + ".req")); + AwsCredentials creds; + creds.access_key_id = "AKIDEXAMPLE"; + creds.secret_access_key = kSecret; + if (name == "get-vanilla-with-session-token") + creds.session_token = "6e86291e8372ff2a2260956d9b8aae1d763fbf315fa00fa31553b73ebf194267"; + SigV4Signer signer(creds, "us-east-1", "service"); + SigV4Result r = signer.sign(req, "20150830T123600Z"); + if (r.canonical_request != rtrim(slurp(base + ".creq"))) + FAIL(name << ": canonical request differs:\n" << r.canonical_request); + if (r.string_to_sign != rtrim(slurp(base + ".sts"))) FAIL(name << ": string to sign differs:\n" << r.string_to_sign); + if (r.authorization != rtrim(slurp(base + ".authz"))) FAIL(name << ": authorization differs:\n" << r.authorization); + } + std::cout << " sigv4 suite cases matched: " << names.size() << "\n"; +} + +TEST(bedrock_sigv4, signing_key_matches_the_aws_documentation_example) { + // docs.aws.amazon.com "Examples of how to derive a signing key": iam, 20150830, us-east-1. + std::string key = SigV4Signer::signing_key(kSecret, "20150830", "us-east-1", "iam"); + ASSERT_EQ(hex_of(key), "c4afb1cc5771d871763a393e44b703571b55cc28424d1a5e86da6ed3c154a4b9"); +} + +TEST(bedrock_sigv4, a_bedrock_model_id_path_is_encoded_twice) { + // On the wire the colon is %3A; the canonical URI encodes the percent sign again. + ASSERT_EQ(SigV4Signer::canonical_uri("/model/anthropic.claude-3-sonnet-20240229-v1%3A0/converse"), + "/model/anthropic.claude-3-sonnet-20240229-v1%253A0/converse"); + ASSERT_EQ(SigV4Signer::canonical_uri("/a/../b//c/./d"), "/b/c/d"); + ASSERT_EQ(SigV4Signer::canonical_uri(""), "/"); +} + +TEST(bedrock_sigv4, query_is_decoded_reencoded_and_sorted) { + ASSERT_EQ(SigV4Signer::canonical_query("b=2&a=1&a=0"), "a=0&a=1&b=2"); + ASSERT_EQ(SigV4Signer::canonical_query("k=a%20b&z"), "k=a%20b&z="); + ASSERT_EQ(SigV4Signer::canonical_query("k=a+b"), "k=a%2Bb"); // '+' is data + ASSERT_EQ(SigV4Signer::canonical_query(""), ""); +} + +TEST(bedrock_sigv4, date_format_and_adding_missing_headers) { + ASSERT_EQ(SigV4Signer::format_amz_date(1440938160), "20150830T123600Z"); + AwsCredentials creds{"AKID", "SECRET", "TOKEN"}; + SigV4Signer signer(creds, "eu-west-1", "bedrock"); + SigV4Request req; + req.method = "GET"; + req.path = "/foundation-models"; + req.headers = {{"Host", "bedrock.eu-west-1.amazonaws.com"}}; + SigV4Result r = signer.sign(req, "20250101T000000Z"); + ASSERT_EQ(r.signed_headers, "host;x-amz-date;x-amz-security-token"); + ASSERT_EQ(r.credential_scope, "20250101/eu-west-1/bedrock/aws4_request"); + bool has_date = false, has_token = false, has_auth = false; + for (const auto& [n, v] : r.added_headers) { + if (n == "x-amz-date") has_date = (v == "20250101T000000Z"); + if (n == "x-amz-security-token") has_token = (v == "TOKEN"); + if (n == "Authorization") has_auth = v.rfind("AWS4-HMAC-SHA256 Credential=AKID/20250101/eu-west-1/bedrock/aws4_request", 0) == 0; + } + ASSERT_TRUE(has_date && has_token && has_auth); + ASSERT_EQ(r.authorization.find("SECRET"), std::string::npos); +} + +TEST(bedrock_sigv4, rejects_missing_host_empty_keys_and_bad_dates) { + ASSERT_THROW(SigV4Signer(AwsCredentials{"", "x", ""}, "us-east-1", "bedrock"), molo::llm::ApiError); + SigV4Signer signer(AwsCredentials{"A", "B", ""}, "us-east-1", "bedrock"); + SigV4Request req; + req.method = "GET"; + req.path = "/"; + ASSERT_THROW(signer.sign(req, "20250101T000000Z"), molo::llm::ApiError); + req.headers = {{"Host", "h"}}; + ASSERT_THROW(signer.sign(req, "2025-01-01"), molo::llm::ApiError); +} diff --git a/tests/src/vendors/bedrock/test_wire.cpp b/tests/src/vendors/bedrock/test_wire.cpp new file mode 100644 index 0000000..63f9624 --- /dev/null +++ b/tests/src/vendors/bedrock/test_wire.cpp @@ -0,0 +1,445 @@ +// What the real BedrockClient puts on the wire: signing, bearer, host and path +// encoding, error mapping, services against the call table. Replies are DOC +// (taken from the AWS API reference) or MODEL (shaped by the service model). +#include "support.h" + +using namespace bedrock_test; + +namespace { + +// DOC: API_runtime_Converse "Send a message to a model", sample request. +const char* kDocConverseRequest = + "{\"messages\":[{\"role\":\"user\",\"content\":[{\"text\":\"Write an article about impact of high inflation to GDP of a country\"}]}]," + "\"system\":[{\"text\":\"You are an economist with access to lots of data\"}]," + "\"inferenceConfig\":{\"maxTokens\":1000,\"temperature\":0.5}}"; + +// DOC: API_runtime_Converse, "Example response" for the request above. +const char* kDocConverseResponse = + "{\"output\":{\"message\":{\"content\":[{\"text\":\"\"}],\"role\":\"assistant\"}}," + "\"stopReason\":\"end_turn\",\"usage\":{\"inputTokens\":30,\"outputTokens\":628,\"totalTokens\":658},\"metrics\":{\"latencyMs\":1275}}"; + +const char* kModelPath = "/model/anthropic.claude-3-sonnet-20240229-v1%3A0/converse"; + +ConverseRequest doc_request() { + ConverseRequest r; + r.model_id = "anthropic.claude-3-sonnet-20240229-v1:0"; + r.add_user("Write an article about impact of high inflation to GDP of a country"); + r.add_system("You are an economist with access to lots of data"); + r.max_tokens = 1000; + r.temperature = 0.5; + return r; +} + +std::string header_of(const MockTransport::SeenRequest& s, const char* name) { return s.headers.get_or(name, ""); } + +} // namespace + +TEST(bedrock_wire, converse_is_signed_with_sigv4_and_the_model_id_is_encoded_in_the_path) { + Rig rig; + rig.expect(Method::kPost, kModelPath, reply(kDocConverseResponse)); + ConverseResult r = rig.runtime().converse(doc_request()); + ASSERT_EQ(r.text(), ""); + ASSERT_EQ(r.stop_reason(), "end_turn"); + ASSERT_EQ(r.usage().input_tokens, 30); + ASSERT_EQ(r.usage().output_tokens, 628); + ASSERT_EQ(r.usage().total_tokens, 658); + ASSERT_EQ(r.latency_ms(), 1275); + + auto seen = rig.mock->seen(); + ASSERT_EQ(seen.size(), 1u); + const auto& s = seen[0]; + ASSERT_EQ(s.method, "POST"); + ASSERT_EQ(s.target, kModelPath); + ASSERT_EQ(header_of(s, "Host"), "bedrock-runtime.us-east-1.amazonaws.com"); + ASSERT_EQ(header_of(s, "X-Amz-Date"), "20150830T123600Z"); + ASSERT_EQ(header_of(s, "Content-Type"), "application/json"); + // The body is the documented sample request (JSON equal, our keys sort). + ASSERT_TRUE(Json::parse(s.body).equals(Json::parse(kDocConverseRequest))); + std::string auth = header_of(s, "Authorization"); + ASSERT_TRUE(auth.rfind("AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/bedrock/aws4_request, " + "SignedHeaders=content-type;host;x-amz-date, Signature=", 0) == 0); + ASSERT_EQ(s.target.find(kSecret), std::string::npos); + ASSERT_EQ(auth.find(kSecret), std::string::npos); + + // Recompute the signature independently from what was sent. + SigV4Request again; + again.method = s.method; + again.path = kModelPath; + again.headers = {{"Host", header_of(s, "Host")}, {"Content-Type", header_of(s, "Content-Type")}, {"X-Amz-Date", header_of(s, "X-Amz-Date")}}; + again.body = s.body; + AwsCredentials creds{kAccess, kSecret, ""}; + SigV4Result expect = SigV4Signer(creds, "us-east-1", "bedrock").sign(again, "20150830T123600Z"); + ASSERT_EQ(auth, expect.authorization); +} + +TEST(bedrock_wire, session_token_is_signed_and_sent) { + AwsCredentials a{kAccess, kSecret, "SESSION-TOKEN-VALUE"}; + Rig rig(Credentials::sigv4(a, "eu-central-1")); + rig.expect(Method::kGet, "/foundation-models", reply("{\"modelSummaries\":[]}")); + rig.models().list(); + const auto s = rig.mock->seen()[0]; + ASSERT_EQ(header_of(s, "Host"), "bedrock.eu-central-1.amazonaws.com"); + ASSERT_EQ(header_of(s, "X-Amz-Security-Token"), "SESSION-TOKEN-VALUE"); + ASSERT_TRUE(has(header_of(s, "Authorization"), "SignedHeaders=host;x-amz-date;x-amz-security-token")); + ASSERT_TRUE(has(header_of(s, "Authorization"), "/eu-central-1/bedrock/aws4_request")); +} + +TEST(bedrock_wire, bearer_key_is_sent_as_authorization_and_nothing_is_signed) { + Rig rig(Credentials::bearer_token(kBearer)); + rig.expect(Method::kPost, kModelPath, reply(kDocConverseResponse)); + rig.runtime().converse(doc_request()); + const auto s = rig.mock->seen()[0]; + ASSERT_EQ(header_of(s, "Authorization"), std::string("Bearer ") + kBearer); + ASSERT_FALSE(s.headers.has("X-Amz-Date")); + ASSERT_EQ(s.target.find(kBearer), std::string::npos); + ASSERT_EQ(s.body.find(kBearer), std::string::npos); +} + +TEST(bedrock_wire, a_bearer_only_credential_is_refused_on_the_agent_runtime_host_before_any_traffic) { + Rig rig(Credentials::bearer_token(kBearer)); + AgentInvokeParams p; + p.agent_id = "AGENT12345"; + p.alias_id = "ALIAS12345"; + p.session_id = "session-1"; + p.input_text = "hi"; + try { + rig.agents().invoke_agent(p); + FAIL("expected refusal"); + } catch (const molo::llm::ApiError& e) { + ASSERT_EQ(e.http_status(), 503); + ASSERT_EQ(e.type(), "NOT_CONFIGURED"); + ASSERT_TRUE(has(e.message(), "SigV4")); + ASSERT_EQ(e.message().find(kBearer), std::string::npos); + } + ASSERT_EQ(rig.mock->send_count(), 0u); + // With both credentials present the agent host gets SigV4 and the runtime gets the key. + Credentials both = sigv4_credentials(); + both.mode = AuthMode::kBearer; + both.bearer = kBearer; + Rig rig2(both); + rig2.expect(Method::kPost, "/knowledgebases/KB12345678/retrieve", reply("{\"retrievalResults\":[]}")); + RetrieveParams rp; + rp.knowledge_base_id = "KB12345678"; + rp.query = "What is AWS?"; + rig2.agents().retrieve(rp); + ASSERT_TRUE(has(header_of(rig2.mock->seen()[0], "Authorization"), "AWS4-HMAC-SHA256")); + ASSERT_EQ(header_of(rig2.mock->seen()[0], "Host"), "bedrock-agent-runtime.us-east-1.amazonaws.com"); +} + +TEST(bedrock_wire, no_credential_means_not_configured_and_no_traffic) { + Rig rig(Credentials::none()); + ASSERT_FALSE(rig.client->configured()); + try { + rig.runtime().converse(doc_request()); + FAIL("expected NotConfiguredError"); + } catch (const molo::llm::NotConfiguredError& e) { + ASSERT_EQ(e.http_status(), 503); + ASSERT_TRUE(has(e.message(), "AWS_BEARER_TOKEN_BEDROCK")); + ASSERT_TRUE(has(e.message(), "AWS_ACCESS_KEY_ID")); + } + ASSERT_EQ(rig.mock->send_count(), 0u); +} + +TEST(bedrock_wire, validation_runs_before_the_credential_check) { + Rig rig(Credentials::none()); + ConverseRequest r = doc_request(); + r.temperature = 1.5; + try { + rig.runtime().converse(r); + FAIL("expected invalid"); + } catch (const molo::llm::NotConfiguredError&) { + FAIL("validation must come first"); + } catch (const molo::llm::ApiError& e) { + ASSERT_EQ(e.type(), "INVALID_ARGUMENT"); + } +} + +TEST(bedrock_wire, aws_error_headers_and_bodies_map_to_api_errors) { + Rig rig; + HeaderMap h; + h.set("Content-Type", "application/json"); + h.set("x-amzn-ErrorType", "UnrecognizedClientException:http://internal.amazon.com/coral/com.amazon.coral.service/"); + h.set("x-amzn-RequestId", "req-123"); + // MODEL: restJson1 error: type in x-amzn-ErrorType, text in "message". + rig.expect(Method::kPost, kModelPath, + ClientResponse(403, h, "{\"message\":\"The security token included in the request is invalid.\"}")); + try { + rig.runtime().converse(doc_request()); + FAIL("expected 403"); + } catch (const molo::llm::ApiError& e) { + ASSERT_EQ(e.http_status(), 403); + ASSERT_EQ(e.type(), "UnrecognizedClientException"); + ASSERT_TRUE(has(e.message(), "security token")); + ASSERT_EQ(e.request_id(), "req-123"); + } + // __type fallback with a namespace prefix and no header. + rig.expect(Method::kPost, kModelPath, reply("{\"__type\":\"com.amazon.coral.service#ThrottlingException\",\"Message\":\"slow down\"}", 429)); + try { + rig.runtime().converse(doc_request()); + FAIL("expected 429"); + } catch (const molo::llm::ApiError& e) { + ASSERT_EQ(e.http_status(), 429); + ASSERT_EQ(e.type(), "ThrottlingException"); + ASSERT_EQ(e.message(), "slow down"); + } +} + +TEST(bedrock_wire, the_call_table_decides_what_may_be_sent) { + Rig rig; + CallInput in; + in.call = Call::kListGuardrails; + in.query["nope"] = "1"; + ASSERT_THROW(rig.client->preview(in), molo::llm::ApiError); + CallInput h; + h.call = Call::kConverse; + h.path["modelId"] = "m"; + h.headers.emplace_back("X-Evil", "1"); + ASSERT_THROW(rig.client->preview(h), molo::llm::ApiError); + CallInput p; + p.call = Call::kConverse; + p.path["modelId"] = "a/../b"; + ASSERT_THROW(rig.client->preview(p), molo::llm::ApiError); + CallInput missing; + missing.call = Call::kConverse; + ASSERT_THROW(rig.client->preview(missing), molo::llm::ApiError); + ASSERT_EQ(rig.mock->send_count(), 0u); +} + +TEST(bedrock_wire, preview_shows_the_canonical_request_but_not_the_secret) { + Rig rig; + CallInput in; + in.call = Call::kGetFoundationModel; + in.path["modelIdentifier"] = "amazon.nova-micro-v1:0"; + WirePreview w = rig.client->preview(in); + ASSERT_EQ(w.method, "GET"); + ASSERT_EQ(w.url, "https://bedrock.us-east-1.amazonaws.com/foundation-models/amazon.nova-micro-v1%3A0"); + ASSERT_EQ(w.auth_mode, "sigv4"); + ASSERT_TRUE(has(w.canonical_request, "/foundation-models/amazon.nova-micro-v1%253A0")); + ASSERT_EQ(w.canonical_request.find(kSecret), std::string::npos); +} + +TEST(bedrock_wire, invoke_model_sends_the_raw_body_and_documented_headers) { + Rig rig; + rig.expect(Method::kPost, "/model/amazon.titan-embed-text-v2%3A0/invoke", reply("{\"embedding\":[0.1,0.2],\"inputTextTokenCount\":3}")); + InvokeOptions o; + o.trace = "ENABLED"; + o.guardrail_id = "gr123"; + o.guardrail_version = "DRAFT"; + o.service_tier = "flex"; + Json body = Json::object(); + body.set("inputText", Json("hello")); + InvokeResult r = rig.runtime().invoke_model("amazon.titan-embed-text-v2:0", body, o); + ASSERT_TRUE(has(r.body, "embedding")); + ASSERT_EQ(r.json().find("inputTextTokenCount")->as_int(), 3); + const auto s = rig.mock->seen()[0]; + ASSERT_EQ(s.body, "{\"inputText\":\"hello\"}"); + ASSERT_EQ(header_of(s, "Accept"), "application/json"); + ASSERT_EQ(header_of(s, "X-Amzn-Bedrock-Trace"), "ENABLED"); + ASSERT_EQ(header_of(s, "X-Amzn-Bedrock-GuardrailIdentifier"), "gr123"); + ASSERT_EQ(header_of(s, "X-Amzn-Bedrock-GuardrailVersion"), "DRAFT"); + ASSERT_EQ(header_of(s, "X-Amzn-Bedrock-Service-Tier"), "flex"); + ASSERT_TRUE(has(header_of(s, "Authorization"), "SignedHeaders=accept;content-type;host;x-amz-date;x-amzn-bedrock-")); + InvokeOptions bad; + bad.trace = "loud"; + ASSERT_THROW(rig.runtime().invoke_model("m", body, bad), molo::llm::ApiError); + bad = InvokeOptions{}; + bad.guardrail_id = "g"; + ASSERT_THROW(rig.runtime().invoke_model("m", body, bad), molo::llm::ApiError); +} + +TEST(bedrock_wire, invoke_model_stream_decodes_base64_chunks) { + Rig rig; + // MODEL: PayloadPart.bytes is a blob (base64 in JSON); each chunk holds one model event. + std::string e1 = "{\"type\":\"content_block_delta\",\"delta\":{\"text\":\"Hel\"}}"; + std::string e2 = "{\"type\":\"content_block_delta\",\"delta\":{\"text\":\"lo\"}}"; + auto b64 = [](const std::string& s) { return molo::security::crypto::Base64::encode(s); }; + rig.expect(Method::kPost, "/model/anthropic.claude-3-haiku-20240307-v1%3A0/invoke-with-response-stream", + stream_reply(Rig::event("chunk", "{\"bytes\":\"" + b64(e1) + "\"}") + Rig::event("chunk", "{\"bytes\":\"" + b64(e2) + "\"}"))); + std::vector got; + Json body = Json::object(); + body.set("prompt", Json("x")); + std::size_t n = rig.runtime().invoke_model_stream("anthropic.claude-3-haiku-20240307-v1:0", body, [&](const std::string& c) { + got.push_back(c); + return true; + }); + ASSERT_EQ(n, 2u); + ASSERT_EQ(got[0], e1); + ASSERT_EQ(got[1], e2); + ASSERT_EQ(header_of(rig.mock->seen()[0], "Content-Type"), "application/json"); +} + +TEST(bedrock_wire, list_walks_pages_and_reports_truncation) { + Rig rig; + MockExpectation page2; + page2.method = Method::kGet; + page2.path = "/guardrails"; + page2.predicate = [](const ClientRequest& r) { return r.url().query_value("nextToken").value_or("") == "T2"; }; + page2.response = reply("{\"guardrails\":[{\"id\":\"g2\",\"name\":\"b\"}]}"); + rig.mock->expect(page2); + rig.expect(Method::kGet, "/guardrails", reply("{\"guardrails\":[{\"id\":\"g1\",\"name\":\"a\"}],\"nextToken\":\"T2\"}")); + // expectation order: the first matching one wins, so page 2 is registered first with a predicate. + ListResult r = rig.guardrails().list(); + ASSERT_EQ(r.items.as_array().size(), 2u); + ASSERT_EQ(r.pages, 2u); + ASSERT_FALSE(r.truncated); + + Rig endless; + endless.expect(Method::kGet, "/inference-profiles", reply("{\"inferenceProfileSummaries\":[{\"inferenceProfileId\":\"p\"}],\"nextToken\":\"more\"}"), 100); + ListResult t = endless.models().list_profiles("SYSTEM_DEFINED", 10); + ASSERT_TRUE(t.truncated); + ASSERT_EQ(t.pages, BedrockCaps::kMaxPages); + ASSERT_EQ(t.next_token, "more"); + ASSERT_EQ(endless.mock->send_count(), BedrockCaps::kMaxPages); + ASSERT_EQ(endless.mock->seen()[0].target, "/inference-profiles?maxResults=10&type=SYSTEM_DEFINED"); +} + +TEST(bedrock_wire, retrieve_body_matches_the_documented_sample_and_filters_pass_through) { + Rig rig; + rig.expect(Method::kPost, "/knowledgebases/KB12345678/retrieve", + reply("{\"retrievalResults\":[{\"content\":{\"text\":\"AWS is Amazon Web Services\",\"type\":\"TEXT\"},\"location\":{\"type\":\"S3\",\"s3Location\":{\"uri\":\"s3://b/k\"}},\"score\":0.5}],\"nextToken\":\"n1\"}")); + RetrieveParams p; + p.knowledge_base_id = "KB12345678"; + p.query = "What is AWS?"; + RetrieveResult r = rig.agents().retrieve(p); + ASSERT_EQ(r.results.as_array().size(), 1u); + ASSERT_EQ(r.next_token, "n1"); + // DOC: API_agent-runtime_Retrieve "Send a basic query". + ASSERT_TRUE(Json::parse(rig.mock->seen()[0].body).equals(Json::parse("{\"retrievalQuery\":{\"text\":\"What is AWS?\"}}"))); + p.number_of_results = 5; + p.filter = Json::parse("{\"equals\":{\"key\":\"genre\",\"value\":\"entertainment\"}}"); + Json body = p.to_json(); + ASSERT_EQ(body.find("retrievalConfiguration")->find("vectorSearchConfiguration")->find("numberOfResults")->as_int(), 5); + p.number_of_results = 101; + ASSERT_THROW(p.to_json(), molo::llm::ApiError); + p.number_of_results = 0; + p.knowledge_base_id = "short"; + ASSERT_THROW(p.to_json(), molo::llm::ApiError); +} + +TEST(bedrock_wire, retrieve_all_follows_tokens_within_the_page_cap) { + Rig rig; + rig.expect(Method::kPost, "/knowledgebases/KB12345678/retrieve", reply("{\"retrievalResults\":[{\"content\":{\"text\":\"x\"}}],\"nextToken\":\"n\"}"), 100); + RetrieveParams p; + p.knowledge_base_id = "KB12345678"; + p.query = "q"; + RetrieveResult r = rig.agents().retrieve_all(p, 3); + ASSERT_EQ(r.pages, 3u); + ASSERT_TRUE(r.truncated); + ASSERT_EQ(r.results.as_array().size(), 3u); + ASSERT_THROW(rig.agents().retrieve_all(p, 21), molo::llm::ApiError); +} + +TEST(bedrock_wire, count_tokens_and_apply_guardrail_use_their_table_rows) { + Rig rig; + rig.expect(Method::kPost, "/model/anthropic.claude-3-sonnet-20240229-v1%3A0/count-tokens", reply("{\"inputTokens\":42}")); + ASSERT_EQ(rig.runtime().count_tokens(doc_request()), 42L); + Json sent = Json::parse(rig.mock->seen()[0].body); + const Json* conv = sent.find("input")->find("converse"); + ASSERT_TRUE(conv && conv->find("messages") && conv->find("system")); + ASSERT_FALSE(conv->has("inferenceConfig")); // CountTokens' converse input has no inference config + + rig.expect(Method::kPost, "/guardrail/gr123/version/1/apply", + reply("{\"usage\":{},\"action\":\"GUARDRAIL_INTERVENED\",\"outputs\":[{\"text\":\"blocked\"}],\"assessments\":[]}")); + GuardrailResult g = rig.runtime().apply_guardrail("gr123", "1", "INPUT", {Runtime::guardrail_text("my secret", {"query"})}, "INTERVENTIONS"); + ASSERT_TRUE(g.intervened()); + ASSERT_EQ(g.output_text(), "blocked"); + Json gb = Json::parse(rig.mock->seen()[1].body); + ASSERT_EQ(gb.find("source")->as_string(), "INPUT"); + ASSERT_EQ(gb.find("outputScope")->as_string(), "INTERVENTIONS"); + ASSERT_THROW(rig.runtime().apply_guardrail("gr123", "1", "BOTH", {Runtime::guardrail_text("x")}), molo::llm::ApiError); + ASSERT_THROW(rig.runtime().apply_guardrail("gr123", "1", "INPUT", {}), molo::llm::ApiError); + ASSERT_THROW(Runtime::guardrail_text("x", {"weird"}), molo::llm::ApiError); +} + +TEST(bedrock_wire, batch_jobs_validate_create_with_a_body_and_stop_with_an_empty_post) { + Rig rig; + BatchJobParams p; + p.job_name = "nightly-batch.1"; + p.role_arn = "arn:aws:iam::123456789012:role/BatchRole"; + p.model_id = "anthropic.claude-3-haiku-20240307-v1:0"; + p.input_s3_uri = "s3://bucket/in.jsonl"; + p.output_s3_uri = "s3://bucket/out/"; + p.invocation_type = "Converse"; + p.timeout_hours = 48; + p.tags["team"] = "ml"; + rig.expect(Method::kPost, "/model-invocation-job", reply("{\"jobArn\":\"arn:aws:bedrock:us-east-1:123456789012:model-invocation-job/abcdefghijkl\"}")); + std::string arn = rig.jobs().create(p); + ASSERT_TRUE(has(arn, "model-invocation-job/abcdefghijkl")); + Json b = Json::parse(rig.mock->seen()[0].body); + ASSERT_EQ(b.find("inputDataConfig")->find("s3InputDataConfig")->find("s3Uri")->as_string(), "s3://bucket/in.jsonl"); + ASSERT_EQ(b.find("timeoutDurationInHours")->as_int(), 48); + + rig.expect(Method::kPost, "/model-invocation-job/abcdefghijkl/stop", reply("{}")); + rig.jobs().stop("abcdefghijkl"); + const auto s = rig.mock->seen()[1]; + ASSERT_EQ(s.body, ""); + ASSERT_FALSE(s.headers.has("Content-Type")); + + // The ARN form of the id is one encoded path segment. + rig.expect(Method::kGet, "/model-invocation-job/arn%3Aaws%3Abedrock%3Aus-east-1%3A123456789012%3Amodel-invocation-job%2Fabcdefghijkl", reply("{\"jobArn\":\"x\",\"status\":\"Completed\"}")); + ASSERT_EQ(rig.jobs().get("arn:aws:bedrock:us-east-1:123456789012:model-invocation-job/abcdefghijkl").find("status")->as_string(), "Completed"); + + BatchJobParams bad = p; + bad.timeout_hours = 12; + ASSERT_THROW(bad.validate(), molo::llm::ApiError); + bad = p; + bad.input_s3_uri = "http://x"; + ASSERT_THROW(bad.validate(), molo::llm::ApiError); + bad = p; + bad.job_name = "-leading-dash"; + ASSERT_THROW(bad.validate(), molo::llm::ApiError); + bad = p; + bad.role_arn = "role"; + ASSERT_THROW(bad.validate(), molo::llm::ApiError); + ASSERT_THROW(rig.jobs().stop("../x"), molo::llm::ApiError); +} + +TEST(bedrock_wire, models_list_filters_and_get_validate_input) { + Rig rig; + rig.expect(Method::kGet, "/foundation-models", reply("{\"modelSummaries\":[{\"modelId\":\"amazon.nova-micro-v1:0\",\"providerName\":\"Amazon\"}]}")); + ModelFilter f; + f.provider = "Amazon"; + f.output_modality = "TEXT"; + ListResult r = rig.models().list(f); + ASSERT_EQ(r.items.as_array().size(), 1u); + ASSERT_EQ(rig.mock->seen()[0].target, "/foundation-models?byOutputModality=TEXT&byProvider=Amazon"); + f.output_modality = "VIDEO"; + ASSERT_THROW(rig.models().list(f), molo::llm::ApiError); + ASSERT_THROW(rig.models().get(""), molo::llm::ApiError); + ASSERT_THROW(rig.models().list_profiles("", 0), molo::llm::ApiError); + ASSERT_THROW(rig.models().list_profiles("", 1001), molo::llm::ApiError); + ASSERT_THROW(rig.guardrails().get("g1", "0"), molo::llm::ApiError); + ASSERT_THROW(rig.guardrails().get("g1", "latest"), molo::llm::ApiError); +} + +TEST(bedrock_wire, invoke_agent_collects_chunks_traces_and_return_control) { + Rig rig; + auto b64 = [](const std::string& s) { return molo::security::crypto::Base64::encode(s); }; + // MODEL: agent ResponseStream events: chunk{bytes}, trace{trace}, returnControl{invocationId}. + rig.expect(Method::kPost, "/agents/AGENT12345/agentAliases/ALIAS12345/sessions/session-1/text", + stream_reply(Rig::event("trace", "{\"agentId\":\"AGENT12345\",\"trace\":{}}") + + Rig::event("chunk", "{\"bytes\":\"" + b64("Hello ") + "\"}") + + Rig::event("chunk", "{\"bytes\":\"" + b64("world") + "\"}") + + Rig::event("returnControl", "{\"invocationId\":\"inv1\",\"invocationInputs\":[]}"))); + AgentInvokeParams p; + p.agent_id = "AGENT12345"; + p.alias_id = "ALIAS12345"; + p.session_id = "session-1"; + p.input_text = "hi"; + p.enable_trace = true; + std::string live; + AgentResult r = rig.agents().invoke_agent(p, [&](const std::string& t) { live += t; return true; }); + ASSERT_EQ(r.text, "Hello world"); + ASSERT_EQ(live, "Hello world"); + ASSERT_EQ(r.chunks, 2u); + ASSERT_EQ(r.traces.as_array().size(), 1u); + ASSERT_EQ(r.return_control.find("invocationId")->as_string(), "inv1"); + Json body = Json::parse(rig.mock->seen()[0].body); + ASSERT_EQ(body.find("inputText")->as_string(), "hi"); + ASSERT_TRUE(body.find("enableTrace")->as_bool()); + p.session_id = "x"; // under 2 characters + ASSERT_THROW(p.validate(), molo::llm::ApiError); + p.session_id = "ok-session"; + p.agent_id = "WAYTOOLONGAGENTID"; + ASSERT_THROW(p.validate(), molo::llm::ApiError); +}