# Sequence story calls

The order an agent calls the story tools in, and the traps between them. A story is a persistent script: each line has
a lane, a start time and, once read, a sound. For a complete worked example, see [Voice a scene](https://mirli.app/docs/agents-voice-scene/index.md).
Arguments and results are in [Voice and story tools](https://mirli.app/docs/mcp-stories/index.md).

## Before you begin

- The agent is connected over Streamable HTTP. The stdio bridge has no story tools.
- At least one text-to-speech model is installed: `list_models` shows one with `task: "tts"`.

## Build and read a story

1. Call `soundbox.list_voices` and choose a voice `id` for each speaker.
2. Call `soundbox.list_models` and read the `delivery` list of each voice's `model_id`.
3. Call `soundbox.create_story` with a `title`, and a story-wide `delivery` if the script needs one.

   The result carries `story_id` and `open_in_studio`.

4. Call `soundbox.add_lines` once with every line in order.

   In a new story, the lines are laid end to end on their lanes as they are read.

5. Call `soundbox.speak_story` with the `story_id` and no `model`.

   Each line is read on its own voice's model. The call blocks until every silent line is read, one at a time.

6. Call `soundbox.get_story` and check that every spoken line has `voiced: true`. Pauses have no audio and stay
   `voiced: false`.
7. Give the user the `open_in_studio` link.

## Add lines to a story that already has audio

A spoken line has no length until it is read, so `add_lines` cannot place a line after another unread line.

> [!WARNING]
> Lines appended together to a lane that already has audio all get the same `start_s`: the end of the lane's last
> voiced clip. After `speak_story` they play on top of each other.

To avoid it, do one of these:

- Give each new line its own `start_s` in the `add_lines` call.
- After `speak_story`, call `soundbox.get_story`, read each new line's `duration_s`, and move the lines apart with
  `soundbox.update_line` and `start_s`.

## Change a line

1. Call `soundbox.update_line` with the new `text`, `voice_id` or `delivery`.

   The old audio stays until the line is read again.

2. Call `soundbox.speak_story` with the `story_id`.

   Only lines without audio are read. Pass `regenerate: true` only to read every line again.

To shorten a clip without losing audio, set `duration_s` or `trim_start_s` with `update_line`. To cut a read line into
two clips, call `soundbox.split_line`; the words stay with the first clip.

## Mix and export

1. Call `soundbox.get_story` to see each lane and its clips.
2. Call `soundbox.mix_track` to name, mute, solo or set the gain of a lane.
3. Call `soundbox.add_device` to put an effect on a lane, then `soundbox.set_device` with the returned `device` id to
   change it.
4. Optional: call `soundbox.export_story` for one mixed file.

`export_story` renders each lane's devices into the mixdown, the same as the studio's playback. Bypassed devices are left
out.

## Edit the studio the user has open

1. Call `soundbox.studio_context`, and use only the lane numbers, clip ids and voice ids it returns.
2. Call `soundbox.studio_apply` with the actions and a short `summary` in the user's words. The actions are in the
   [studio action vocabulary](https://mirli.app/docs/agents-studio-actions/index.md).
3. If the result's `held_deletes` is more than 0, ask the user, then send the same deletes with `allow_delete: true`.
4. If you sent `generate_clip` or `speak_lines`, call `soundbox.studio_context` again after a few seconds to see the
   clips fill in.

## What's next

- [Voice a scene](https://mirli.app/docs/agents-voice-scene/index.md): every call for a two-character scene.
- [Troubleshoot agent calls](https://mirli.app/docs/agents-troubleshooting/index.md): errors and what to do about them.
