|
| 1 | +using System; |
| 2 | +using System.Text; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using MCPForUnity.Editor.Security; |
| 6 | +using MCPForUnity.Editor.Services.AssetGen.Http; |
| 7 | +using Newtonsoft.Json; |
| 8 | +using Newtonsoft.Json.Linq; |
| 9 | + |
| 10 | +namespace MCPForUnity.Editor.Services.AssetGen.Providers |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// MiniMax music-cover adapter for the synchronous music-generation endpoint. A cover accepts |
| 14 | + /// one raw reference source (URL or base64 audio) and an optional preprocessed feature id, then |
| 15 | + /// exposes the returned URL or hex payload through the normal audio job pipeline. |
| 16 | + /// </summary> |
| 17 | + public sealed class MiniMaxAudioAdapter : IAudioProviderAdapter |
| 18 | + { |
| 19 | + internal const string GlobalEndpoint = "https://api.minimax.io/v1/music_generation"; |
| 20 | + internal const string ChinaEndpoint = "https://api.minimaxi.com/v1/music_generation"; |
| 21 | + internal const string RegionEnvVar = "MCPFORUNITY_MINIMAX_REGION"; |
| 22 | + internal const string DefaultModel = "music-cover"; |
| 23 | + |
| 24 | + private const string GlobalHost = "api.minimax.io"; |
| 25 | + private const string ChinaHost = "api.minimaxi.com"; |
| 26 | + private const string FreeModel = "music-cover-free"; |
| 27 | + |
| 28 | + private byte[] _inlineData; |
| 29 | + private string _downloadUrl; |
| 30 | + private string _resultExt; |
| 31 | + private string _error; |
| 32 | + |
| 33 | + public string Id => "minimax"; |
| 34 | + |
| 35 | + internal static bool IsCoverModel(string model) |
| 36 | + => string.Equals(model, DefaultModel, StringComparison.Ordinal) |
| 37 | + || string.Equals(model, FreeModel, StringComparison.Ordinal); |
| 38 | + |
| 39 | + public async Task<string> SubmitAsync( |
| 40 | + AudioGenRequest req, |
| 41 | + string apiKey, |
| 42 | + IHttpTransport http, |
| 43 | + CancellationToken ct) |
| 44 | + { |
| 45 | + if (req == null) throw new ArgumentNullException(nameof(req)); |
| 46 | + if (http == null) throw new ArgumentNullException(nameof(http)); |
| 47 | + |
| 48 | + string model = string.IsNullOrWhiteSpace(req.Model) ? DefaultModel : req.Model; |
| 49 | + ValidateRequest(req, model); |
| 50 | + ResolveRegion(out string endpoint, out string host, out bool chinaRegion); |
| 51 | + ProviderHttp.RequireHost(endpoint, host, apiKey, "MiniMax cover submit"); |
| 52 | + |
| 53 | + var spec = new HttpRequestSpec |
| 54 | + { |
| 55 | + Method = "POST", |
| 56 | + Url = endpoint, |
| 57 | + ContentType = "application/json", |
| 58 | + Body = Encoding.UTF8.GetBytes(BuildBody(req, model, chinaRegion).ToString(Formatting.None)) |
| 59 | + }; |
| 60 | + spec.Headers["Authorization"] = "Bearer " + apiKey; |
| 61 | + |
| 62 | + HttpResult response = await http.SendAsync(spec, ct); |
| 63 | + JObject json = ParseOk(response, apiKey); |
| 64 | + |
| 65 | + int statusCode = AsInt(json["base_resp"]?["status_code"], -1); |
| 66 | + if (statusCode != 0) |
| 67 | + { |
| 68 | + string statusMessage = json["base_resp"]?["status_msg"]?.ToString(); |
| 69 | + _error = SecretRedactor.Scrub( |
| 70 | + $"MiniMax music cover failed (status_code={statusCode}): {statusMessage ?? "unknown error"}", |
| 71 | + apiKey); |
| 72 | + return "ready"; |
| 73 | + } |
| 74 | + |
| 75 | + JToken data = json["data"]; |
| 76 | + int generationStatus = AsInt(data?["status"], -1); |
| 77 | + if (generationStatus != 2) |
| 78 | + { |
| 79 | + _error = generationStatus == 1 |
| 80 | + ? "MiniMax music cover is still in progress, but the response included no query endpoint." |
| 81 | + : $"MiniMax music cover returned an unexpected status ({generationStatus})."; |
| 82 | + return "ready"; |
| 83 | + } |
| 84 | + |
| 85 | + string audio = data?["audio"]?.ToString(); |
| 86 | + if (string.IsNullOrWhiteSpace(audio)) |
| 87 | + { |
| 88 | + _error = "MiniMax music cover completed without audio data."; |
| 89 | + return "ready"; |
| 90 | + } |
| 91 | + |
| 92 | + if (Uri.TryCreate(audio, UriKind.Absolute, out Uri audioUri) |
| 93 | + && (audioUri.Scheme == Uri.UriSchemeHttp || audioUri.Scheme == Uri.UriSchemeHttps)) |
| 94 | + { |
| 95 | + _downloadUrl = audio; |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + _inlineData = TryDecodeHex(audio); |
| 100 | + if (_inlineData == null || _inlineData.Length == 0) |
| 101 | + { |
| 102 | + _error = "MiniMax returned an unrecognized audio payload."; |
| 103 | + return "ready"; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + _resultExt = NormalizeAudioFormat(req.AudioFormat); |
| 108 | + return "ready"; |
| 109 | + } |
| 110 | + |
| 111 | + public Task<ProviderPollResult> PollAsync( |
| 112 | + string providerJobId, |
| 113 | + string apiKey, |
| 114 | + IHttpTransport http, |
| 115 | + CancellationToken ct) |
| 116 | + { |
| 117 | + var result = new ProviderPollResult { Progress = 1f }; |
| 118 | + if (!string.IsNullOrEmpty(_error) || (_inlineData == null && string.IsNullOrEmpty(_downloadUrl))) |
| 119 | + { |
| 120 | + result.State = ProviderPollState.Failed; |
| 121 | + result.Error = _error ?? "MiniMax produced no cover audio."; |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + result.State = ProviderPollState.Succeeded; |
| 126 | + result.InlineData = _inlineData; |
| 127 | + result.DownloadUrl = _downloadUrl; |
| 128 | + result.ResultExt = _resultExt; |
| 129 | + } |
| 130 | + return Task.FromResult(result); |
| 131 | + } |
| 132 | + |
| 133 | + private static void ValidateRequest(AudioGenRequest req, string model) |
| 134 | + { |
| 135 | + if (!IsCoverModel(model)) |
| 136 | + throw new NotSupportedException("MiniMax audio supports music-cover and music-cover-free."); |
| 137 | + |
| 138 | + int sourceCount = 0; |
| 139 | + if (!string.IsNullOrWhiteSpace(req.AudioUrl)) sourceCount++; |
| 140 | + if (!string.IsNullOrWhiteSpace(req.AudioBase64)) sourceCount++; |
| 141 | + if (sourceCount != 1) |
| 142 | + throw new ArgumentException("Provide exactly one cover source: audio_url or audio_base64."); |
| 143 | + |
| 144 | + NormalizeOutputFormat(req.OutputFormat); |
| 145 | + NormalizeAudioFormat(req.AudioFormat); |
| 146 | + } |
| 147 | + |
| 148 | + private static JObject BuildBody(AudioGenRequest req, string model, bool chinaRegion) |
| 149 | + { |
| 150 | + var body = new JObject |
| 151 | + { |
| 152 | + ["model"] = model, |
| 153 | + ["stream"] = false, |
| 154 | + ["output_format"] = NormalizeOutputFormat(req.OutputFormat), |
| 155 | + ["audio_setting"] = new JObject |
| 156 | + { |
| 157 | + ["format"] = NormalizeAudioFormat(req.AudioFormat) |
| 158 | + } |
| 159 | + }; |
| 160 | + |
| 161 | + if (!string.IsNullOrWhiteSpace(req.Prompt)) body["prompt"] = req.Prompt; |
| 162 | + if (!string.IsNullOrWhiteSpace(req.Lyrics)) body["lyrics"] = req.Lyrics; |
| 163 | + if (req.LyricsOptimizer.HasValue) body["lyrics_optimizer"] = req.LyricsOptimizer.Value; |
| 164 | + if (req.IsInstrumental.HasValue) body["is_instrumental"] = req.IsInstrumental.Value; |
| 165 | + if (!string.IsNullOrWhiteSpace(req.AudioUrl)) body["audio_url"] = req.AudioUrl; |
| 166 | + if (!string.IsNullOrWhiteSpace(req.AudioBase64)) body["audio_base64"] = req.AudioBase64; |
| 167 | + if (!string.IsNullOrWhiteSpace(req.CoverFeatureId)) body["cover_feature_id"] = req.CoverFeatureId; |
| 168 | + if (chinaRegion && req.AigcWatermark.HasValue) body["aigc_watermark"] = req.AigcWatermark.Value; |
| 169 | + return body; |
| 170 | + } |
| 171 | + |
| 172 | + private static string NormalizeOutputFormat(string value) |
| 173 | + { |
| 174 | + string normalized = string.IsNullOrWhiteSpace(value) ? "url" : value.Trim().ToLowerInvariant(); |
| 175 | + if (normalized != "url" && normalized != "hex") |
| 176 | + throw new ArgumentException("output_format must be 'url' or 'hex'."); |
| 177 | + return normalized; |
| 178 | + } |
| 179 | + |
| 180 | + private static string NormalizeAudioFormat(string value) |
| 181 | + { |
| 182 | + string normalized = string.IsNullOrWhiteSpace(value) ? "mp3" : value.Trim().ToLowerInvariant(); |
| 183 | + if (normalized != "mp3" && normalized != "wav" && normalized != "pcm") |
| 184 | + throw new ArgumentException("audio_format must be 'mp3', 'wav', or 'pcm'."); |
| 185 | + return normalized; |
| 186 | + } |
| 187 | + |
| 188 | + private static void ResolveRegion(out string endpoint, out string host, out bool chinaRegion) |
| 189 | + { |
| 190 | + string region = (Environment.GetEnvironmentVariable(RegionEnvVar) ?? string.Empty) |
| 191 | + .Trim().ToLowerInvariant(); |
| 192 | + chinaRegion = region == "cn" || region == "cn_zh" || region == "china"; |
| 193 | + endpoint = chinaRegion ? ChinaEndpoint : GlobalEndpoint; |
| 194 | + host = chinaRegion ? ChinaHost : GlobalHost; |
| 195 | + } |
| 196 | + |
| 197 | + private static int AsInt(JToken token, int fallback) |
| 198 | + { |
| 199 | + if (token == null || token.Type == JTokenType.Null) return fallback; |
| 200 | + if (token.Type == JTokenType.Integer) return (int)token; |
| 201 | + return int.TryParse(token.ToString(), out int parsed) ? parsed : fallback; |
| 202 | + } |
| 203 | + |
| 204 | + private static byte[] TryDecodeHex(string hex) |
| 205 | + { |
| 206 | + if (string.IsNullOrEmpty(hex) || (hex.Length % 2) != 0) return null; |
| 207 | + var bytes = new byte[hex.Length / 2]; |
| 208 | + for (int i = 0; i < bytes.Length; i++) |
| 209 | + { |
| 210 | + int high = HexValue(hex[i * 2]); |
| 211 | + int low = HexValue(hex[i * 2 + 1]); |
| 212 | + if (high < 0 || low < 0) return null; |
| 213 | + bytes[i] = (byte)((high << 4) | low); |
| 214 | + } |
| 215 | + return bytes; |
| 216 | + } |
| 217 | + |
| 218 | + private static int HexValue(char value) |
| 219 | + { |
| 220 | + if (value >= '0' && value <= '9') return value - '0'; |
| 221 | + if (value >= 'a' && value <= 'f') return value - 'a' + 10; |
| 222 | + if (value >= 'A' && value <= 'F') return value - 'A' + 10; |
| 223 | + return -1; |
| 224 | + } |
| 225 | + |
| 226 | + private static JObject ParseOk(HttpResult response, string apiKey) |
| 227 | + { |
| 228 | + string text = ProviderHttp.BodyText(response); |
| 229 | + JObject json = null; |
| 230 | + if (!string.IsNullOrEmpty(text)) |
| 231 | + { |
| 232 | + try { json = JObject.Parse(text); } |
| 233 | + catch { /* handled below */ } |
| 234 | + } |
| 235 | + |
| 236 | + if (response?.Ok != true) |
| 237 | + { |
| 238 | + string detail = json?["base_resp"]?["status_msg"]?.ToString() |
| 239 | + ?? json?["error"]?.ToString() |
| 240 | + ?? ProviderHttp.Truncate(text); |
| 241 | + throw new Exception(SecretRedactor.Scrub( |
| 242 | + $"MiniMax cover request failed (status={response?.Status}): {detail}", apiKey)); |
| 243 | + } |
| 244 | + return json ?? new JObject(); |
| 245 | + } |
| 246 | + } |
| 247 | +} |
0 commit comments