Skip to content

Commit 61f347e

Browse files
committed
fix(tts): deliver voice-only replies when speech runtime is cold
1 parent 4d00dc4 commit 61f347e

2 files changed

Lines changed: 84 additions & 40 deletions

File tree

‎src/tts/tts-payload.ts‎

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,17 @@ function hasLegacyFinalMediaDirective(text: string): boolean {
8383
return /(?:^|\n)\s*MEDIA\s*:/i.test(text);
8484
}
8585

86-
// A voice-only send (structured voiceText, empty visible text) must still end in a visible
87-
// outcome when speech cannot be attempted at all; otherwise delivery normalizes the empty
88-
// payload to null and the send silently vanishes. Mirrors the synthesis-failure fallback.
89-
function applyExplicitSpeechVisibleFallback(payload: ReplyPayload): ReplyPayload {
90-
const explicitText = getReplyPayloadMetadata(payload)?.tts?.text?.trim();
91-
if (!explicitText || hasReplyPayloadContent(payload, {})) {
86+
// Channel-owned content checks distinguish visible blocks from transport-only metadata.
87+
function applyExplicitSpeechVisibleFallback(
88+
payload: ReplyPayload,
89+
channel?: string,
90+
explicitText = getReplyPayloadMetadata(payload)?.tts?.text?.trim(),
91+
): ReplyPayload {
92+
const channelId = explicitText && payload.channelData ? normalizeMessageChannel(channel) : null;
93+
const hasChannelData = channelId
94+
? getChannelPlugin(channelId)?.messaging?.hasStructuredReplyPayload?.({ payload })
95+
: undefined;
96+
if (!explicitText || hasReplyPayloadContent(payload, { hasChannelData })) {
9297
return payload;
9398
}
9499
return { ...payload, text: explicitText };
@@ -108,7 +113,7 @@ export async function maybeApplyTtsToPayloadCore(
108113
persistTtsAudio: TtsAudioPersistence,
109114
): Promise<ReplyPayload> {
110115
if (!isSpeechRuntimeAvailable()) {
111-
return applyExplicitSpeechVisibleFallback(params.payload);
116+
return applyExplicitSpeechVisibleFallback(params.payload, params.channel);
112117
}
113118
if (params.payload.isCompactionNotice) {
114119
return params.payload;
@@ -292,13 +297,5 @@ export async function maybeApplyTtsToPayloadCore(
292297

293298
const latency = Date.now() - ttsStart;
294299
logVerbose(`TTS: conversion failed after ${latency}ms (${result.error ?? "unknown"}).`);
295-
const channelId =
296-
explicitTtsText && nextPayload.channelData ? normalizeMessageChannel(params.channel) : null;
297-
const hasChannelData = channelId
298-
? getChannelPlugin(channelId)?.messaging?.hasStructuredReplyPayload?.({ payload: nextPayload })
299-
: undefined;
300-
// Channel-owned content checks keep transport-only metadata from silently dropping the answer.
301-
return explicitTtsText && !hasReplyPayloadContent(nextPayload, { hasChannelData })
302-
? { ...nextPayload, text: explicitTtsText }
303-
: nextPayload;
300+
return applyExplicitSpeechVisibleFallback(nextPayload, params.channel, explicitTtsText);
304301
}

‎src/tts/tts-runtime-fallbacks.test.ts‎

Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -791,31 +791,78 @@ describe("TTS runtime provider fallback and delivery behavior", () => {
791791
});
792792

793793
describe("cold speech runtime visible fallback", () => {
794-
it("materializes voice-only structured speech as visible text when the runtime is unavailable", async () => {
795-
const { setSpeechRuntimeAvailabilityGuard } = await import("./runtime-availability.js");
796-
setSpeechRuntimeAvailabilityGuard(() => {
797-
throw new Error("speech runtime configured cold");
798-
});
799-
try {
800-
const payload: ReplyPayload = { text: "" };
801-
setReplyPayloadMetadata(payload, {
802-
ttsExplicit: true,
803-
tts: { tagged: true, text: "Spoken greeting" },
794+
it.each([
795+
{ loaded: true, structured: false },
796+
{ loaded: true, structured: true },
797+
{ loaded: false, structured: false },
798+
{ loaded: false, structured: true },
799+
])(
800+
"delivers voice-only speech when its runtime is unavailable (loaded: $loaded, structured: $structured)",
801+
async ({ loaded, structured }) => {
802+
const { setSpeechRuntimeAvailabilityGuard } = await import("./runtime-availability.js");
803+
const restoreRegistry = installStructuredReplyTestChannel(loaded);
804+
setSpeechRuntimeAvailabilityGuard(() => {
805+
throw new Error("speech runtime configured cold");
804806
});
805-
const result = await maybeApplyTtsToPayloadCore(
806-
{
807-
payload,
808-
cfg: createTtsConfig("openclaw-speech-cold-runtime-fallback-test"),
809-
channel: "slack",
810-
kind: "final",
811-
},
812-
async () => "unused",
813-
);
814-
expect(result.text).toBe("Spoken greeting");
815-
} finally {
816-
setSpeechRuntimeAvailabilityGuard(undefined);
817-
}
818-
});
807+
routedPayloads.length = 0;
808+
synthesizeMock.mockClear();
809+
try {
810+
const answer = "Spoken greeting";
811+
const channelData = structured
812+
? {
813+
slack: {
814+
blocks: [
815+
{ type: "section", text: { type: "mrkdwn", text: "Visible channel card" } },
816+
],
817+
},
818+
}
819+
: { slack: { unfurl: false } };
820+
const cfg = createTtsConfig(`openclaw-speech-cold-runtime-${loaded}-${structured}`);
821+
const routingResults: Array<Awaited<ReturnType<typeof routeReply>>> = [];
822+
const dispatcher = createReplyDispatcher({
823+
beforeDeliver: (payload, info) =>
824+
maybeApplyTtsToPayloadCore(
825+
{ payload, cfg, channel: "slack", kind: info.kind },
826+
async () => "unused",
827+
),
828+
deliver: async (payload, info) => {
829+
routingResults.push(
830+
await routeReply({
831+
payload,
832+
channel: "slack",
833+
to: "channel:C123",
834+
cfg,
835+
replyKind: info.kind,
836+
}),
837+
);
838+
},
839+
});
840+
841+
expect(
842+
dispatcher.sendFinalReply(
843+
setReplyPayloadMetadata(
844+
{ text: "", channelData },
845+
{ ttsExplicit: true, tts: { tagged: true, text: answer } },
846+
),
847+
),
848+
).toBe(true);
849+
dispatcher.markComplete();
850+
await dispatcher.waitForIdle();
851+
852+
expect(routingResults).toEqual([{ ok: true, delivered: true, messageId: "tts-route-1" }]);
853+
expect(routedPayloads).toHaveLength(1);
854+
expect(routedPayloads[0]).toMatchObject({
855+
text: structured ? "" : answer,
856+
channelData,
857+
});
858+
expect(synthesizeMock).not.toHaveBeenCalled();
859+
} finally {
860+
setSpeechRuntimeAvailabilityGuard(undefined);
861+
restoreRegistry();
862+
routedPayloads.length = 0;
863+
}
864+
},
865+
);
819866

820867
it("keeps payloads with visible text unchanged when the runtime is unavailable", async () => {
821868
const { setSpeechRuntimeAvailabilityGuard } = await import("./runtime-availability.js");

0 commit comments

Comments
 (0)