From 971d4b3c12da889bac10b122a81716b394796584 Mon Sep 17 00:00:00 2001 From: mattie ruth backman Date: Fri, 9 Jan 2026 10:44:33 -0500 Subject: [PATCH] Improved and introduced error handling for too-large messages --- transports/daily/src/transport.ts | 13 ++++++++++++- .../src/smallWebRTCTransport.ts | 19 +++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/transports/daily/src/transport.ts b/transports/daily/src/transport.ts index 2dd8d43..760e82e 100644 --- a/transports/daily/src/transport.ts +++ b/transports/daily/src/transport.ts @@ -20,6 +20,7 @@ import Daily, { import { DeviceArray, DeviceError, + MessageTooLargeError, Participant, PipecatClientOptions, RTVIError, @@ -597,7 +598,17 @@ export class DailyTransport extends Transport { } public sendMessage(message: RTVIMessage) { - this._daily.sendAppMessage(message, "*"); + try { + this._daily.sendAppMessage(message, "*"); + } catch (error: unknown) { + if ( + error instanceof Error && + error.message.includes("Message data too large") + ) { + throw new MessageTooLargeError(error.message); + } + throw error; + } } private handleAppMessage(ev: DailyEventObjectAppMessage) { diff --git a/transports/small-webrtc-transport/src/smallWebRTCTransport.ts b/transports/small-webrtc-transport/src/smallWebRTCTransport.ts index d2f3bdb..65e95e1 100644 --- a/transports/small-webrtc-transport/src/smallWebRTCTransport.ts +++ b/transports/small-webrtc-transport/src/smallWebRTCTransport.ts @@ -1,7 +1,10 @@ import cloneDeep from "lodash/cloneDeep"; import { + APIRequest, + isAPIRequest, logger, makeRequest, + MessageTooLargeError, RTVIError, RTVIMessage, PipecatClientOptions, @@ -10,8 +13,6 @@ import { TransportStartError, TransportState, UnsupportedFeatureError, - APIRequest, - isAPIRequest, } from "@pipecat-ai/client-js"; import { MediaManager } from "../../../lib/media-mgmt/mediaManager"; import { DailyMediaManager } from "../../../lib/media-mgmt/dailyMediaManager"; @@ -422,6 +423,20 @@ export class SmallWebRTCTransport extends Transport { logger.warn(`Datachannel is not ready. Message not sent: ${message}`); return; } + const getSizeInBytes = (obj: any) => { + const jsonString = JSON.stringify(obj); + const encoder = new TextEncoder(); + const bytes = encoder.encode(jsonString); + return bytes.length; + }; + + const objectSize = getSizeInBytes(message); + const maxSize = this.pc?.sctp?.maxMessageSize ?? 64 * 1024; + if (objectSize > maxSize) { + throw new MessageTooLargeError( + "Message data too large. Max size is " + maxSize, + ); + } this.dc?.send(JSON.stringify(message)); }