#19439: fix(tts): pass audioAsVoice flag through tool result pipeline
agents
size: S
trusted-contributor
Cluster:
Voice Call and TTS Improvements
Fixes #14174
## What
The built-in `tts` agent tool now correctly delivers voice messages on Telegram by passing the `audioAsVoice` flag through the tool result pipeline.
## Why
The TTS tool was returning `[[audio_as_voice]]` tags in text content, but this flag was being dropped when passing through `onToolResult` callbacks. This caused voice messages to be sent as regular audio file attachments instead of voice bubbles.
The parsing in `parseReplyDirectives` / `splitMediaFromOutput` was working correctly to extract the flag, but `emitToolOutput` and the downstream pipeline weren't passing it through.
## How
- Added `audioAsVoice?: boolean` to the `onToolResult` callback payload type in `pi-embedded-subscribe.types.ts`
- Updated `emitToolOutput` and `emitToolSummary` to pass `audioAsVoice` from `parseReplyDirectives`
- Added `extractToolResultAudioAsVoice()` helper to extract the flag for the direct media delivery path (when `shouldEmitToolOutput` is false)
- Passes `audioAsVoice` through `agent-runner-execution.ts` to the final delivery
## Testing
- [x] Added unit tests for `extractToolResultAudioAsVoice()`
- [x] Existing tests pass (`pnpm vitest run`)
- [x] `pnpm build` passes
## AI-Assisted 🤖
This PR was built with AI assistance (Claude via OpenClaw).
- [x] Code reviewed and understood
- [x] Matches existing patterns in the codebase
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR fixes a bug where the `tts` agent tool was delivering voice messages as regular audio file attachments on Telegram instead of voice bubbles. The `[[audio_as_voice]]` flag was being emitted by the TTS tool but dropped when passing through the `onToolResult` callback pipeline.
Key changes:
- Added `audioAsVoice?: boolean` to `onToolResult` callback payload types in `pi-embedded-subscribe.types.ts` and `pi-embedded-runner/run/params.ts`
- Added `extractToolResultAudioAsVoice()` in `pi-embedded-subscribe.tools.ts` to extract the flag for the direct media delivery path (when `shouldEmitToolOutput` is false)
- Updated `emitToolSummary` and `emitToolOutput` in `pi-embedded-subscribe.ts` to pass `audioAsVoice` from `parseReplyDirectives`
- Propagated `payload.audioAsVoice` through `agent-runner-execution.ts` to the outer `onToolResult` handler
- Added unit tests covering edge cases for `extractToolResultAudioAsVoice()`
The fix correctly threads the flag through both delivery paths (verbose tool output and direct media delivery). The implementation follows the same patterns used elsewhere in the codebase for `audioAsVoice` propagation.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge - it fixes a clear bug with a minimal, well-scoped change that follows existing codebase patterns.
- The changes are small and targeted, threading a single boolean flag through the tool result pipeline. The new `extractToolResultAudioAsVoice()` helper mirrors the existing `extractToolResultMediaPaths()` pattern exactly. Both delivery paths (verbose/emitToolOutput and direct media delivery) are handled. Unit tests cover null/undefined, non-object, missing content, and the TTS format. No pre-existing behavior is altered, and the type changes are backward compatible (optional field added).
- No files require special attention
<sub>Last reviewed commit: 8979a3b</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#21110: fix(tts): deliver audio via structured mediaUrl instead of MEDIA: t...
by hydro13 · 2026-02-19
85.0%
#21513: Agents: track TTS media in duplicate filter state
by DevvGwardo · 2026-02-20
81.9%
#20735: fix: skip auto-attaching tool MEDIA: paths already sent via message t…
by anillBhoi · 2026-02-19
79.0%
#19399: telegram: fix MEDIA false positives and partial final drop
by HOYALIM · 2026-02-17
78.8%
#21193: fix(tts): send voice messages as Opus bubbles on Telegram
by aris-katkova · 2026-02-19
78.4%
#18077: fix: deduplicate TTS audio delivered via tool results
by stakeswky · 2026-02-16
78.1%
#18890: fix(media): parse tool-result MEDIA directives with shared parser
by teededung · 2026-02-17
77.3%
#17552: fix(agents): suppress tool error warnings when assistant already re...
by AytuncYildizli · 2026-02-15
77.3%
#20992: fix(tts): apply TTS processing to agentCommand outbound delivery path
by mmyyfirstb · 2026-02-19
76.7%
#14227: fix(matrix): pass asVoice to audioAsVoice for native voice messages
by tfm-neo-ai · 2026-02-11
76.5%