mmx-cli 1.0.16 → 1.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/sdk.mjs CHANGED
@@ -2,7 +2,7 @@ import { createRequire } from "node:module";
2
2
  var __require = /* @__PURE__ */ createRequire(import.meta.url);
3
3
 
4
4
  // src/config/loader.ts
5
- import { readFileSync, writeFileSync, renameSync, existsSync } from "fs";
5
+ import { copyFileSync, existsSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "fs";
6
6
 
7
7
  // src/config/schema.ts
8
8
  var REGIONS = {
@@ -215,6 +215,23 @@ class SDKError extends CLIError {
215
215
  }
216
216
 
217
217
  // src/config/loader.ts
218
+ function isCrossDeviceError(err) {
219
+ return err instanceof Error && "code" in err && err.code === "EXDEV";
220
+ }
221
+ function renameWithCrossDeviceFallback(from, to, ops = {
222
+ rename: renameSync,
223
+ copy: copyFileSync,
224
+ unlink: unlinkSync
225
+ }) {
226
+ try {
227
+ ops.rename(from, to);
228
+ } catch (err) {
229
+ if (!isCrossDeviceError(err))
230
+ throw err;
231
+ ops.copy(from, to);
232
+ ops.unlink(from);
233
+ }
234
+ }
218
235
  function readConfigFile() {
219
236
  const path = getConfigPath();
220
237
  if (!existsSync(path))
@@ -230,13 +247,13 @@ function readConfigFile() {
230
247
  return {};
231
248
  }
232
249
  }
233
- async function writeConfigFile(data) {
250
+ async function writeConfigFile(data, renameOps) {
234
251
  await ensureConfigDir();
235
252
  const path = getConfigPath();
236
253
  const tmp = path + ".tmp";
237
254
  writeFileSync(tmp, JSON.stringify(data, null, 2) + `
238
255
  `, { mode: 384 });
239
- renameSync(tmp, path);
256
+ renameWithCrossDeviceFallback(tmp, path, renameOps);
240
257
  }
241
258
  function loadConfig(flags) {
242
259
  const file = readConfigFile();
@@ -357,6 +374,18 @@ async function ensureFreshToken(creds) {
357
374
  return updated.access_token;
358
375
  }
359
376
 
377
+ // src/auth/hints.ts
378
+ var NO_CREDENTIALS_REMEDIATION = [
379
+ "Log in: mmx auth login",
380
+ "Pass per-call: --api-key sk-xxxxx",
381
+ "Or set env var: MMX_CONFIG_DIR=/path/to/.mmx"
382
+ ].join(`
383
+ `);
384
+ function buildNoCredentialsHint(configPath) {
385
+ return `Looked for credentials in: ${configPath ?? getConfigPath()}
386
+ ${NO_CREDENTIALS_REMEDIATION}`;
387
+ }
388
+
360
389
  // src/auth/resolver.ts
361
390
  async function resolveCredential(config) {
362
391
  if (config.apiKey) {
@@ -370,8 +399,7 @@ async function resolveCredential(config) {
370
399
  if (config.fileApiKey) {
371
400
  return { token: config.fileApiKey, method: "api-key", source: "config.json" };
372
401
  }
373
- throw new CLIError("No credentials found.", ExitCode.AUTH, `Log in: mmx auth login
374
- Pass directly: --api-key sk-xxxxx`);
402
+ throw new CLIError("No credentials found.", ExitCode.AUTH, buildNoCredentialsHint(config.configPath));
375
403
  }
376
404
 
377
405
  // src/errors/api.ts
@@ -468,7 +496,7 @@ function maybeShowStatusBar(config, token, model) {
468
496
  // package.json
469
497
  var package_default = {
470
498
  name: "mmx-cli",
471
- version: "1.0.16",
499
+ version: "1.0.17",
472
500
  description: "CLI for the MiniMax AI Platform",
473
501
  type: "module",
474
502
  engines: {
@@ -717,11 +745,10 @@ function vlmEndpoint(baseUrl) {
717
745
  return `${baseUrl}/v1/coding_plan/vlm`;
718
746
  }
719
747
  function quotaEndpoint(baseUrl) {
720
- const host = baseUrl.includes("minimaxi.com") ? "https://api.minimaxi.com" : "https://api.minimax.io";
721
- return `${host}/v1/api/openplatform/coding_plan/remains`;
748
+ return `${baseUrl}/v1/token_plan/remains`;
722
749
  }
723
750
  function fileUploadEndpoint(baseUrl) {
724
- return `${baseUrl}/v1/files`;
751
+ return `${baseUrl}/v1/files/upload`;
725
752
  }
726
753
  function fileListEndpoint(baseUrl) {
727
754
  return `${baseUrl}/v1/files/list`;
@@ -1071,7 +1098,7 @@ import { existsSync as existsSync3, mkdirSync as mkdirSync2, writeFileSync as wr
1071
1098
  import { join as join2, resolve as resolve2, dirname as dirname2 } from "node:path";
1072
1099
 
1073
1100
  // src/files/download.ts
1074
- import { createWriteStream, renameSync as renameSync2, unlinkSync } from "fs";
1101
+ import { createWriteStream, renameSync as renameSync2, unlinkSync as unlinkSync2 } from "fs";
1075
1102
 
1076
1103
  // src/output/progress.ts
1077
1104
  var SPINNER_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
@@ -1194,7 +1221,7 @@ async function downloadFile(url, destPath, opts) {
1194
1221
  } finally {
1195
1222
  if (!completed) {
1196
1223
  try {
1197
- unlinkSync(tmpPath);
1224
+ unlinkSync2(tmpPath);
1198
1225
  } catch {}
1199
1226
  }
1200
1227
  }
@@ -1401,10 +1428,80 @@ import { existsSync as existsSync4, mkdirSync as mkdirSync3, writeFileSync as wr
1401
1428
  import { resolve as resolve3, dirname as dirname3 } from "node:path";
1402
1429
 
1403
1430
  // src/commands/music/models.ts
1431
+ var MUSIC_GENERATE_MODELS = [
1432
+ "music-2.6",
1433
+ "music-2.6-free",
1434
+ "music-2.5+",
1435
+ "music-2.5"
1436
+ ];
1437
+ var MUSIC_COVER_MODELS = ["music-cover", "music-cover-free"];
1438
+ function includesModel(models, model) {
1439
+ return models.includes(model);
1440
+ }
1404
1441
  function musicGenerateModel(config) {
1405
- return config.defaultMusicModel ?? "music-2.6";
1442
+ if (config.defaultMusicModel && includesModel(MUSIC_GENERATE_MODELS, config.defaultMusicModel)) {
1443
+ return config.defaultMusicModel;
1444
+ }
1445
+ return "music-2.6";
1446
+ }
1447
+
1448
+ // src/utils/audio-stream.ts
1449
+ function decodeHexAudio(hex) {
1450
+ if (!/^[0-9a-fA-F]+$/.test(hex)) {
1451
+ throw new CLIError("API returned invalid audio data (not valid hex).", ExitCode.GENERAL);
1452
+ }
1453
+ if (hex.length % 2 !== 0) {
1454
+ throw new CLIError("API returned truncated audio data (odd-length hex string).", ExitCode.GENERAL);
1455
+ }
1456
+ return Uint8Array.from(Buffer.from(hex, "hex"));
1457
+ }
1458
+ function throwIfApiError(response, payload) {
1459
+ const statusCode = payload.base_resp?.status_code;
1460
+ if (statusCode !== undefined && statusCode !== 0 || payload.error) {
1461
+ throw mapApiError(response.status, payload, response.url || undefined);
1462
+ }
1463
+ }
1464
+ function missingAudioError() {
1465
+ return new CLIError("API stream ended without audio data.", ExitCode.GENERAL);
1466
+ }
1467
+ async function* decodeAudioStream(response) {
1468
+ const contentType = response.headers.get("content-type") || "";
1469
+ if (!contentType.toLowerCase().includes("text/event-stream")) {
1470
+ let payload;
1471
+ try {
1472
+ payload = await response.json();
1473
+ } catch {
1474
+ throw new CLIError(`Expected SSE audio stream but got content-type "${contentType || "unknown"}".`, ExitCode.GENERAL);
1475
+ }
1476
+ throwIfApiError(response, payload);
1477
+ const hex = payload.data?.audio;
1478
+ if (!hex)
1479
+ throw missingAudioError();
1480
+ yield decodeHexAudio(hex);
1481
+ return;
1482
+ }
1483
+ let receivedAudio = false;
1484
+ for await (const event of parseSSE(response)) {
1485
+ if (!event.data || event.data === "[DONE]")
1486
+ break;
1487
+ let parsed;
1488
+ try {
1489
+ parsed = JSON.parse(event.data);
1490
+ } catch (error) {
1491
+ throw new CLIError(`Failed to parse audio stream chunk: ${error instanceof Error ? error.message : String(error)}`, ExitCode.GENERAL);
1492
+ }
1493
+ throwIfApiError(response, parsed);
1494
+ const hex = parsed.data?.audio;
1495
+ if (hex) {
1496
+ receivedAudio = true;
1497
+ yield decodeHexAudio(hex);
1498
+ }
1499
+ if (parsed.data?.status === 2)
1500
+ break;
1501
+ }
1502
+ if (!receivedAudio)
1503
+ throw missingAudioError();
1406
1504
  }
1407
- var VALID_COVER_MODELS = new Set(["music-cover"]);
1408
1505
 
1409
1506
  // src/sdk/music/index.ts
1410
1507
  function hexToBuffer2(hex) {
@@ -1429,20 +1526,10 @@ class MusicSDK extends Client {
1429
1526
  body,
1430
1527
  stream: true
1431
1528
  });
1432
- const reader = res.body?.getReader();
1433
- if (!reader) {
1529
+ if (!res.body) {
1434
1530
  throw new SDKError("No response body", ExitCode.GENERAL);
1435
1531
  }
1436
- try {
1437
- while (true) {
1438
- const { done, value } = await reader.read();
1439
- if (done)
1440
- break;
1441
- yield value;
1442
- }
1443
- } finally {
1444
- reader.releaseLock();
1445
- }
1532
+ yield* decodeAudioStream(res);
1446
1533
  }
1447
1534
  async generate(request2) {
1448
1535
  const body = this.validateParams(request2);
@@ -1493,18 +1580,17 @@ class MusicSDK extends Client {
1493
1580
  structuredParts.push(`Key: ${request2.key}`);
1494
1581
  if (request2.avoid)
1495
1582
  structuredParts.push(`Avoid: ${request2.avoid}`);
1496
- if (request2.useCase)
1497
- structuredParts.push(`Use case: ${request2.useCase}`);
1583
+ const useCase = request2.useCase || request2.use_case;
1584
+ if (useCase)
1585
+ structuredParts.push(`Use case: ${useCase}`);
1498
1586
  if (request2.structure)
1499
1587
  structuredParts.push(`Structure: ${request2.structure}`);
1500
1588
  if (request2.references)
1501
1589
  structuredParts.push(`References: ${request2.references}`);
1502
1590
  if (request2.extra)
1503
1591
  structuredParts.push(`Extra: ${request2.extra}`);
1504
- let lyrics = request2.lyrics;
1505
1592
  let prompt = request2.prompt;
1506
- if (request2.instrumental || !lyrics || lyrics === "无歌词" || lyrics === "no lyrics") {
1507
- lyrics = "[intro] [outro]";
1593
+ if (request2.is_instrumental || request2.instrumental) {
1508
1594
  structuredParts.push("Style: instrumental, no vocals, pure music");
1509
1595
  }
1510
1596
  if (structuredParts.length > 0) {
@@ -1514,6 +1600,33 @@ class MusicSDK extends Client {
1514
1600
  return prompt;
1515
1601
  }
1516
1602
  validateParams(params) {
1603
+ const instrumental = params.instrumental;
1604
+ const apiParams = { ...params };
1605
+ const sdkOnlyFields = [
1606
+ "instrumental",
1607
+ "vocals",
1608
+ "genre",
1609
+ "mood",
1610
+ "instruments",
1611
+ "tempo",
1612
+ "bpm",
1613
+ "key",
1614
+ "avoid",
1615
+ "use_case",
1616
+ "useCase",
1617
+ "structure",
1618
+ "references",
1619
+ "extra"
1620
+ ];
1621
+ for (const field of sdkOnlyFields)
1622
+ delete apiParams[field];
1623
+ if (instrumental !== undefined && params.is_instrumental !== undefined && instrumental !== params.is_instrumental) {
1624
+ throw new SDKError("instrumental and is_instrumental must not conflict", ExitCode.USAGE);
1625
+ }
1626
+ const normalized = {
1627
+ ...apiParams,
1628
+ is_instrumental: params.is_instrumental ?? instrumental
1629
+ };
1517
1630
  const {
1518
1631
  model,
1519
1632
  output_format,
@@ -1522,22 +1635,15 @@ class MusicSDK extends Client {
1522
1635
  lyrics,
1523
1636
  is_instrumental,
1524
1637
  lyrics_optimizer
1525
- } = params;
1526
- if (is_instrumental && lyrics) {
1527
- throw new SDKError("Cannot use is_instrumental with lyrics", ExitCode.USAGE);
1528
- }
1529
- if (lyrics_optimizer && (lyrics || is_instrumental)) {
1530
- throw new SDKError("Cannot use lyrics_optimizer with lyrics or is_instrumental", ExitCode.USAGE);
1531
- }
1532
- if (!prompt && !lyrics && !is_instrumental && !lyrics_optimizer) {
1533
- throw new SDKError("At least one of prompt or lyrics or is_instrumental or lyrics_optimizer is required", ExitCode.USAGE);
1534
- }
1535
- if (!is_instrumental && !lyrics_optimizer && !lyrics?.trim()) {
1536
- throw new SDKError("lyrics is required", ExitCode.USAGE);
1537
- }
1538
- const VALID_MODELS = ["music-2.6", "music-2.5+", "music-2.5"];
1539
- if (model && !VALID_MODELS.includes(model)) {
1540
- throw new SDKError(`Invalid model: ${model}. Valid models are ${VALID_MODELS.join(", ")}.`, ExitCode.USAGE);
1638
+ } = normalized;
1639
+ const effectiveModel = model ?? musicGenerateModel(this.config);
1640
+ const isGenerateModel = MUSIC_GENERATE_MODELS.includes(effectiveModel);
1641
+ const isCoverModel = MUSIC_COVER_MODELS.includes(effectiveModel);
1642
+ if (!isGenerateModel && !isCoverModel) {
1643
+ throw new SDKError(`Invalid model: ${effectiveModel}. Valid models are ${[
1644
+ ...MUSIC_GENERATE_MODELS,
1645
+ ...MUSIC_COVER_MODELS
1646
+ ].join(", ")}.`, ExitCode.USAGE);
1541
1647
  }
1542
1648
  const VALID_OUTPUT_FORMATS = ["hex", "url"];
1543
1649
  if (output_format && !VALID_OUTPUT_FORMATS.includes(output_format)) {
@@ -1546,9 +1652,78 @@ class MusicSDK extends Client {
1546
1652
  if (stream && output_format === "url") {
1547
1653
  throw new SDKError(`stream and output_format url cannot be used together. Streaming requires hex format.`, ExitCode.USAGE);
1548
1654
  }
1549
- const targetPrompt = this.buildPrompt(params);
1655
+ let targetPrompt = this.buildPrompt({
1656
+ ...params,
1657
+ is_instrumental
1658
+ });
1659
+ if (isCoverModel) {
1660
+ if (is_instrumental) {
1661
+ throw new SDKError("is_instrumental is only supported by music generation models", ExitCode.USAGE);
1662
+ }
1663
+ if (lyrics_optimizer) {
1664
+ throw new SDKError("lyrics_optimizer is only supported by music generation models", ExitCode.USAGE);
1665
+ }
1666
+ delete normalized.is_instrumental;
1667
+ delete normalized.lyrics_optimizer;
1668
+ targetPrompt = targetPrompt?.trim();
1669
+ const coverPromptLength = targetPrompt?.length ?? 0;
1670
+ if (coverPromptLength < 10 || coverPromptLength > 300) {
1671
+ throw new SDKError("prompt must be between 10 and 300 characters for music cover models", ExitCode.USAGE);
1672
+ }
1673
+ const hasAudioUrl = Boolean(normalized.audio_url?.trim());
1674
+ const hasAudioBase64 = Boolean(normalized.audio_base64?.trim());
1675
+ const hasCoverFeatureId = Boolean(normalized.cover_feature_id?.trim());
1676
+ const sourceCount = [hasAudioUrl, hasAudioBase64, hasCoverFeatureId].filter(Boolean).length;
1677
+ if (sourceCount !== 1) {
1678
+ throw new SDKError("Exactly one of audio_url, audio_base64, or cover_feature_id is required for music cover models", ExitCode.USAGE);
1679
+ }
1680
+ if (hasAudioUrl)
1681
+ normalized.audio_url = normalized.audio_url?.trim();
1682
+ else
1683
+ delete normalized.audio_url;
1684
+ if (hasAudioBase64)
1685
+ normalized.audio_base64 = normalized.audio_base64?.trim();
1686
+ else
1687
+ delete normalized.audio_base64;
1688
+ if (hasCoverFeatureId) {
1689
+ normalized.cover_feature_id = normalized.cover_feature_id?.trim();
1690
+ } else {
1691
+ delete normalized.cover_feature_id;
1692
+ }
1693
+ const coverLyrics = lyrics?.trim();
1694
+ const lyricsLength = coverLyrics?.length ?? 0;
1695
+ if (hasCoverFeatureId && lyricsLength === 0) {
1696
+ throw new SDKError("lyrics is required with cover_feature_id", ExitCode.USAGE);
1697
+ }
1698
+ if (coverLyrics)
1699
+ normalized.lyrics = coverLyrics;
1700
+ else
1701
+ delete normalized.lyrics;
1702
+ if (lyricsLength > 0 && (lyricsLength < 10 || lyricsLength > 1000)) {
1703
+ throw new SDKError("lyrics must be between 10 and 1000 characters for music cover models", ExitCode.USAGE);
1704
+ }
1705
+ } else {
1706
+ if (normalized.audio_url || normalized.audio_base64 || normalized.cover_feature_id) {
1707
+ throw new SDKError("audio_url, audio_base64, and cover_feature_id are only supported by music cover models", ExitCode.USAGE);
1708
+ }
1709
+ if (is_instrumental && lyrics) {
1710
+ throw new SDKError("Cannot use is_instrumental with lyrics", ExitCode.USAGE);
1711
+ }
1712
+ if (lyrics_optimizer && (lyrics || is_instrumental)) {
1713
+ throw new SDKError("Cannot use lyrics_optimizer with lyrics or is_instrumental", ExitCode.USAGE);
1714
+ }
1715
+ if (!prompt && !lyrics && !is_instrumental && !lyrics_optimizer) {
1716
+ throw new SDKError("At least one of prompt or lyrics or is_instrumental or lyrics_optimizer is required", ExitCode.USAGE);
1717
+ }
1718
+ if ((is_instrumental || lyrics_optimizer) && !prompt?.trim()) {
1719
+ throw new SDKError("prompt is required with is_instrumental or lyrics_optimizer", ExitCode.USAGE);
1720
+ }
1721
+ if (!is_instrumental && !lyrics_optimizer && !lyrics?.trim()) {
1722
+ throw new SDKError("lyrics is required", ExitCode.USAGE);
1723
+ }
1724
+ }
1550
1725
  return toMerged({
1551
- model: musicGenerateModel(this.config),
1726
+ model: effectiveModel,
1552
1727
  audio_setting: {
1553
1728
  format: "mp3",
1554
1729
  sample_rate: 44100,
@@ -1556,7 +1731,7 @@ class MusicSDK extends Client {
1556
1731
  },
1557
1732
  output_format: "hex"
1558
1733
  }, {
1559
- ...params,
1734
+ ...normalized,
1560
1735
  prompt: targetPrompt
1561
1736
  });
1562
1737
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mmx-cli",
3
- "version": "1.0.16",
3
+ "version": "1.0.17",
4
4
  "description": "CLI for the MiniMax AI Platform",
5
5
  "type": "module",
6
6
  "engines": {