Skip to content

Commit 1ca4103

Browse files
authored
fix(voice-call): expose realtime/streaming stream paths through tailscale serve/funnel (#125468)
* fix(voice-call): expose realtime/streaming stream paths through tailscale serve/funnel Tailscale serve/funnel now auto-exposes the realtime and streaming WebSocket stream paths when those audio modes are enabled; previously Twilio <Connect><Stream> could not reach wss://<ts-host><streamPath> and realtime calls dropped after ~1s with no log. Also: CLI voicecall expose mounts/clears stream paths symmetrically, partial tailscale mounts roll back on failure, and a warning now fires when a call's stream WebSocket never arrives within the token TTL. Reported/observed live by Peter Steinberger. * fix(voice-call): make tailscale stream exposure atomic Preserve configured public Tailscale prefixes when mapping realtime and streaming routes, and roll back the full route set when any mount fails. The CLI now uses the same transactional setup path and reports failure instead of accepting a partial exposure. * fix(voice-call): expose Twilio's configured streaming path Keep realtime routes under the public webhook prefix they advertise, while mounting streaming.streamPath exactly as Twilio emits it. This preserves the canonical public-to-local route list without expanding the provider API outside the scoped repair.
1 parent fab7fa9 commit 1ca4103

14 files changed

Lines changed: 648 additions & 58 deletions

‎docs/cli/voicecall.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ JSON with `recordsScanned`, `turnLatency`, and `listenWait` summaries.
187187
### `expose`
188188

189189
Enable, disable, or change the Tailscale serve/funnel configuration for the
190-
voice webhook.
190+
voice webhook. When realtime or streaming audio is enabled, the command also
191+
exposes or clears that mode's WebSocket stream path.
191192

192193
| Flag | Default | Description |
193194
| --------------------- | ----------------------------------------- | ----------------------------------------------- |

‎docs/plugins/voice-call.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ that Region. See
208208
- On ngrok free tier, set `publicUrl` to the exact ngrok URL; signature verification is always enforced.
209209
- `tunnel.allowNgrokFreeTierLoopbackBypass: true` allows Twilio webhooks with invalid signatures **only** when `tunnel.provider="ngrok"` and `serve.bind` is loopback (ngrok local agent). Local dev only.
210210
- Ngrok free-tier URLs can change or add interstitial behavior; if `publicUrl` drifts, Twilio signatures fail. Production: prefer a stable domain or a Tailscale funnel.
211+
- Tailscale Serve and Funnel automatically expose the realtime or streaming WebSocket path when that audio mode is enabled.
211212

212213
</Accordion>
213214
<Accordion title="Streaming connection caps">

‎extensions/voice-call/src/cli.test.ts‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import { afterEach, describe, expect, it, vi } from "vitest";
88
const callGatewayFromCliMock = vi.hoisted(() => vi.fn());
99
const findCallMatchesInStoreMock = vi.hoisted(() => vi.fn());
1010
const loadActiveCallsFromStoreMock = vi.hoisted(() => vi.fn());
11+
const tailscaleMocks = vi.hoisted(() => ({
12+
cleanup: vi.fn(),
13+
getSelfInfo: vi.fn(),
14+
setup: vi.fn(),
15+
}));
1116
const sleepMock = vi.hoisted(() =>
1217
vi.fn(
1318
async (ms: number) =>
@@ -30,6 +35,12 @@ vi.mock("./manager/store.js", async (importOriginal) => ({
3035
findCallMatchesInStore: findCallMatchesInStoreMock,
3136
loadActiveCallsFromStore: loadActiveCallsFromStoreMock,
3237
}));
38+
vi.mock("./webhook/tailscale.js", async (importOriginal) => ({
39+
...(await importOriginal<typeof import("./webhook/tailscale.js")>()),
40+
cleanupTailscaleExposureRoute: tailscaleMocks.cleanup,
41+
getTailscaleSelfInfo: tailscaleMocks.getSelfInfo,
42+
setupTailscaleExposureRoutes: tailscaleMocks.setup,
43+
}));
3344

3445
import { registerVoiceCallCli } from "./cli.js";
3546

@@ -50,6 +61,9 @@ describe("voice-call CLI status fallback", () => {
5061
callGatewayFromCliMock.mockReset();
5162
findCallMatchesInStoreMock.mockReset();
5263
loadActiveCallsFromStoreMock.mockReset();
64+
tailscaleMocks.cleanup.mockReset();
65+
tailscaleMocks.getSelfInfo.mockReset();
66+
tailscaleMocks.setup.mockReset();
5367
sleepMock.mockReset();
5468
sleepMock.mockImplementation(
5569
async (ms: number) =>
@@ -151,6 +165,115 @@ describe("voice-call CLI status fallback", () => {
151165
).rejects.toThrow("Invalid numeric value for --since: 0x10");
152166
});
153167

168+
it("exposes the webhook target and enabled stream paths", async () => {
169+
tailscaleMocks.setup.mockResolvedValue("https://bot.example.ts.net/voice/webhook");
170+
const program = buildProgram(
171+
{},
172+
{
173+
serve: { port: 3334, path: "/voice/webhook" },
174+
tailscale: { mode: "off", path: "/voice/webhook" },
175+
realtime: { enabled: true, streamPath: "/voice/stream/realtime" },
176+
streaming: { enabled: true, streamPath: "/voice/stream" },
177+
},
178+
);
179+
const capturer = captureStdout();
180+
try {
181+
await program.parseAsync(
182+
[
183+
"voicecall",
184+
"expose",
185+
"--mode",
186+
"funnel",
187+
"--port",
188+
"4444",
189+
"--path",
190+
"/edge/custom/webhook",
191+
"--serve-path",
192+
"/custom/webhook",
193+
],
194+
{ from: "user" },
195+
);
196+
} finally {
197+
capturer.restore();
198+
}
199+
200+
expect(tailscaleMocks.setup).toHaveBeenCalledWith({
201+
mode: "funnel",
202+
routes: [
203+
{
204+
path: "/edge/custom/webhook",
205+
localUrl: "http://127.0.0.1:4444/custom/webhook",
206+
},
207+
{
208+
path: "/edge/voice/stream/realtime",
209+
localUrl: "http://127.0.0.1:4444/voice/stream/realtime",
210+
},
211+
{
212+
path: "/voice/stream",
213+
localUrl: "http://127.0.0.1:4444/voice/stream",
214+
},
215+
],
216+
});
217+
expect(JSON.parse(capturer.output())).toMatchObject({
218+
localUrl: "http://127.0.0.1:4444/custom/webhook",
219+
streamPaths: ["/edge/voice/stream/realtime", "/voice/stream"],
220+
});
221+
});
222+
223+
it("reports failure when any exposure route cannot be mounted", async () => {
224+
tailscaleMocks.setup.mockResolvedValue(null);
225+
tailscaleMocks.getSelfInfo.mockResolvedValue(null);
226+
const program = buildProgram(
227+
{},
228+
{
229+
serve: { port: 3334, path: "/voice/webhook" },
230+
tailscale: { mode: "off", path: "/voice/webhook" },
231+
realtime: { enabled: true, streamPath: "/voice/stream/realtime" },
232+
streaming: { enabled: false },
233+
},
234+
);
235+
const capturer = captureStdout();
236+
try {
237+
await program.parseAsync(["voicecall", "expose", "--mode", "funnel"], { from: "user" });
238+
} finally {
239+
capturer.restore();
240+
}
241+
242+
expect(JSON.parse(capturer.output())).toMatchObject({
243+
ok: false,
244+
publicUrl: null,
245+
});
246+
});
247+
248+
it("clears webhook and stream paths for both Tailscale modes", async () => {
249+
const program = buildProgram(
250+
{},
251+
{
252+
serve: { port: 3334, path: "/voice/webhook" },
253+
tailscale: { mode: "off", path: "/voice/webhook" },
254+
realtime: { enabled: true, streamPath: "/voice/stream/realtime" },
255+
streaming: { enabled: true, streamPath: "/voice/stream" },
256+
},
257+
);
258+
const capturer = captureStdout();
259+
try {
260+
await program.parseAsync(["voicecall", "expose", "--mode", "off"], { from: "user" });
261+
} finally {
262+
capturer.restore();
263+
}
264+
265+
expect(tailscaleMocks.cleanup.mock.calls).toEqual(
266+
["/voice/webhook", "/voice/stream/realtime", "/voice/stream"].flatMap((exposurePath) => [
267+
[{ mode: "serve", path: exposurePath }],
268+
[{ mode: "funnel", path: exposurePath }],
269+
]),
270+
);
271+
expect(JSON.parse(capturer.output())).toMatchObject({
272+
mode: "off",
273+
streamPaths: ["/voice/stream/realtime", "/voice/stream"],
274+
});
275+
});
276+
154277
async function runCustomLogTailShortRead(
155278
appended: string | Buffer,
156279
firstReadBytes?: number,

‎extensions/voice-call/src/cli.ts‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import {
1818
normalizeOptionalLowercaseString,
1919
} from "openclaw/plugin-sdk/string-coerce-runtime";
2020
import { sleep } from "../api.js";
21-
import { validateProviderConfig, type VoiceCallConfig } from "./config.js";
21+
import {
22+
resolveVoiceCallStreamExposurePaths,
23+
validateProviderConfig,
24+
type VoiceCallConfig,
25+
} from "./config.js";
2226
import {
2327
findCallMatchesInStore,
2428
getCallHistoryFromStore,
@@ -32,7 +36,7 @@ import { resolveWebhookExposureStatus } from "./webhook-exposure.js";
3236
import {
3337
cleanupTailscaleExposureRoute,
3438
getTailscaleSelfInfo,
35-
setupTailscaleExposureRoute,
39+
setupTailscaleExposureRoutes,
3640
} from "./webhook/tailscale.js";
3741

3842
type Logger = {
@@ -893,20 +897,31 @@ export function registerVoiceCallCli(params: {
893897
);
894898
const servePath = options.servePath ?? config.serve.path ?? "/voice/webhook";
895899
const tsPath = options.path ?? config.tailscale?.path ?? servePath;
896-
897-
const localUrl = `http://127.0.0.1:${servePort}`;
900+
const streamExposurePaths = resolveVoiceCallStreamExposurePaths(config, {
901+
publicWebhookPath: tsPath,
902+
localWebhookPath: servePath,
903+
});
904+
const streamPaths = streamExposurePaths.map(({ publicPath }) => publicPath);
905+
const localUrl = `http://127.0.0.1:${servePort}${servePath}`;
898906

899907
if (mode === "off") {
900-
await cleanupTailscaleExposureRoute({ mode: "serve", path: tsPath });
901-
await cleanupTailscaleExposureRoute({ mode: "funnel", path: tsPath });
902-
writeStdoutJson({ ok: true, mode: "off", path: tsPath });
908+
for (const exposurePath of [tsPath, ...streamPaths]) {
909+
await cleanupTailscaleExposureRoute({ mode: "serve", path: exposurePath });
910+
await cleanupTailscaleExposureRoute({ mode: "funnel", path: exposurePath });
911+
}
912+
writeStdoutJson({ ok: true, mode: "off", path: tsPath, streamPaths });
903913
return;
904914
}
905915

906-
const publicUrl = await setupTailscaleExposureRoute({
916+
const publicUrl = await setupTailscaleExposureRoutes({
907917
mode,
908-
path: tsPath,
909-
localUrl,
918+
routes: [
919+
{ path: tsPath, localUrl },
920+
...streamExposurePaths.map(({ publicPath, localPath }) => ({
921+
path: publicPath,
922+
localUrl: `http://127.0.0.1:${servePort}${localPath}`,
923+
})),
924+
],
910925
});
911926

912927
const tsInfo = publicUrl ? null : await getTailscaleSelfInfo();
@@ -918,6 +933,7 @@ export function registerVoiceCallCli(params: {
918933
ok: Boolean(publicUrl),
919934
mode,
920935
path: tsPath,
936+
streamPaths,
921937
localUrl,
922938
publicUrl,
923939
hint: publicUrl

‎extensions/voice-call/src/config.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
resolveVoiceCallEffectiveConfig,
77
resolveVoiceCallNumberRouteKeyForCall,
88
resolveVoiceCallSessionKey,
9+
resolveVoiceCallStreamExposurePaths,
910
validateProviderConfig,
1011
normalizeVoiceCallConfig,
1112
resolveVoiceCallConfig,
@@ -828,6 +829,68 @@ describe("normalizeVoiceCallConfig", () => {
828829
});
829830
});
830831

832+
describe("resolveVoiceCallStreamExposurePaths", () => {
833+
it("returns no paths when both audio modes are disabled", () => {
834+
const config = normalizeVoiceCallConfig({});
835+
836+
expect(resolveVoiceCallStreamExposurePaths(config)).toEqual([]);
837+
});
838+
839+
it("derives the default realtime path from the webhook path", () => {
840+
const config = normalizeVoiceCallConfig({
841+
serve: { path: "/custom/webhook" },
842+
realtime: { enabled: true },
843+
});
844+
845+
expect(resolveVoiceCallStreamExposurePaths(config)).toEqual([
846+
{
847+
localPath: "/custom/stream/realtime",
848+
publicPath: "/custom/stream/realtime",
849+
},
850+
]);
851+
});
852+
853+
it("normalizes explicit realtime and streaming paths", () => {
854+
const config = normalizeVoiceCallConfig({
855+
realtime: { enabled: true, streamPath: "custom/realtime" },
856+
streaming: { enabled: true, streamPath: "custom/stream" },
857+
});
858+
859+
expect(resolveVoiceCallStreamExposurePaths(config)).toEqual([
860+
{ localPath: "/custom/realtime", publicPath: "/custom/realtime" },
861+
{ localPath: "/custom/stream", publicPath: "/custom/stream" },
862+
]);
863+
});
864+
865+
it("deduplicates equal realtime and streaming paths", () => {
866+
const config = normalizeVoiceCallConfig({
867+
realtime: { enabled: true, streamPath: "/voice/stream" },
868+
streaming: { enabled: true, streamPath: "/voice/stream" },
869+
});
870+
871+
expect(resolveVoiceCallStreamExposurePaths(config)).toEqual([
872+
{ localPath: "/voice/stream", publicPath: "/voice/stream" },
873+
]);
874+
});
875+
876+
it("maps stream paths through a distinct public Tailscale prefix", () => {
877+
const config = normalizeVoiceCallConfig({
878+
serve: { path: "/voice/webhook" },
879+
tailscale: { path: "/edge/voice/webhook" },
880+
realtime: { enabled: true },
881+
streaming: { enabled: true, streamPath: "/voice/stream" },
882+
});
883+
884+
expect(resolveVoiceCallStreamExposurePaths(config)).toEqual([
885+
{
886+
localPath: "/voice/stream/realtime",
887+
publicPath: "/edge/voice/stream/realtime",
888+
},
889+
{ localPath: "/voice/stream", publicPath: "/voice/stream" },
890+
]);
891+
});
892+
});
893+
831894
describe("resolveVoiceCallConfig realtime settings", () => {
832895
it("preserves configured realtime instructions without env indirection", () => {
833896
const resolved = resolveVoiceCallConfig({

‎extensions/voice-call/src/config.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,48 @@ function defaultRealtimeStreamPathForServePath(servePath: string): string {
539539
return `${normalized}/stream/realtime`;
540540
}
541541

542+
export type VoiceCallStreamExposurePath = {
543+
publicPath: string;
544+
localPath: string;
545+
};
546+
547+
export function resolveVoiceCallPublicPathPrefix(
548+
publicWebhookPath: string,
549+
localWebhookPath: string,
550+
): string {
551+
const publicPath = normalizeWebhookPath(publicWebhookPath);
552+
const localPathIndex = publicPath.indexOf(normalizeWebhookPath(localWebhookPath));
553+
return localPathIndex > 0 ? publicPath.slice(0, localPathIndex) : "";
554+
}
555+
556+
export function resolveVoiceCallStreamExposurePaths(
557+
config: VoiceCallConfig,
558+
webhookPaths: { publicWebhookPath?: string; localWebhookPath?: string } = {},
559+
): VoiceCallStreamExposurePath[] {
560+
const exposurePaths: VoiceCallStreamExposurePath[] = [];
561+
const localWebhookPath = webhookPaths.localWebhookPath ?? config.serve.path;
562+
const publicWebhookPath = webhookPaths.publicWebhookPath ?? config.tailscale.path;
563+
const publicPathPrefix = resolveVoiceCallPublicPathPrefix(publicWebhookPath, localWebhookPath);
564+
if (config.realtime.enabled) {
565+
const localPath = normalizeWebhookPath(
566+
config.realtime.streamPath ?? defaultRealtimeStreamPathForServePath(config.serve.path),
567+
);
568+
exposurePaths.push({
569+
localPath,
570+
publicPath: `${publicPathPrefix}${localPath}`,
571+
});
572+
}
573+
if (config.streaming.enabled) {
574+
const localPath = normalizeWebhookPath(config.streaming.streamPath);
575+
if (
576+
!exposurePaths.some((path) => path.localPath === localPath && path.publicPath === localPath)
577+
) {
578+
exposurePaths.push({ localPath, publicPath: localPath });
579+
}
580+
}
581+
return exposurePaths;
582+
}
583+
542584
function normalizeVoiceCallTtsConfig(
543585
defaults: VoiceCallTtsConfig,
544586
overrides: DeepPartial<NonNullable<VoiceCallTtsConfig>> | undefined,

0 commit comments

Comments
 (0)