feat: MiniMax provider for image/video/podcast skills + new music-generation skill (#3437)

* docs(spec): MiniMax integration for generation skills + new music skill

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* docs(plan): MiniMax generation providers implementation plan

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* test(skills): add importlib loader + FakeResp for skill tests

* test(skills): register loaded module in sys.modules; raise requests.HTTPError in FakeResp

* feat(image-generation): add MiniMax provider with env auto-detect

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* refactor(image-generation): guard unknown provider, derive ref MIME, strengthen tests

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(video-generation): add MiniMax provider with async poll/download

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* refactor(video-generation): surface base_resp errors while polling; add timeout test

* feat(podcast-generation): add MiniMax t2a_v2 provider with env auto-detect

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* refactor(podcast-generation): restore TTS credential guard; add volcengine + voice tests

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(music-generation): new MiniMax music skill via skill-creator

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* refactor(music-generation): treat empty lyrics as absent; test no-audio-data path

* refactor(skills): add request timeouts to MiniMax network calls

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Potential fix for pull request finding 'Explicit returns mixed with implicit (fall through) returns'

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>

* fix(models): strip inconsistent user-message names for MiniMax chat

DeerFlow middlewares tag user messages with provenance names (user-input, summary, loop_warning); langchain serializes them into the OpenAI-compatible payload and MiniMax rejects mismatched user-message names with "user name must be consistent (2013)". PatchedChatMiniMax now drops the per-message name from user-role messages. Point the config.example MiniMax models at PatchedChatMiniMax so they also get reasoning_content mapping.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(image-generation): MiniMax sends JSON prompt field, guard 1500-char limit

MiniMax image-01 takes one text string capped at 1500 chars, but the skill was sending the whole structured JSON. The MiniMax provider now extracts the JSON `prompt` field (relying on prompt_optimizer to expand it) and fails fast with a clear error before calling the API when that field exceeds 1500 chars. Authoring stays provider-agnostic; Gemini still receives the full JSON.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(podcast-generation): per-provider TTS concurrency and retry/backoff

Each TTS provider owns its concurrency internally — MiniMax runs single-threaded to reduce rate-limit failures, Volcengine keeps 4 workers — with automatic retry and backoff on transient HTTP and base_resp errors. No caller-facing concurrency knob.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(skills): address Copilot review comments on generation skills

- video: add raise_for_status + timeout to the Gemini download/POST/poll calls so non-2xx responses surface as clear HTTP errors instead of JSON/KeyError or hangs
- video: check the task Fail status before the generic base_resp check so the failure keeps its task_id context
- video/image: create the output file parent directory before writing (matching music-generation) so nested output paths do not raise FileNotFoundError
- music: require a non-empty prompt and fail fast with ValueError instead of sending an empty prompt to the API

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(scripts): reclaim dev ports across worktrees in make stop/dev

All deer-flow worktrees (main checkout + linked worktrees) hardcode the same dev ports (8001/3000/2026), so a service started from any worktree must be reclaimable from another. stop_all now resolves the set of worktree roots (DEERFLOW_ROOTS) and treats a process as deer-flow-owned when its open files live under any of them. It also force-kills survivors on 2026 alongside 8001/3000, fixing `make dev` aborting on the nginx port preflight when a prior nginx lingered on 2026.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(view-image): hide the injected image-context message from the UI

ViewImageMiddleware injects a HumanMessage (text + base64 images) so the vision model can see viewed images, but it was the only internal injector that set neither hide_from_ui nor a hidden name, so it leaked into the chat UI (and IM channels) as a user bubble reading "Here are the images you've viewed:". Mark it with additional_kwargs={"hide_from_ui": True}, matching todo/dynamic_context injections, which the frontend isHiddenFromUIMessage and the channel sender already honor. The model still receives the full content.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(minimax): mark M2.7 models as text-only (no vision)

MiniMax M2.7 / M2.7-highspeed do not support vision; only M3 does. The
provider config asserted vision support for M2.7 in four places.

- config.example.yaml: 4 M2.7 entries -> supports_vision: false
- backend/docs/CONFIGURATION.md: M2.7 + highspeed -> supports_vision: false
- wizard: add LLMProvider.model_vision_overrides + extra_config_for() so
  selecting an M2.7 model writes supports_vision: false while M3 (default)
  keeps vision; wire it through setup_wizard.py
- tests: M2.7-highspeed fixture -> supports_vision=False; add
  test_minimax_vision_is_per_model

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
This commit is contained in:
DanielWalnut
2026-06-08 22:04:38 +08:00
committed by GitHub
parent 1651d1f1f5
commit cd5bedaa74
27 changed files with 3564 additions and 365 deletions
@@ -3,6 +3,8 @@ import base64
import json
import logging
import os
import random
import time
import uuid
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Literal, Optional
@@ -12,8 +14,14 @@ import requests
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
MINIMAX_DEFAULT_HOST = "https://api.minimaxi.com"
# MiniMax base_resp codes worth retrying: unknown, timeout, RPM limit, TPM limit.
MINIMAX_RETRYABLE_CODES = {1000, 1001, 1002, 1039}
DEFAULT_TTS_MAX_RETRIES = 4
DEFAULT_MAX_WORKERS = 4
DEFAULT_MINIMAX_MAX_WORKERS = 1
# Types
class ScriptLine:
def __init__(self, speaker: Literal["male", "female"] = "male", paragraph: str = ""):
self.speaker = speaker
@@ -30,113 +38,243 @@ class Script:
script = cls(locale=data.get("locale", "en"))
for line in data.get("lines", []):
script.lines.append(
ScriptLine(
speaker=line.get("speaker", "male"),
paragraph=line.get("paragraph", ""),
)
ScriptLine(speaker=line.get("speaker", "male"),
paragraph=line.get("paragraph", ""))
)
return script
def text_to_speech(text: str, voice_type: str) -> Optional[bytes]:
"""Convert text to speech using Volcengine TTS."""
def _resolve_provider(override_env: str, existing_provider: str, has_existing_creds: bool) -> str:
override = os.getenv(override_env)
if override:
return override.strip().lower()
if has_existing_creds:
return existing_provider
if os.getenv("MINIMAX_API_KEY"):
return "minimax"
raise ValueError(
f"No credentials found. Set VOLCENGINE_TTS_APPID + VOLCENGINE_TTS_ACCESS_TOKEN "
f"for {existing_provider}, or MINIMAX_API_KEY for minimax "
f"(optionally force with {override_env})."
)
def _resolve_tts_provider() -> str:
has_volc = bool(
os.getenv("VOLCENGINE_TTS_APPID") and os.getenv("VOLCENGINE_TTS_ACCESS_TOKEN")
)
provider = _resolve_provider("PODCAST_GENERATION_PROVIDER", "volcengine", has_volc)
if provider not in ("volcengine", "minimax"):
raise ValueError(
f"Unknown podcast provider: {provider!r} (use 'volcengine' or 'minimax')"
)
return provider
def _default_max_retries() -> int:
try:
return int(os.getenv("MINIMAX_TTS_MAX_RETRIES", str(DEFAULT_TTS_MAX_RETRIES)))
except ValueError:
return DEFAULT_TTS_MAX_RETRIES
def _default_max_workers(provider: str) -> int:
"""Each provider owns its own concurrency: MiniMax stays low to avoid rate
limits, Volcengine keeps the historical default. Not user-tunable by design.
"""
if provider == "minimax":
return DEFAULT_MINIMAX_MAX_WORKERS
return DEFAULT_MAX_WORKERS
def _parse_retry_after(response) -> Optional[float]:
"""Return the server-provided Retry-After (seconds), if any."""
headers = getattr(response, "headers", None) or {}
value = headers.get("Retry-After")
try:
return float(value) if value else None
except (TypeError, ValueError):
return None
def _backoff_sleep(attempt: int, retry_after: Optional[float]) -> None:
"""Sleep with exponential backoff + jitter, honoring Retry-After when present.
Jitter de-synchronizes concurrent workers that all got rate-limited at once,
avoiding a thundering-herd retry storm.
"""
base = retry_after if retry_after else min(2 ** attempt, 30)
time.sleep(base + random.uniform(0, 1))
def text_to_speech_volcengine(
text: str, voice_type: str, max_retries: Optional[int] = None
) -> Optional[bytes]:
"""Convert text to speech using Volcengine TTS (returns base64-decoded mp3 bytes).
Retries with exponential backoff on transient HTTP errors (429 / 5xx).
"""
app_id = os.getenv("VOLCENGINE_TTS_APPID")
access_token = os.getenv("VOLCENGINE_TTS_ACCESS_TOKEN")
cluster = os.getenv("VOLCENGINE_TTS_CLUSTER", "volcano_tts")
if not app_id or not access_token:
raise ValueError(
"VOLCENGINE_TTS_APPID and VOLCENGINE_TTS_ACCESS_TOKEN environment variables must be set"
)
if max_retries is None:
max_retries = _default_max_retries()
url = "https://openspeech.bytedance.com/api/v1/tts"
# Authentication: Bearer token with semicolon separator
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer;{access_token}",
}
headers = {"Content-Type": "application/json", "Authorization": f"Bearer;{access_token}"}
payload = {
"app": {
"appid": app_id,
"token": "access_token", # literal string, not the actual token
"cluster": cluster,
},
"app": {"appid": app_id, "token": "access_token", "cluster": cluster},
"user": {"uid": "podcast-generator"},
"audio": {
"voice_type": voice_type,
"encoding": "mp3",
"speed_ratio": 1.2,
},
"request": {
"reqid": str(uuid.uuid4()), # must be unique UUID
"text": text,
"text_type": "plain",
"operation": "query",
},
"audio": {"voice_type": voice_type, "encoding": "mp3", "speed_ratio": 1.2},
"request": {"reqid": str(uuid.uuid4()), "text": text,
"text_type": "plain", "operation": "query"},
}
try:
response = requests.post(url, json=payload, headers=headers)
for attempt in range(max_retries + 1):
try:
response = requests.post(url, json=payload, headers=headers, timeout=60)
except Exception as e:
logger.error(f"TTS error: {e}")
if attempt < max_retries:
_backoff_sleep(attempt, None)
continue
return None
if response.status_code == 429 or response.status_code >= 500:
logger.warning(
f"Volcengine TTS transient HTTP {response.status_code} "
f"(attempt {attempt + 1}/{max_retries + 1})"
)
if attempt < max_retries:
_backoff_sleep(attempt, _parse_retry_after(response))
continue
return None
if response.status_code != 200:
logger.error(f"TTS API error: {response.status_code} - {response.text}")
return None
result = response.json()
if result.get("code") != 3000:
logger.error(f"TTS error: {result.get('message')} (code: {result.get('code')})")
return None
audio_data = result.get("data")
if audio_data:
return base64.b64decode(audio_data)
except Exception as e:
logger.error(f"TTS error: {str(e)}")
return None
return None
def _process_line(args: tuple[int, ScriptLine, int]) -> tuple[int, Optional[bytes]]:
def text_to_speech_minimax(
text: str, voice_id: str, max_retries: Optional[int] = None
) -> Optional[bytes]:
"""Convert text to speech using MiniMax t2a_v2 (returns hex-decoded mp3 bytes).
Retries with exponential backoff on HTTP 429/5xx and on retryable base_resp
codes (rate/TPM limits, timeouts). Permanent errors (auth, balance, bad input)
are not retried.
"""
api_key = os.getenv("MINIMAX_API_KEY")
host = os.getenv("MINIMAX_API_HOST", MINIMAX_DEFAULT_HOST).rstrip("/")
if max_retries is None:
max_retries = _default_max_retries()
payload = {
"model": os.getenv("MINIMAX_TTS_MODEL", "speech-2.6-hd"),
"text": text,
"voice_setting": {"voice_id": voice_id, "speed": 1.0, "vol": 1.0, "pitch": 0},
"audio_setting": {"sample_rate": 32000, "bitrate": 128000, "format": "mp3", "channel": 1},
"output_format": "hex",
}
for attempt in range(max_retries + 1):
try:
response = requests.post(
f"{host}/v1/t2a_v2",
headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"},
json=payload,
timeout=60,
)
except Exception as e:
logger.error(f"MiniMax TTS error: {e}")
if attempt < max_retries:
_backoff_sleep(attempt, None)
continue
return None
if response.status_code == 429 or response.status_code >= 500:
logger.warning(
f"MiniMax TTS rate-limited HTTP {response.status_code} "
f"(attempt {attempt + 1}/{max_retries + 1})"
)
if attempt < max_retries:
_backoff_sleep(attempt, _parse_retry_after(response))
continue
return None
if response.status_code != 200:
logger.error(f"MiniMax TTS error: {response.status_code} - {response.text}")
return None
result = response.json()
base = result.get("base_resp") or {}
code = base.get("status_code", 0)
if code in MINIMAX_RETRYABLE_CODES:
logger.warning(
f"MiniMax TTS retryable error {code}: {base.get('status_msg')} "
f"(attempt {attempt + 1}/{max_retries + 1})"
)
if attempt < max_retries:
_backoff_sleep(attempt, None)
continue
return None
if code != 0:
logger.error(f"MiniMax TTS error {code}: {base.get('status_msg')}")
return None
audio_hex = (result.get("data") or {}).get("audio")
if audio_hex:
return bytes.fromhex(audio_hex)
return None
return None
def _process_line(args: tuple[int, ScriptLine, int, str]) -> tuple[int, Optional[bytes]]:
"""Process a single script line for TTS. Returns (index, audio_bytes)."""
i, line, total = args
# Select voice based on speaker gender
if line.speaker == "male":
voice_type = "zh_male_yangguangqingnian_moon_bigtts" # Male voice
i, line, total, provider = args
logger.info(f"Processing line {i + 1}/{total} ({line.speaker}) via {provider}")
if provider == "minimax":
if line.speaker == "male":
voice = os.getenv("MINIMAX_TTS_VOICE_MALE", "male-qn-qingse")
else:
voice = os.getenv("MINIMAX_TTS_VOICE_FEMALE", "female-tianmei")
audio = text_to_speech_minimax(line.paragraph, voice)
else:
voice_type = "zh_female_sajiaonvyou_moon_bigtts" # Female voice
logger.info(f"Processing line {i + 1}/{total} ({line.speaker})")
audio = text_to_speech(line.paragraph, voice_type)
if line.speaker == "male":
voice = "zh_male_yangguangqingnian_moon_bigtts"
else:
voice = "zh_female_sajiaonvyou_moon_bigtts"
audio = text_to_speech_volcengine(line.paragraph, voice)
if not audio:
logger.warning(f"Failed to generate audio for line {i + 1}")
return (i, audio)
def tts_node(script: Script, max_workers: int = 4) -> list[bytes]:
"""Convert script lines to audio chunks using TTS with multi-threading."""
logger.info(f"Converting script to audio using {max_workers} workers...")
def tts_node(script: Script) -> list[bytes]:
"""Convert script lines to audio chunks using TTS with multi-threading.
Concurrency is owned by the resolved provider (see _default_max_workers);
there is no caller-facing knob. Fails loudly: if any line cannot be
synthesized (even after retries), raise rather than silently emitting an
incomplete podcast.
"""
total = len(script.lines)
# Handle empty script case
if total == 0:
raise ValueError("Script contains no lines to process")
# Validate required environment variables before starting TTS
if not os.getenv("VOLCENGINE_TTS_APPID") or not os.getenv("VOLCENGINE_TTS_ACCESS_TOKEN"):
provider = _resolve_tts_provider()
max_workers = _default_max_workers(provider)
if provider == "volcengine" and not (
os.getenv("VOLCENGINE_TTS_APPID") and os.getenv("VOLCENGINE_TTS_ACCESS_TOKEN")
):
raise ValueError(
"Missing required environment variables: VOLCENGINE_TTS_APPID and VOLCENGINE_TTS_ACCESS_TOKEN must be set"
"Volcengine TTS selected but VOLCENGINE_TTS_APPID / "
"VOLCENGINE_TTS_ACCESS_TOKEN are not set"
)
if provider == "minimax" and not os.getenv("MINIMAX_API_KEY"):
raise ValueError("MiniMax TTS selected but MINIMAX_API_KEY is not set")
logger.info(f"Converting script to audio using {max_workers} workers (provider={provider})...")
tasks = [(i, line, total, provider) for i, line in enumerate(script.lines)]
tasks = [(i, line, total) for i, line in enumerate(script.lines)]
# Use ThreadPoolExecutor for parallel TTS generation
results: dict[int, Optional[bytes]] = {}
failed_indices: list[int] = []
with ThreadPoolExecutor(max_workers=max_workers) as executor:
@@ -144,81 +282,52 @@ def tts_node(script: Script, max_workers: int = 4) -> list[bytes]:
for future in as_completed(futures):
idx, audio = future.result()
results[idx] = audio
# Use `not audio` to catch both None and empty bytes
if not audio:
failed_indices.append(idx)
# Log failed lines with 1-based indices for user-friendly output
if failed_indices:
logger.warning(
f"Failed to generate audio for {len(failed_indices)}/{total} lines: "
f"line numbers {sorted(i + 1 for i in failed_indices)}"
)
# Collect results in order, skipping failed ones
audio_chunks = []
for i in range(total):
audio = results.get(i)
if audio:
audio_chunks.append(audio)
logger.info(f"Generated {len(audio_chunks)}/{total} audio chunks successfully")
if not audio_chunks:
raise ValueError(
f"TTS generation failed for all {total} lines. "
"Please check VOLCENGINE_TTS_APPID and VOLCENGINE_TTS_ACCESS_TOKEN environment variables."
f"TTS failed for {len(failed_indices)}/{total} lines after retries: "
f"line numbers {sorted(i + 1 for i in failed_indices)}. "
f"This is usually transient API rate limiting — wait a moment and retry."
)
audio_chunks = [results[i] for i in range(total)]
logger.info(f"Generated {len(audio_chunks)}/{total} audio chunks successfully")
return audio_chunks
def mix_audio(audio_chunks: list[bytes]) -> bytes:
"""Combine audio chunks into a single audio file."""
logger.info("Mixing audio chunks...")
if not audio_chunks:
raise ValueError("No audio chunks to mix - TTS generation may have failed")
output = b"".join(audio_chunks)
if len(output) == 0:
raise ValueError("Mixed audio is empty - TTS generation may have failed")
logger.info(f"Audio mixing complete: {len(output)} bytes")
return output
def generate_markdown(script: Script, title: str = "Podcast Script") -> str:
"""Generate a markdown script from the podcast script."""
lines = [f"# {title}", ""]
for line in script.lines:
speaker_name = "**Host (Male)**" if line.speaker == "male" else "**Host (Female)**"
lines.append(f"{speaker_name}: {line.paragraph}")
lines.append("")
return "\n".join(lines)
def generate_podcast(
script_file: str,
output_file: str,
transcript_file: Optional[str] = None,
) -> str:
"""Generate a podcast from a script JSON file."""
# Read script JSON
def generate_podcast(script_file: str, output_file: str,
transcript_file: Optional[str] = None) -> str:
with open(script_file, "r", encoding="utf-8") as f:
script_json = json.load(f)
if "lines" not in script_json:
raise ValueError(f"Invalid script format: missing 'lines' key. Got keys: {list(script_json.keys())}")
raise ValueError(
f"Invalid script format: missing 'lines' key. Got keys: {list(script_json.keys())}"
)
script = Script.from_dict(script_json)
logger.info(f"Loaded script with {len(script.lines)} lines")
# Generate transcript markdown if requested
if transcript_file:
title = script_json.get("title", "Podcast Script")
markdown_content = generate_markdown(script, title)
@@ -229,16 +338,11 @@ def generate_podcast(
f.write(markdown_content)
logger.info(f"Generated transcript to {transcript_file}")
# Convert to audio
audio_chunks = tts_node(script)
if not audio_chunks:
raise Exception("Failed to generate any audio")
# Mix audio
output_audio = mix_audio(audio_chunks)
# Save output
output_dir = os.path.dirname(output_file)
if output_dir:
os.makedirs(output_dir, exist_ok=True)
@@ -253,30 +357,15 @@ def generate_podcast(
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate podcast from script JSON file")
parser.add_argument(
"--script-file",
required=True,
help="Absolute path to script JSON file",
)
parser.add_argument(
"--output-file",
required=True,
help="Output path for generated podcast MP3",
)
parser.add_argument(
"--transcript-file",
required=False,
help="Output path for transcript markdown file (optional)",
)
parser.add_argument("--script-file", required=True, help="Absolute path to script JSON file")
parser.add_argument("--output-file", required=True, help="Output path for generated podcast MP3")
parser.add_argument("--transcript-file", required=False,
help="Output path for transcript markdown file (optional)")
args = parser.parse_args()
try:
result = generate_podcast(
args.script_file,
args.output_file,
args.transcript_file,
)
result = generate_podcast(args.script_file, args.output_file,
args.transcript_file)
print(result)
except Exception as e:
import traceback