#include "../AiDemos.hpp" #include #include "../../fw/Router.hpp" #include "../../fw/Templates.hpp" #include "llm/config.h" #include "xai/core/client.h" namespace molo::example::app { // xAI Grok core platform over the real src/xai/core part. GET routes need no // credential. Every route that calls xAI is a POST: 503 not_configured without // XAI_API_KEY, 422 on bad input (validated locally, before the credential // check). Media generation, batch submission, deletes and cancels are dry runs // that return the validated wire body unless the body says "confirm":true. namespace { namespace xc = xai::core; using Json = clients::body::Json; constexpr std::size_t kMaxBody = 256 * 1024; const char kPage[] = R"XAI({% extends "base.html" %} {% block content %}

xAI Grok

Responses, chat, tools, images, video, embeddings, models and batches over api.x.ai. Paid calls are dry runs until you tick confirm.

Checking XAI_API_KEY...

More calls


{% endblock %} )XAI"; 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; } [[noreturn]] void bad(const std::string& why) { throw fw::HttpError(422, "validation_error", why); } [[noreturn]] void rethrow(const llm::ApiError& e) { if (e.type() == "NOT_CONFIGURED") throw fw::HttpError(503, "not_configured", e.message()); if (e.type() == "INVALID_ARGUMENT" && e.http_status() == 400) bad(e.message()); throw fw::HttpError(502, "xai_error", e.type() + ": " + e.message()); } template auto guarded(F&& f) -> decltype(f()) { try { return f(); } catch (const llm::ApiError& e) { rethrow(e); } } 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()); } 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 a boolean"); return v->as_bool(); } std::string pick(const Json& b, const char* key, std::initializer_list allowed) { std::string v = text(b, key, true); for (const char* a : allowed) if (v == a) return v; bad(std::string(key) + " is not one of the allowed values"); } std::vector string_list(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"); for (const Json& e : v->as_array()) { if (!e.is_string()) bad(std::string(key) + " must hold strings"); out.push_back(e.as_string()); } return out; } Json dry_run(const Json& wire, const std::string& what) { Json o = Json::object(); o.set("dry_run", Json(true)); o.set("would_send", Json(what)); o.set("body", wire); o.set("hint", Json("send confirm:true to run it")); return o; } Json usage_json(const xc::TokenUsage& 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("cached_tokens", Json(u.cached_tokens)); o.set("reasoning_tokens", Json(u.reasoning_tokens)); o.set("cost_usd_ticks", Json(u.cost_usd_ticks)); o.set("cost_usd", Json(u.cost_usd())); return o; } Json response_json(const xc::ModelResponse& r) { Json o = Json::object(); o.set("id", Json(r.id())); o.set("model", Json(r.model())); o.set("status", Json(r.status())); o.set("text", Json(r.text())); if (!r.reasoning_text().empty()) o.set("reasoning", Json(r.reasoning_text())); Json cites = Json::array(); for (const auto& c : r.citations()) cites.push(Json(c.url)); o.set("citations", cites); Json calls = Json::array(); for (const auto& c : r.function_calls()) { Json cj = Json::object(); cj.set("call_id", Json(c.call_id())); cj.set("name", Json(c.name())); cj.set("arguments", c.arguments()); calls.push(cj); } o.set("function_calls", calls); o.set("server_tool_calls", Json(static_cast(r.server_tool_calls().size()))); o.set("usage", usage_json(r.usage())); return o; } Json model_json(const xc::ModelInfo& m) { Json o = Json::object(); o.set("id", Json(m.id)); o.set("aliases", xc::jx::string_array(m.aliases)); o.set("input_modalities", xc::jx::string_array(m.input_modalities)); o.set("output_modalities", xc::jx::string_array(m.output_modalities)); o.set("reasoning_effort", xc::jx::string_array(m.reasoning_efforts)); Json prices = Json::object(); for (const auto& [k, v] : m.prices) prices.set(k, Json(static_cast(v))); o.set("prices", prices); return o; } Json batch_json(const xc::Batch& b) { Json o = Json::object(); o.set("batch_id", Json(b.batch_id)); o.set("name", Json(b.name)); o.set("finished", Json(b.finished())); Json s = Json::object(); s.set("num_requests", Json(b.state.num_requests)); s.set("num_pending", Json(b.state.num_pending)); s.set("num_success", Json(b.state.num_success)); s.set("num_error", Json(b.state.num_error)); s.set("num_cancelled", Json(b.state.num_cancelled)); o.set("state", s); return o; } xc::ResponseRequest build_response(const Json& b) { std::string model = text(b, "model", true); xc::ResponseRequest r(model); r.input_text(text(b, "prompt", true)); if (std::string i = text(b, "instructions"); !i.empty()) r.instructions(i); r.store(false); if (std::string e = text(b, "effort"); !e.empty()) r.reasoning_effort(e); if (auto n = number(b, "maxOutputTokens")) r.max_output_tokens(*n); for (const std::string& t : string_list(b, "tools")) { if (t == "web_search") r.add_tool(xc::WebSearchTool()); else if (t == "x_search") r.add_tool(xc::XSearchTool()); else if (t == "code_interpreter") r.add_tool(xc::CodeExecutionTool()); else bad("tools may hold web_search, x_search, code_interpreter"); } return r; } xc::ImageOptions image_options(const Json& b) { xc::ImageOptions o; o.model = text(b, "model"); if (auto n = number(b, "n")) o.n = *n; o.aspect_ratio = text(b, "aspectRatio"); o.resolution = text(b, "resolution"); o.response_format = text(b, "responseFormat"); return o; } class Routes { public: Routes() = default; static std::shared_ptr client() { return xc::XaiClient::from_env(); } static fw::Response chat(const fw::Request& req) { Json b = parse_body(req); std::string wire = text(b, "wire"); if (!wire.empty() && wire != "responses" && wire != "chat") bad("wire must be responses or chat"); xc::ResponseRequest r = build_response(b); xc::ChatRequest cr(text(b, "model", true)); if (wire == "chat") { cr.add(xc::ChatMessage::user(text(b, "prompt", true))); if (std::string e = text(b, "effort"); !e.empty()) cr.reasoning_effort(e); if (auto n = number(b, "maxOutputTokens")) cr.max_completion_tokens(*n); cr.to_json(); // validate } else { r.to_json(); } return guarded([&] { auto c = client(); if (wire == "chat") { xc::ChatCompletion done = c->chat.create(cr); Json o = Json::object(); o.set("id", Json(done.id)); o.set("text", Json(done.text())); o.set("finish_reason", Json(done.finish_reason())); o.set("usage", usage_json(done.usage)); return json_out(o); } return json_out(response_json(c->responses.create(r))); }); } static fw::Response messages(const fw::Request& req) { Json b = parse_body(req); xc::MessagesRequest r(text(b, "model", true), number(b, "maxTokens").value_or(256)); r.user(text(b, "prompt", true)); r.to_json(); return guarded([&] { xc::MessagesResponse m = client()->messages.create(r); Json o = Json::object(); o.set("id", Json(m.id)); o.set("text", Json(m.text())); o.set("stop_reason", Json(m.stop_reason)); o.set("input_tokens", Json(m.input_tokens)); o.set("output_tokens", Json(m.output_tokens)); return json_out(o); }); } static fw::Response response_get(const fw::Request& req) { Json b = parse_body(req); std::string id = text(b, "id", true); xc::render(xc::Call::kResponsesGet, {id}); std::string action = text(b, "action"); if (!action.empty() && action != "get" && action != "items") bad("action must be get or items"); return guarded([&] { auto c = client(); if (action == "items") { xc::InputItemList l = c->responses.all_input_items(id, 200); Json o = Json::object(); Json arr = Json::array(); for (const Json& i : l.data) arr.push(i); o.set("items", arr); o.set("truncated", Json(l.truncated)); return json_out(o); } return json_out(response_json(c->responses.retrieve(id))); }); } static fw::Response response_delete(const fw::Request& req) { Json b = parse_body(req); std::string id = text(b, "id", true); xc::render(xc::Call::kResponsesDelete, {id}); if (!flag(b, "confirm")) { Json o = Json::object(); o.set("dry_run", Json(true)); o.set("would_delete", Json(id)); return json_out(o); } return guarded([&] { xc::DeletedResponse d = client()->responses.remove(id); Json o = Json::object(); o.set("id", Json(d.id)); o.set("deleted", Json(d.deleted)); return json_out(o); }); } static fw::Response images(const fw::Request& req) { Json b = parse_body(req); std::string action = pick(b, "action", {"generate", "edit", "poll"}); if (action == "poll") { std::string id = text(b, "requestId", true); xc::render(xc::Call::kImagesGet, {id}); return guarded([&] { xc::DeferredImage d = client()->images.poll(id); Json o = Json::object(); o.set("status", Json(d.status)); Json urls = Json::array(); if (d.result) for (const auto& g : d.result->data) urls.push(Json(g.url)); o.set("urls", urls); return json_out(o); }); } Json wire; xc::ImageGenerationRequest gen; xc::ImageEditRequest edit; if (action == "generate") { gen.prompt = text(b, "prompt", true); gen.options = image_options(b); wire = gen.to_json(); } else { edit.prompt = text(b, "prompt", true); edit.image = xc::MediaSource::image_url(text(b, "imageUrl", true)); edit.options = image_options(b); wire = edit.to_json(); } if (!flag(b, "confirm")) return json_out(dry_run(wire, action == "generate" ? "/v1/images/generations" : "/v1/images/edits")); return guarded([&] { xc::ImageResult r = action == "generate" ? client()->images.generate(gen) : client()->images.edit(edit); Json o = Json::object(); Json urls = Json::array(); for (const auto& g : r.data) urls.push(Json(g.url.empty() ? "(base64 payload omitted)" : g.url)); o.set("urls", urls); o.set("cost_usd", Json(r.usage.cost_usd())); return json_out(o); }); } static fw::Response videos(const fw::Request& req) { Json b = parse_body(req); std::string action = pick(b, "action", {"generate", "edit", "extend", "poll"}); if (action == "poll") { std::string id = text(b, "requestId", true); xc::render(xc::Call::kVideosGet, {id}); return guarded([&] { xc::VideoJob j = client()->videos.poll(id); Json o = Json::object(); o.set("status", Json(j.status)); if (j.progress) o.set("progress", Json(static_cast(*j.progress))); if (j.video) o.set("url", Json(j.video->url)); if (j.error) o.set("error", Json(j.error->code + ": " + j.error->message)); return json_out(o); }); } xc::VideoGenerationRequest gen; xc::VideoEditRequest edit; xc::VideoExtendRequest ext; Json wire; if (action == "generate") { gen.model = text(b, "model"); gen.prompt = text(b, "prompt"); gen.duration = number(b, "duration"); gen.aspect_ratio = text(b, "aspectRatio"); gen.resolution = text(b, "resolution"); if (std::string u = text(b, "imageUrl"); !u.empty()) gen.image = xc::MediaSource::image_url(u); wire = gen.to_json(); } else if (action == "edit") { edit.model = text(b, "model"); edit.prompt = text(b, "prompt", true); edit.video = xc::MediaSource::video_url(text(b, "videoUrl", true)); wire = edit.to_json(); } else { ext.model = text(b, "model"); ext.prompt = text(b, "prompt", true); ext.video = xc::MediaSource::video_url(text(b, "videoUrl", true)); ext.duration = number(b, "duration"); wire = ext.to_json(); } if (!flag(b, "confirm")) return json_out(dry_run(wire, "/v1/videos/" + std::string(action == "generate" ? "generations" : action == "edit" ? "edits" : "extensions"))); return guarded([&] { auto c = client(); std::string id = action == "generate" ? c->videos.start_generate(gen) : action == "edit" ? c->videos.start_edit(edit) : c->videos.start_extend(ext); Json o = Json::object(); o.set("request_id", Json(id)); o.set("hint", Json("poll with action poll")); return json_out(o, 202); }); } static fw::Response embeddings(const fw::Request& req) { Json b = parse_body(req); xc::EmbeddingRequest r; r.model = text(b, "model", true); r.texts = string_list(b, "texts"); r.dimensions = number(b, "dimensions"); r.to_json(); return guarded([&] { xc::EmbeddingResult e = client()->embeddings.embed(r); Json o = Json::object(); Json arr = Json::array(); for (const auto& v : e.data) { Json row = Json::object(); row.set("index", Json(v.index)); row.set("dimensions", Json(static_cast(v.values.size()))); arr.push(row); } o.set("embeddings", arr); o.set("prompt_tokens", Json(e.prompt_tokens)); return json_out(o); }); } static fw::Response tokenize(const fw::Request& req) { Json b = parse_body(req); std::string model = text(b, "model", true); std::string t = text(b, "text", true); xc::Check::model(model); xc::Check::text("text", t, xc::Caps::kMaxTokenizeBytes); return guarded([&] { auto toks = client()->tokenizer.tokenize(model, t); Json arr = Json::array(); for (const auto& k : toks) { Json row = Json::object(); row.set("token_id", Json(k.token_id)); row.set("token", Json(k.string_token)); arr.push(row); } Json o = Json::object(); o.set("count", Json(static_cast(toks.size()))); o.set("tokens", arr); return json_out(o); }); } static fw::Response models(const fw::Request& req) { Json b = parse_body(req); std::string kind = text(b, "kind"); if (kind.empty()) kind = "language"; pick_kind(kind); std::string id = text(b, "id"); if (!id.empty()) xc::Check::id("model", id); return guarded([&] { auto c = client(); xc::ModelKind k = kind_of(kind); if (!id.empty()) return json_out(model_json(c->models.get(k, id))); xc::ModelList l = c->models.list(k); Json arr = Json::array(); for (const auto& m : l.models) arr.push(model_json(m)); Json o = Json::object(); o.set("models", arr); o.set("truncated", Json(l.truncated)); return json_out(o); }); } static fw::Response batches(const fw::Request& req) { Json b = parse_body(req); std::string action = pick(b, "action", {"create", "list", "get", "add", "requests", "results", "cancel"}); std::string id = text(b, "batchId"); if (action != "create" && action != "list") { id = text(b, "batchId", true); xc::render(xc::Call::kBatchGet, {id}); } std::string name = action == "create" ? text(b, "name", true) : ""; std::vector entries; if (action == "add") { std::string model = text(b, "model", true); for (const std::string& p : string_list(b, "prompts")) { xc::ChatRequest cr(model); cr.add(xc::ChatMessage::user(p)); entries.push_back(xc::BatchEntry::chat("req_" + std::to_string(entries.size() + 1), cr)); } if (entries.empty()) bad("prompts is required"); if (entries.size() > xc::Caps::kMaxBatchRequestsPerAdd) bad("too many prompts"); } if (name.size() > xc::Caps::kMaxNameLen) bad("name too long"); bool confirm = flag(b, "confirm"); if ((action == "add" || action == "cancel") && !confirm) { Json o = Json::object(); o.set("dry_run", Json(true)); o.set("would", Json(action)); o.set("batch_id", Json(id)); o.set("requests", Json(static_cast(entries.size()))); return json_out(o); } return guarded([&] { auto c = client(); if (action == "create") return json_out(batch_json(c->batches.create(name)), 201); if (action == "get") return json_out(batch_json(c->batches.get(id))); if (action == "add") return json_out(batch_json(c->batches.add_requests(id, entries))); if (action == "cancel") return json_out(batch_json(c->batches.cancel(id))); Json o = Json::object(); if (action == "list") { Json arr = Json::array(); for (const auto& x : c->batches.list(20).batches) arr.push(batch_json(x)); o.set("batches", arr); } else if (action == "requests") { Json arr = Json::array(); for (const auto& r : c->batches.list_requests(id, 100).requests) { Json row = Json::object(); row.set("id", Json(r.batch_request_id)); row.set("state", Json(r.state)); arr.push(row); } o.set("requests", arr); } else { xc::BatchResultSet set = c->batches.all_results(id, 200); Json arr = Json::array(); for (const auto& r : set.results) { Json row = Json::object(); row.set("id", Json(r.batch_request_id)); row.set("ok", Json(r.succeeded)); if (!r.error.empty()) row.set("error", Json(r.error)); arr.push(row); } o.set("results", arr); o.set("truncated", Json(set.truncated)); } return json_out(o); }); } static fw::Response account(const fw::Request& req) { Json b = parse_body(req); std::string action = pick(b, "action", {"api_key", "me"}); return guarded([&] { auto c = client(); Json o = Json::object(); if (action == "api_key") { xc::ApiKeyInfo k = c->account.api_key(); o.set("name", Json(k.name)); o.set("redacted_api_key", Json(k.redacted_api_key)); o.set("usable", Json(k.usable())); o.set("acls", xc::jx::string_array(k.acls)); } else { xc::MeInfo m = c->account.me(); o.set("team_id", Json(m.team_id)); o.set("user_id", Json(m.user_id)); o.set("zdr_status", Json(m.zdr_status)); o.set("team_blocked", Json(m.team_blocked)); } return json_out(o); }); } private: static void pick_kind(const std::string& k) { if (k != "generic" && k != "language" && k != "image" && k != "video" && k != "embedding") bad("kind must be generic, language, image, video or embedding"); } static xc::ModelKind kind_of(const std::string& k) { if (k == "language") return xc::ModelKind::kLanguage; if (k == "image") return xc::ModelKind::kImage; if (k == "video") return xc::ModelKind::kVideo; if (k == "embedding") return xc::ModelKind::kEmbedding; return xc::ModelKind::kGeneric; } }; } // namespace void register_ai_xai_demo(fw::Router& router, Services& services) { fw::Environment* env = services.templates.get(); services.templates->add_template("ai_xai.html", kPage); services.add_feature("ai-xai", "/ai/xai"); router.get("/ai/xai", "ai-xai-page", [env](fw::Request req) { (void)req; fw::TemplateCtx ctx; ctx["title"] = std::string("xAI Grok"); return fw::Response(200, env->render("ai_xai.html", ctx), "text/html; charset=utf-8"); }); router.get("/ai/xai/status", "ai-xai-status", [](fw::Request req) { (void)req; Json o = Json::object(); o.set("configured", Json(!llm::Config::get(xc::XaiService::kKeyEnv).empty())); o.set("key_env", Json(xc::XaiService::kKeyEnv)); o.set("base_url", Json(xc::XaiService::kBaseUrl)); o.set("calls", Json(static_cast(xc::call_count()))); return json_out(o); }); // The spec-checked call table, for humans. router.get("/ai/xai/calls", "ai-xai-calls", [](fw::Request req) { (void)req; Json arr = Json::array(); for (std::size_t i = 0; i < xc::call_count(); ++i) { const xc::CallSpec& s = xc::spec_at(i); Json row = Json::object(); row.set("http", Json(s.http_method)); row.set("path", Json(s.path)); row.set("op", Json(s.op_id)); row.set("source", Json(s.source)); arr.push(row); } Json o = Json::object(); o.set("calls", arr); return json_out(o); }); router.post("/ai/xai/chat", "ai-xai-chat", [](fw::Request r) { return guarded([&] { return Routes::chat(r); }); }); router.post("/ai/xai/messages", "ai-xai-messages", [](fw::Request r) { return guarded([&] { return Routes::messages(r); }); }); router.post("/ai/xai/responses/get", "ai-xai-response-get", [](fw::Request r) { return guarded([&] { return Routes::response_get(r); }); }); router.post("/ai/xai/responses/delete", "ai-xai-response-delete", [](fw::Request r) { return guarded([&] { return Routes::response_delete(r); }); }); router.post("/ai/xai/images", "ai-xai-images", [](fw::Request r) { return guarded([&] { return Routes::images(r); }); }); router.post("/ai/xai/videos", "ai-xai-videos", [](fw::Request r) { return guarded([&] { return Routes::videos(r); }); }); router.post("/ai/xai/embeddings", "ai-xai-embeddings", [](fw::Request r) { return guarded([&] { return Routes::embeddings(r); }); }); router.post("/ai/xai/tokenize", "ai-xai-tokenize", [](fw::Request r) { return guarded([&] { return Routes::tokenize(r); }); }); router.post("/ai/xai/models", "ai-xai-models", [](fw::Request r) { return guarded([&] { return Routes::models(r); }); }); router.post("/ai/xai/batches", "ai-xai-batches", [](fw::Request r) { return guarded([&] { return Routes::batches(r); }); }); router.post("/ai/xai/account", "ai-xai-account", [](fw::Request r) { return guarded([&] { return Routes::account(r); }); }); } } // namespace molo::example::app