Skip to content

feat(tools): add text-to-music generation tool - #1265

Merged
edenreich merged 6 commits into
mainfrom
fix/issue-1261
Sep 22, 2026
Merged

edenreich merged 6 commits into
mainfrom
fix/issue-1261

Conversation

@inference-gateway-maintainer

@inference-gateway-maintainer inference-gateway-maintainer Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #1261

Summary

Adds a TextToMusic tool that composes a music clip from a text prompt through the gateway's Music API (sdk.Client.CreateMusic, response_format: mp3), mirroring the TextToSpeech / TextToSFX shape: prompt, optional seconds (maps to duration_seconds), optional instrumental, and output_path (bare file name). The tool is gated by text_to_music.enabled (default false) and reports path and prompt. Output is MP3: the gateway music providers do not serve wav (ElevenLabs offers mp3, opus and pcm), verified against the real provider. It builds on the SDK v1.38.0 bump from PR #1262 (CreateMusic with per-endpoint CreateMusicRequestResponseFormatMp3).

Note on the mock gateway: the tokenless mock gateway does NOT serve POST /v1/audio/music, so the integration test pins the real behavior of a gateway without the endpoint - the tool call fails with a one-line error naming the configured model (music generation with elevenlabs/music_v2_5 failed: ...), the agent run still completes, and no partial file is left behind. The gap is tracked as inference-gateway/tokenless#59.

Note on the shared helper: the issue pointed at the output_path helper from #1227 (TextToSFX), which has not landed yet, so this PR extracts resolveMediaOutputPath out of TextToSpeechTool in internal/agent/tools/text_to_speech.go (behavior-preserving refactor plus the prefix parameter); #1227 can reuse it.

Changes

  • Domain: MusicService port in internal/agent/domain/media.go (error-only Compose(ctx, prompt, outPath, seconds, instrumental) signature) next to the speech port.
  • Infrastructure: internal/agent/infrastructure/music_service.go implementing MusicService via sdk.Client.CreateMusic with CreateMusicRequestResponseFormatMp3, writing the returned MP3 to the configured output dir.
  • Tool: internal/agent/tools/text_to_music.go - definition, validation, execution and formatting; failures (provider error, gateway without the endpoint, invalid output_path) surface as one-line failed results and leave no partial file (timestamped targets are removed on failure, caller-named targets are left untouched).
  • Config: top-level text_to_music block (enabled default false, model default elevenlabs/music_v2_5, output_dir default ~/.infer/tmp/music, require_approval) plus prompts entry; INFER_TEXT_TO_MUSIC_* env overrides come from the reflective env loader.
  • Wiring: registry and container wired with the new tool; counterfeiter fake tests/mocks/agentdomain/fake_music_service.go added to mocks:generate.
  • Shared helper: resolveMediaOutputPath extracted from TextToSpeechTool so output_path rejects directories, absolute paths and .. through one function.
  • Tests: unit tests for validation/execution/formatting/registry gating with the fake service, an infrastructure test with tests/mocks/sdk FakeClient, config tests, and an integration test running the tool against the mock gateway.
  • Docs: new docs/text-to-music.md page mirroring text-to-speech.md (enabling, env overrides, gateway requirements, tool usage, troubleshooting), linked from the README, docs/tools-reference.md and docs/directory-structure.md; docs/configuration-reference.md updated; a [DOCS] ticket is filed against inference-gateway/docs.
  • Gateway: Config.NeedsGatewayAudio() gates AUDIO_ENABLED=true (and the audio-restart probe) on gateway-engine speech or music. Before this, enabling text_to_music alone started the local gateway without /v1/audio/music and every call 404'd.

Review follow-ups (later commits)

  • refactor(tools): Validate folded back to the TextToSpeech shape (invalid output_path is an argument error, like speech), seconds/instrumental pointers passed straight to the SDK request, zero-value TextToMusic block dropped from DefaultConfig, one shared fakeWav() test helper.
  • docs(tools): the docs/text-to-music.md page above.
  • fix(gateway): the AUDIO_ENABLED gap above, with a "music only" row in TestNeedsAudioRestart.
  • fix(tools): live test against ElevenLabs showed the hardcoded wav format fails every call with a 400, so the tool now requests MP3, writes .mp3 and drops the WAV-only duration readout. The same test showed resolveMediaOutputPath only created the output directory on the timestamped branch, so a caller-named output_path failed with ENOENT on a fresh machine; it now creates it on both branches, which fixes TextToSpeech too.

Follow-ups

Adds a TextToMusic tool composing music from a text prompt through the gateway's Music API (sdk Client.CreateMusic, response_format wav), mirroring TextToSpeech/TextToSFX shape: prompt/seconds/instrumental/output_path inputs, text_to_music.enabled gating (default false), MusicService domain port with gateway implementation, shared resolveMediaOutputPath helper, container/registry wiring, counterfeiter fake, unit and integration tests, and docs.
Fold validatePromptAndOptions back into Validate, pass the optional
duration and instrumental pointers straight to the SDK request, drop the
zero-value TextToMusic block from DefaultConfig, and share one fakeWav
test helper between the media tool tests.
Covers enabling, env overrides, gateway requirements (AUDIO_ENABLED and
provider credentials), tool usage and troubleshooting; linked from the
README, the tools reference and the directory structure page.
…_music is on

The Audio API flag was keyed only off gateway-engine text_to_speech, so
enabling text_to_music alone started a gateway without /v1/audio/music
and every tool call failed with a 404. Route the three checks through
Config.NeedsGatewayAudio, which also covers music.
… for named files

The gateway music providers do not serve wav (elevenlabs offers mp3, opus
and pcm), so the hardcoded wav response format failed every call with a
400. Request mp3, write a .mp3, and drop the WAV-only duration readout.

resolveMediaOutputPath only created the output directory on the
timestamped branch, so a caller-named output_path failed with ENOENT on
a fresh machine; create it for both branches (fixes TextToSpeech too).
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.206.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] TextToMusic tool for text-to-music generation

1 participant