Stability, BFL, Runway, Luma, Replicate, fal, Ideogram, Recraft, Leonardo, MiniMax, Kling, Higgsfield: call tables checked against vendor specs/SDKs, ImageGenerator/VideoGenerator seam with capped polling, /ai/media demo. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
139 lines
4.6 KiB
C++
139 lines
4.6 KiB
C++
#include "vendors/media/minimax/minimax.h"
|
|
|
|
#include "vendors/media/common/adapter_support.h"
|
|
#include "vendors/media/common/output_scanner.h"
|
|
|
|
namespace molo::media {
|
|
|
|
namespace {
|
|
|
|
CallArgs with_body(const Json& body) {
|
|
CallArgs a;
|
|
a.body = body;
|
|
return a;
|
|
}
|
|
|
|
CallArgs task(const std::string& id) {
|
|
CallArgs a;
|
|
a.path["task_id"] = id;
|
|
return a;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void MinimaxClient::check_base_resp(const Json& doc) {
|
|
const Json* br = doc.is_object() ? doc.find("base_resp") : nullptr;
|
|
if (!br || !br->is_object()) return;
|
|
const Json* code = br->find("status_code");
|
|
if (!code || !code->is_number() || code->as_int() == 0) return;
|
|
throw llm::ApiError("minimax", 400, std::to_string(code->as_int()), AdapterSupport::str(*br, "status_msg"));
|
|
}
|
|
|
|
PreparedCall MinimaxClient::plan_video(const Json& body) const { return prepare("video.create", with_body(body)); }
|
|
|
|
Json MinimaxClient::create_video(const Json& body, bool confirm) {
|
|
Json out = call_json("video.create", with_body(body), confirm);
|
|
check_base_resp(out);
|
|
return out;
|
|
}
|
|
|
|
Json MinimaxClient::query_video(const std::string& task_id) {
|
|
CallArgs a;
|
|
a.query["task_id"] = task_id;
|
|
Json out = call_json("video.query", a);
|
|
check_base_resp(out);
|
|
return out;
|
|
}
|
|
|
|
Json MinimaxClient::retrieve_file(const std::string& file_id) {
|
|
CallArgs a;
|
|
a.query["file_id"] = file_id;
|
|
Json out = call_json("files.retrieve", a);
|
|
check_base_resp(out);
|
|
return out;
|
|
}
|
|
|
|
Json MinimaxClient::create_video_v2(const Json& body, bool confirm) {
|
|
return call_json("v2.video.create", with_body(body), confirm);
|
|
}
|
|
Json MinimaxClient::query_video_v2(const std::string& id) { return call_json("v2.video.query", task(id)); }
|
|
|
|
Json MinimaxClient::list_video_v2(int page_num, int page_size) {
|
|
CallArgs a;
|
|
if (page_num > 0) a.query["page_num"] = std::to_string(page_num);
|
|
if (page_size > 0) a.query["page_size"] = std::to_string(page_size);
|
|
return call_json("v2.video.list", a);
|
|
}
|
|
|
|
Json MinimaxClient::delete_video_v2(const std::string& id, bool confirm) {
|
|
return call_json("v2.video.delete", task(id), confirm);
|
|
}
|
|
|
|
JobState MinimaxVideoGenerator::state_of(const std::string& status) {
|
|
if (status == "Success") return JobState::kSucceeded;
|
|
if (status == "Fail") return JobState::kFailed;
|
|
if (status == "Processing") return JobState::kRunning;
|
|
return JobState::kQueued; // Preparing, Queueing
|
|
}
|
|
|
|
Json MinimaxVideoGenerator::body(const VideoRequest& r) const {
|
|
r.validate();
|
|
Json b = Json::object();
|
|
b.set("model", Json(r.model.empty() ? "MiniMax-Hailuo-2.3" : r.model));
|
|
AdapterSupport::put(b, "prompt", r.prompt);
|
|
AdapterSupport::put_int(b, "duration", r.duration_seconds);
|
|
if (!r.resolution.empty()) {
|
|
std::string res = r.resolution;
|
|
for (char& c : res) c = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
|
|
b.set("resolution", Json(res)); // docs: 768P / 1080P
|
|
}
|
|
AdapterSupport::put(b, "first_frame_image", r.image_url);
|
|
AdapterSupport::put(b, "last_frame_image", r.end_image_url);
|
|
if (!r.aspect_ratio.empty()) throw llm::ApiError::invalid("minimax", "v1 video_generation has no aspect_ratio");
|
|
if (r.seed >= 0) throw llm::ApiError::invalid("minimax", "v1 video_generation has no seed");
|
|
AdapterSupport::merge_extra(b, r.extra);
|
|
return b;
|
|
}
|
|
|
|
Json MinimaxVideoGenerator::plan(const VideoRequest& request) const {
|
|
return client_->plan_video(body(request)).summary();
|
|
}
|
|
|
|
JobTicket MinimaxVideoGenerator::submit(const VideoRequest& request, bool confirm) {
|
|
Json b = body(request);
|
|
AdapterSupport::need_confirm(confirm, "minimax", "generate");
|
|
Json out = client_->create_video(b, true);
|
|
JobTicket t;
|
|
t.provider = "minimax";
|
|
t.model = b.find("model")->as_string();
|
|
t.kind = MediaKind::kVideo;
|
|
t.id = AdapterSupport::str(out, "task_id");
|
|
if (t.id.empty()) throw llm::ApiError("minimax", 502, "BAD_RESPONSE", "no task_id in the reply");
|
|
return t;
|
|
}
|
|
|
|
JobStatus MinimaxVideoGenerator::poll(const JobTicket& ticket) {
|
|
Json q = client_->query_video(ticket.id);
|
|
JobStatus s;
|
|
s.raw_state = AdapterSupport::str(q, "status");
|
|
s.state = state_of(s.raw_state);
|
|
if (s.state == JobState::kFailed) s.error = "Fail";
|
|
if (s.state == JobState::kSucceeded) {
|
|
std::string file_id = AdapterSupport::str(q, "file_id");
|
|
if (file_id.empty()) {
|
|
s.state = JobState::kFailed;
|
|
s.error = "Success without a file_id";
|
|
return s;
|
|
}
|
|
Json f = client_->retrieve_file(file_id);
|
|
if (const Json* file = f.find("file")) s.assets = OutputScanner::scan(*file, MediaKind::kVideo);
|
|
if (s.assets.empty()) {
|
|
s.state = JobState::kFailed;
|
|
s.error = "file retrieved without a download_url";
|
|
}
|
|
}
|
|
return s;
|
|
}
|
|
|
|
} // namespace molo::media
|