openai 7.13.0 → 7.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/client.d.mts +3 -0
- package/client.d.mts.map +1 -1
- package/client.d.ts +3 -0
- package/client.d.ts.map +1 -1
- package/client.js +3 -0
- package/client.js.map +1 -1
- package/client.mjs +3 -0
- package/client.mjs.map +1 -1
- package/helpers/live.d.mts +2 -0
- package/helpers/live.d.mts.map +1 -0
- package/helpers/live.d.ts +2 -0
- package/helpers/live.d.ts.map +1 -0
- package/helpers/live.js +6 -0
- package/helpers/live.js.map +1 -0
- package/helpers/live.mjs +2 -0
- package/helpers/live.mjs.map +1 -0
- package/lib/live/transcript-grouper.d.mts +96 -0
- package/lib/live/transcript-grouper.d.mts.map +1 -0
- package/lib/live/transcript-grouper.d.ts +96 -0
- package/lib/live/transcript-grouper.d.ts.map +1 -0
- package/lib/live/transcript-grouper.js +223 -0
- package/lib/live/transcript-grouper.js.map +1 -0
- package/lib/live/transcript-grouper.mjs +219 -0
- package/lib/live/transcript-grouper.mjs.map +1 -0
- package/lib/live/transcript-grouping.d.mts +49 -0
- package/lib/live/transcript-grouping.d.mts.map +1 -0
- package/lib/live/transcript-grouping.d.ts +49 -0
- package/lib/live/transcript-grouping.d.ts.map +1 -0
- package/lib/live/transcript-grouping.js +287 -0
- package/lib/live/transcript-grouping.js.map +1 -0
- package/lib/live/transcript-grouping.mjs +283 -0
- package/lib/live/transcript-grouping.mjs.map +1 -0
- package/lib/live/webrtc.d.mts +13 -0
- package/lib/live/webrtc.d.mts.map +1 -0
- package/lib/live/webrtc.d.ts +13 -0
- package/lib/live/webrtc.d.ts.map +1 -0
- package/lib/live/webrtc.js +16 -0
- package/lib/live/webrtc.js.map +1 -0
- package/lib/live/webrtc.mjs +10 -0
- package/lib/live/webrtc.mjs.map +1 -0
- package/lib/realtime/webrtc.d.mts +13 -0
- package/lib/realtime/webrtc.d.mts.map +1 -0
- package/lib/realtime/webrtc.d.ts +13 -0
- package/lib/realtime/webrtc.d.ts.map +1 -0
- package/lib/realtime/webrtc.js +16 -0
- package/lib/realtime/webrtc.js.map +1 -0
- package/lib/realtime/webrtc.mjs +10 -0
- package/lib/realtime/webrtc.mjs.map +1 -0
- package/lib/webrtc/connection.d.mts +40 -0
- package/lib/webrtc/connection.d.mts.map +1 -0
- package/lib/webrtc/connection.d.ts +40 -0
- package/lib/webrtc/connection.d.ts.map +1 -0
- package/lib/webrtc/connection.js +228 -0
- package/lib/webrtc/connection.js.map +1 -0
- package/lib/webrtc/connection.mjs +224 -0
- package/lib/webrtc/connection.mjs.map +1 -0
- package/lib/webrtc/data-channel.d.mts +32 -0
- package/lib/webrtc/data-channel.d.mts.map +1 -0
- package/lib/webrtc/data-channel.d.ts +32 -0
- package/lib/webrtc/data-channel.d.ts.map +1 -0
- package/lib/webrtc/data-channel.js +125 -0
- package/lib/webrtc/data-channel.js.map +1 -0
- package/lib/webrtc/data-channel.mjs +121 -0
- package/lib/webrtc/data-channel.mjs.map +1 -0
- package/lib/webrtc/errors.d.mts +19 -0
- package/lib/webrtc/errors.d.mts.map +1 -0
- package/lib/webrtc/errors.d.ts +19 -0
- package/lib/webrtc/errors.d.ts.map +1 -0
- package/lib/webrtc/errors.js +18 -0
- package/lib/webrtc/errors.js.map +1 -0
- package/lib/webrtc/errors.mjs +14 -0
- package/lib/webrtc/errors.mjs.map +1 -0
- package/lib/webrtc/subscriptions.d.mts +13 -0
- package/lib/webrtc/subscriptions.d.mts.map +1 -0
- package/lib/webrtc/subscriptions.d.ts +13 -0
- package/lib/webrtc/subscriptions.d.ts.map +1 -0
- package/lib/webrtc/subscriptions.js +62 -0
- package/lib/webrtc/subscriptions.js.map +1 -0
- package/lib/webrtc/subscriptions.mjs +58 -0
- package/lib/webrtc/subscriptions.mjs.map +1 -0
- package/lib/webrtc/types.d.mts +99 -0
- package/lib/webrtc/types.d.mts.map +1 -0
- package/lib/webrtc/types.d.ts +99 -0
- package/lib/webrtc/types.d.ts.map +1 -0
- package/lib/webrtc/types.js +3 -0
- package/lib/webrtc/types.js.map +1 -0
- package/lib/webrtc/types.mjs +2 -0
- package/lib/webrtc/types.mjs.map +1 -0
- package/live/webrtc.d.mts +3 -0
- package/live/webrtc.d.mts.map +1 -0
- package/live/webrtc.d.ts +3 -0
- package/live/webrtc.d.ts.map +1 -0
- package/live/webrtc.js +8 -0
- package/live/webrtc.js.map +1 -0
- package/live/webrtc.mjs +2 -0
- package/live/webrtc.mjs.map +1 -0
- package/package.json +11 -1
- package/realtime/webrtc.d.mts +3 -0
- package/realtime/webrtc.d.mts.map +1 -0
- package/realtime/webrtc.d.ts +3 -0
- package/realtime/webrtc.d.ts.map +1 -0
- package/realtime/webrtc.js +8 -0
- package/realtime/webrtc.js.map +1 -0
- package/realtime/webrtc.mjs +2 -0
- package/realtime/webrtc.mjs.map +1 -0
- package/resources/beta/responses/responses.d.mts +48 -0
- package/resources/beta/responses/responses.d.mts.map +1 -1
- package/resources/beta/responses/responses.d.ts +48 -0
- package/resources/beta/responses/responses.d.ts.map +1 -1
- package/resources/beta/responses/responses.js.map +1 -1
- package/resources/beta/responses/responses.mjs.map +1 -1
- package/resources/conversations/items.d.mts +16 -0
- package/resources/conversations/items.d.mts.map +1 -1
- package/resources/conversations/items.d.ts +16 -0
- package/resources/conversations/items.d.ts.map +1 -1
- package/resources/index.d.mts +1 -0
- package/resources/index.d.mts.map +1 -1
- package/resources/index.d.ts +1 -0
- package/resources/index.d.ts.map +1 -1
- package/resources/index.js +3 -1
- package/resources/index.js.map +1 -1
- package/resources/index.mjs +1 -0
- package/resources/index.mjs.map +1 -1
- package/resources/live/forks/forks.d.mts +110 -0
- package/resources/live/forks/forks.d.mts.map +1 -0
- package/resources/live/forks/forks.d.ts +110 -0
- package/resources/live/forks/forks.d.ts.map +1 -0
- package/resources/live/forks/forks.js +9 -0
- package/resources/live/forks/forks.js.map +1 -0
- package/resources/live/forks/forks.mjs +5 -0
- package/resources/live/forks/forks.mjs.map +1 -0
- package/resources/live/forks/index.d.mts +4 -0
- package/resources/live/forks/index.d.mts.map +1 -0
- package/resources/live/forks/index.d.ts +4 -0
- package/resources/live/forks/index.d.ts.map +1 -0
- package/resources/live/forks/index.js +7 -0
- package/resources/live/forks/index.js.map +1 -0
- package/resources/live/forks/index.mjs +3 -0
- package/resources/live/forks/index.mjs.map +1 -0
- package/resources/live/forks/internal-base.d.mts +73 -0
- package/resources/live/forks/internal-base.d.mts.map +1 -0
- package/resources/live/forks/internal-base.d.ts +73 -0
- package/resources/live/forks/internal-base.d.ts.map +1 -0
- package/resources/live/forks/internal-base.js +49 -0
- package/resources/live/forks/internal-base.js.map +1 -0
- package/resources/live/forks/internal-base.mjs +43 -0
- package/resources/live/forks/internal-base.mjs.map +1 -0
- package/resources/live/forks/ws-base.d.mts +109 -0
- package/resources/live/forks/ws-base.d.mts.map +1 -0
- package/resources/live/forks/ws-base.d.ts +109 -0
- package/resources/live/forks/ws-base.d.ts.map +1 -0
- package/resources/live/forks/ws-base.js +476 -0
- package/resources/live/forks/ws-base.js.map +1 -0
- package/resources/live/forks/ws-base.mjs +472 -0
- package/resources/live/forks/ws-base.mjs.map +1 -0
- package/resources/live/forks/ws.d.mts +13 -0
- package/resources/live/forks/ws.d.mts.map +1 -0
- package/resources/live/forks/ws.d.ts +13 -0
- package/resources/live/forks/ws.d.ts.map +1 -0
- package/resources/live/forks/ws.js +34 -0
- package/resources/live/forks/ws.js.map +1 -0
- package/resources/live/forks/ws.mjs +29 -0
- package/resources/live/forks/ws.mjs.map +1 -0
- package/resources/live/forks.d.mts +2 -0
- package/resources/live/forks.d.mts.map +1 -0
- package/resources/live/forks.d.ts +2 -0
- package/resources/live/forks.d.ts.map +1 -0
- package/resources/live/forks.js +6 -0
- package/resources/live/forks.js.map +1 -0
- package/resources/live/forks.mjs +3 -0
- package/resources/live/forks.mjs.map +1 -0
- package/resources/live/index.d.mts +7 -0
- package/resources/live/index.d.mts.map +1 -0
- package/resources/live/index.d.ts +7 -0
- package/resources/live/index.d.ts.map +1 -0
- package/resources/live/index.js +13 -0
- package/resources/live/index.js.map +1 -0
- package/resources/live/index.mjs +6 -0
- package/resources/live/index.mjs.map +1 -0
- package/resources/live/internal-base.d.mts +71 -0
- package/resources/live/internal-base.d.mts.map +1 -0
- package/resources/live/internal-base.d.ts +71 -0
- package/resources/live/internal-base.d.ts.map +1 -0
- package/resources/live/internal-base.js +48 -0
- package/resources/live/internal-base.js.map +1 -0
- package/resources/live/internal-base.mjs +42 -0
- package/resources/live/internal-base.mjs.map +1 -0
- package/resources/live/live.d.mts +1864 -0
- package/resources/live/live.d.mts.map +1 -0
- package/resources/live/live.d.ts +1864 -0
- package/resources/live/live.d.ts.map +1 -0
- package/resources/live/live.js +40 -0
- package/resources/live/live.js.map +1 -0
- package/resources/live/live.mjs +35 -0
- package/resources/live/live.mjs.map +1 -0
- package/resources/live/sessions.d.mts +254 -0
- package/resources/live/sessions.d.mts.map +1 -0
- package/resources/live/sessions.d.ts +254 -0
- package/resources/live/sessions.d.ts.map +1 -0
- package/resources/live/sessions.js +118 -0
- package/resources/live/sessions.js.map +1 -0
- package/resources/live/sessions.mjs +114 -0
- package/resources/live/sessions.mjs.map +1 -0
- package/resources/live/sideband/index.d.mts +4 -0
- package/resources/live/sideband/index.d.mts.map +1 -0
- package/resources/live/sideband/index.d.ts +4 -0
- package/resources/live/sideband/index.d.ts.map +1 -0
- package/resources/live/sideband/index.js +7 -0
- package/resources/live/sideband/index.js.map +1 -0
- package/resources/live/sideband/index.mjs +3 -0
- package/resources/live/sideband/index.mjs.map +1 -0
- package/resources/live/sideband/internal-base.d.mts +73 -0
- package/resources/live/sideband/internal-base.d.mts.map +1 -0
- package/resources/live/sideband/internal-base.d.ts +73 -0
- package/resources/live/sideband/internal-base.d.ts.map +1 -0
- package/resources/live/sideband/internal-base.js +49 -0
- package/resources/live/sideband/internal-base.js.map +1 -0
- package/resources/live/sideband/internal-base.mjs +43 -0
- package/resources/live/sideband/internal-base.mjs.map +1 -0
- package/resources/live/sideband/sideband.d.mts +25 -0
- package/resources/live/sideband/sideband.d.mts.map +1 -0
- package/resources/live/sideband/sideband.d.ts +25 -0
- package/resources/live/sideband/sideband.d.ts.map +1 -0
- package/resources/live/sideband/sideband.js +9 -0
- package/resources/live/sideband/sideband.js.map +1 -0
- package/resources/live/sideband/sideband.mjs +5 -0
- package/resources/live/sideband/sideband.mjs.map +1 -0
- package/resources/live/sideband/ws-base.d.mts +114 -0
- package/resources/live/sideband/ws-base.d.mts.map +1 -0
- package/resources/live/sideband/ws-base.d.ts +114 -0
- package/resources/live/sideband/ws-base.d.ts.map +1 -0
- package/resources/live/sideband/ws-base.js +476 -0
- package/resources/live/sideband/ws-base.js.map +1 -0
- package/resources/live/sideband/ws-base.mjs +472 -0
- package/resources/live/sideband/ws-base.mjs.map +1 -0
- package/resources/live/sideband/ws.d.mts +13 -0
- package/resources/live/sideband/ws.d.mts.map +1 -0
- package/resources/live/sideband/ws.d.ts +13 -0
- package/resources/live/sideband/ws.d.ts.map +1 -0
- package/resources/live/sideband/ws.js +34 -0
- package/resources/live/sideband/ws.js.map +1 -0
- package/resources/live/sideband/ws.mjs +29 -0
- package/resources/live/sideband/ws.mjs.map +1 -0
- package/resources/live/sideband.d.mts +2 -0
- package/resources/live/sideband.d.mts.map +1 -0
- package/resources/live/sideband.d.ts +2 -0
- package/resources/live/sideband.d.ts.map +1 -0
- package/resources/live/sideband.js +6 -0
- package/resources/live/sideband.js.map +1 -0
- package/resources/live/sideband.mjs +3 -0
- package/resources/live/sideband.mjs.map +1 -0
- package/resources/live/ws-base.d.mts +106 -0
- package/resources/live/ws-base.d.mts.map +1 -0
- package/resources/live/ws-base.d.ts +106 -0
- package/resources/live/ws-base.d.ts.map +1 -0
- package/resources/live/ws-base.js +476 -0
- package/resources/live/ws-base.js.map +1 -0
- package/resources/live/ws-base.mjs +472 -0
- package/resources/live/ws-base.mjs.map +1 -0
- package/resources/live/ws.d.mts +13 -0
- package/resources/live/ws.d.mts.map +1 -0
- package/resources/live/ws.d.ts +13 -0
- package/resources/live/ws.d.ts.map +1 -0
- package/resources/live/ws.js +34 -0
- package/resources/live/ws.js.map +1 -0
- package/resources/live/ws.mjs +29 -0
- package/resources/live/ws.mjs.map +1 -0
- package/resources/live.d.mts +2 -0
- package/resources/live.d.mts.map +1 -0
- package/resources/live.d.ts +2 -0
- package/resources/live.d.ts.map +1 -0
- package/resources/live.js +6 -0
- package/resources/live.js.map +1 -0
- package/resources/live.mjs +3 -0
- package/resources/live.mjs.map +1 -0
- package/resources/responses/responses.d.mts +48 -0
- package/resources/responses/responses.d.mts.map +1 -1
- package/resources/responses/responses.d.ts +48 -0
- package/resources/responses/responses.d.ts.map +1 -1
- package/resources/responses/responses.js.map +1 -1
- package/resources/responses/responses.mjs.map +1 -1
- package/resources/webhooks/webhooks.d.mts +72 -5
- package/resources/webhooks/webhooks.d.mts.map +1 -1
- package/resources/webhooks/webhooks.d.ts +72 -5
- package/resources/webhooks/webhooks.d.ts.map +1 -1
- package/src/client.ts +5 -0
- package/src/helpers/live.ts +8 -0
- package/src/lib/live/transcript-grouper.ts +303 -0
- package/src/lib/live/transcript-grouping.ts +362 -0
- package/src/lib/live/webrtc.ts +23 -0
- package/src/lib/realtime/webrtc.ts +20 -0
- package/src/lib/webrtc/connection.ts +309 -0
- package/src/lib/webrtc/data-channel.ts +144 -0
- package/src/lib/webrtc/errors.ts +38 -0
- package/src/lib/webrtc/subscriptions.ts +62 -0
- package/src/lib/webrtc/types.ts +109 -0
- package/src/live/webrtc.ts +10 -0
- package/src/realtime/webrtc.ts +10 -0
- package/src/resources/beta/responses/responses.ts +60 -0
- package/src/resources/conversations/items.ts +20 -0
- package/src/resources/index.ts +1 -0
- package/src/resources/live/api.md +87 -0
- package/src/resources/live/forks/forks.ts +169 -0
- package/src/resources/live/forks/index.ts +5 -0
- package/src/resources/live/forks/internal-base.ts +107 -0
- package/src/resources/live/forks/ws-base.ts +611 -0
- package/src/resources/live/forks/ws.ts +46 -0
- package/src/resources/live/forks.ts +3 -0
- package/src/resources/live/index.ts +20 -0
- package/src/resources/live/internal-base.ts +104 -0
- package/src/resources/live/live.ts +2296 -0
- package/src/resources/live/sessions.ts +337 -0
- package/src/resources/live/sideband/index.ts +10 -0
- package/src/resources/live/sideband/internal-base.ts +107 -0
- package/src/resources/live/sideband/sideband.ts +58 -0
- package/src/resources/live/sideband/ws-base.ts +621 -0
- package/src/resources/live/sideband/ws.ts +46 -0
- package/src/resources/live/sideband.ts +3 -0
- package/src/resources/live/ws-base.ts +607 -0
- package/src/resources/live/ws.ts +42 -0
- package/src/resources/live.ts +3 -0
- package/src/resources/responses/responses.ts +60 -0
- package/src/resources/webhooks/api.md +1 -0
- package/src/resources/webhooks/webhooks.ts +82 -3
- package/src/version.ts +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { WebRTCConnection } from "../webrtc/connection.mjs";
|
|
2
|
+
import { DataChannel } from "../webrtc/data-channel.mjs";
|
|
3
|
+
export { WebRTCError } from "../webrtc/errors.mjs";
|
|
4
|
+
/** Browser Live connection with application-owned SDP exchange and typed server-event subscriptions. */
|
|
5
|
+
export class OpenAILiveWebRTC extends WebRTCConnection {
|
|
6
|
+
}
|
|
7
|
+
/** Typed Live events on a borrowed data channel; dispose() never closes application resources. */
|
|
8
|
+
export class LiveDataChannel extends DataChannel {
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=webrtc.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webrtc.mjs","sourceRoot":"","sources":["../../src/lib/live/webrtc.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,gBAAgB,EAAE,iCAA6B;AACxD,OAAO,EAAE,WAAW,EAAE,mCAA+B;AAErD,OAAO,EAAE,WAAW,EAAE,6BAAyB;AAU/C,wGAAwG;AACxG,MAAM,OAAO,gBAAiB,SAAQ,gBAA0C;CAAG;AAEnF,kGAAkG;AAClG,MAAM,OAAO,eAAgB,SAAQ,WAAqC;CAAG"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { RealtimeClientEvent, RealtimeServerEvent } from "../../resources/realtime/realtime.mjs";
|
|
2
|
+
import { WebRTCConnection } from "../webrtc/connection.mjs";
|
|
3
|
+
import { DataChannel } from "../webrtc/data-channel.mjs";
|
|
4
|
+
export { WebRTCError } from "../webrtc/errors.mjs";
|
|
5
|
+
export type { WebRTCErrorCode, WebRTCErrorPhase } from "../webrtc/errors.mjs";
|
|
6
|
+
export type { ExchangeSdp, WebRTCConnectOptions, WebRTCConnectionEvent, WebRTCConfiguration, WebRTCState, } from "../webrtc/types.mjs";
|
|
7
|
+
/** Browser Realtime connection with application-owned SDP exchange and typed server-event subscriptions. */
|
|
8
|
+
export declare class OpenAIRealtimeWebRTC extends WebRTCConnection<RealtimeClientEvent, RealtimeServerEvent> {
|
|
9
|
+
}
|
|
10
|
+
/** Typed Realtime events on a borrowed data channel; dispose() never closes application resources. */
|
|
11
|
+
export declare class RealtimeDataChannel extends DataChannel<RealtimeClientEvent, RealtimeServerEvent> {
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=webrtc.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webrtc.d.mts","sourceRoot":"","sources":["../../src/lib/realtime/webrtc.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,8CAA0C;AAClG,OAAO,EAAE,gBAAgB,EAAE,iCAA6B;AACxD,OAAO,EAAE,WAAW,EAAE,mCAA+B;AAErD,OAAO,EAAE,WAAW,EAAE,6BAAyB;AAC/C,YAAY,EAAE,eAAe,EAAE,gBAAgB,EAAE,6BAAyB;AAC1E,YAAY,EACV,WAAW,EACX,oBAAoB,EACpB,qBAAqB,EACrB,mBAAmB,EACnB,WAAW,GACZ,4BAAwB;AAEzB,4GAA4G;AAC5G,qBAAa,oBAAqB,SAAQ,gBAAgB,CAAC,mBAAmB,EAAE,mBAAmB,CAAC;CAAG;AAEvG,sGAAsG;AACtG,qBAAa,mBAAoB,SAAQ,WAAW,CAAC,mBAAmB,EAAE,mBAAmB,CAAC;CAAG"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { RealtimeClientEvent, RealtimeServerEvent } from "../../resources/realtime/realtime.js";
|
|
2
|
+
import { WebRTCConnection } from "../webrtc/connection.js";
|
|
3
|
+
import { DataChannel } from "../webrtc/data-channel.js";
|
|
4
|
+
export { WebRTCError } from "../webrtc/errors.js";
|
|
5
|
+
export type { WebRTCErrorCode, WebRTCErrorPhase } from "../webrtc/errors.js";
|
|
6
|
+
export type { ExchangeSdp, WebRTCConnectOptions, WebRTCConnectionEvent, WebRTCConfiguration, WebRTCState, } from "../webrtc/types.js";
|
|
7
|
+
/** Browser Realtime connection with application-owned SDP exchange and typed server-event subscriptions. */
|
|
8
|
+
export declare class OpenAIRealtimeWebRTC extends WebRTCConnection<RealtimeClientEvent, RealtimeServerEvent> {
|
|
9
|
+
}
|
|
10
|
+
/** Typed Realtime events on a borrowed data channel; dispose() never closes application resources. */
|
|
11
|
+
export declare class RealtimeDataChannel extends DataChannel<RealtimeClientEvent, RealtimeServerEvent> {
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=webrtc.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webrtc.d.ts","sourceRoot":"","sources":["../../src/lib/realtime/webrtc.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,6CAA0C;AAClG,OAAO,EAAE,gBAAgB,EAAE,gCAA6B;AACxD,OAAO,EAAE,WAAW,EAAE,kCAA+B;AAErD,OAAO,EAAE,WAAW,EAAE,4BAAyB;AAC/C,YAAY,EAAE,eAAe,EAAE,gBAAgB,EAAE,4BAAyB;AAC1E,YAAY,EACV,WAAW,EACX,oBAAoB,EACpB,qBAAqB,EACrB,mBAAmB,EACnB,WAAW,GACZ,2BAAwB;AAEzB,4GAA4G;AAC5G,qBAAa,oBAAqB,SAAQ,gBAAgB,CAAC,mBAAmB,EAAE,mBAAmB,CAAC;CAAG;AAEvG,sGAAsG;AACtG,qBAAa,mBAAoB,SAAQ,WAAW,CAAC,mBAAmB,EAAE,mBAAmB,CAAC;CAAG"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RealtimeDataChannel = exports.OpenAIRealtimeWebRTC = exports.WebRTCError = void 0;
|
|
4
|
+
const connection_1 = require("../webrtc/connection.js");
|
|
5
|
+
const data_channel_1 = require("../webrtc/data-channel.js");
|
|
6
|
+
var errors_1 = require("../webrtc/errors.js");
|
|
7
|
+
Object.defineProperty(exports, "WebRTCError", { enumerable: true, get: function () { return errors_1.WebRTCError; } });
|
|
8
|
+
/** Browser Realtime connection with application-owned SDP exchange and typed server-event subscriptions. */
|
|
9
|
+
class OpenAIRealtimeWebRTC extends connection_1.WebRTCConnection {
|
|
10
|
+
}
|
|
11
|
+
exports.OpenAIRealtimeWebRTC = OpenAIRealtimeWebRTC;
|
|
12
|
+
/** Typed Realtime events on a borrowed data channel; dispose() never closes application resources. */
|
|
13
|
+
class RealtimeDataChannel extends data_channel_1.DataChannel {
|
|
14
|
+
}
|
|
15
|
+
exports.RealtimeDataChannel = RealtimeDataChannel;
|
|
16
|
+
//# sourceMappingURL=webrtc.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webrtc.js","sourceRoot":"","sources":["../../src/lib/realtime/webrtc.ts"],"names":[],"mappings":";;;AAEA,wDAAwD;AACxD,4DAAqD;AAErD,8CAA+C;AAAtC,qGAAA,WAAW,OAAA;AAUpB,4GAA4G;AAC5G,MAAa,oBAAqB,SAAQ,6BAA0D;CAAG;AAAvG,oDAAuG;AAEvG,sGAAsG;AACtG,MAAa,mBAAoB,SAAQ,0BAAqD;CAAG;AAAjG,kDAAiG"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { WebRTCConnection } from "../webrtc/connection.mjs";
|
|
2
|
+
import { DataChannel } from "../webrtc/data-channel.mjs";
|
|
3
|
+
export { WebRTCError } from "../webrtc/errors.mjs";
|
|
4
|
+
/** Browser Realtime connection with application-owned SDP exchange and typed server-event subscriptions. */
|
|
5
|
+
export class OpenAIRealtimeWebRTC extends WebRTCConnection {
|
|
6
|
+
}
|
|
7
|
+
/** Typed Realtime events on a borrowed data channel; dispose() never closes application resources. */
|
|
8
|
+
export class RealtimeDataChannel extends DataChannel {
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=webrtc.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webrtc.mjs","sourceRoot":"","sources":["../../src/lib/realtime/webrtc.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,iCAA6B;AACxD,OAAO,EAAE,WAAW,EAAE,mCAA+B;AAErD,OAAO,EAAE,WAAW,EAAE,6BAAyB;AAU/C,4GAA4G;AAC5G,MAAM,OAAO,oBAAqB,SAAQ,gBAA0D;CAAG;AAEvG,sGAAsG;AACtG,MAAM,OAAO,mBAAoB,SAAQ,WAAqD;CAAG"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { BrowserDataChannel, BrowserPeerConnection, WebRTCConfiguration, WebRTCConnectOptions, WebRTCConnectionEvent, WebRTCState } from "./types.mjs";
|
|
2
|
+
/** Browser-native WebRTC negotiation with application-owned SDP signaling. */
|
|
3
|
+
export declare class WebRTCConnection<ClientEvent, ServerEvent extends {
|
|
4
|
+
type: string;
|
|
5
|
+
}> {
|
|
6
|
+
/** Native peer connection; configure application-owned media before connect(). */
|
|
7
|
+
readonly peerConnection: BrowserPeerConnection;
|
|
8
|
+
/** Native ordered, reliable channel carrying protocol events. */
|
|
9
|
+
readonly dataChannel: BrowserDataChannel;
|
|
10
|
+
private readonly peer;
|
|
11
|
+
private readonly adapter;
|
|
12
|
+
private readonly connectionEvents;
|
|
13
|
+
private currentState;
|
|
14
|
+
private attempt;
|
|
15
|
+
/** Creates native resources, but does not request media, contact a server, or begin negotiation. */
|
|
16
|
+
constructor(configuration?: WebRTCConfiguration);
|
|
17
|
+
/** Current state, including before subscriptions are registered; notifications are not replayed. */
|
|
18
|
+
get state(): WebRTCState;
|
|
19
|
+
/** Subscribes to one server event type with its generated payload; returns an independent unsubscribe function. */
|
|
20
|
+
on<Type extends ServerEvent['type']>(type: Type, handler: (event: Extract<ServerEvent, {
|
|
21
|
+
type: Type;
|
|
22
|
+
}>) => unknown): () => void;
|
|
23
|
+
/** Subscribes before connecting so initial server events are observable. API errors remain protocol data. */
|
|
24
|
+
onEvent(handler: (event: ServerEvent) => unknown): () => void;
|
|
25
|
+
/** Subscribes to local diagnostics and lifecycle. Setup failures reject connect() instead of emitting errors here. */
|
|
26
|
+
onConnectionEvent(handler: (event: WebRTCConnectionEvent) => unknown): () => void;
|
|
27
|
+
/** Sends without buffering; returning means local submission, not server acceptance. */
|
|
28
|
+
send(event: ClientEvent): void;
|
|
29
|
+
/** Negotiates once and waits for both native connection and data-channel readiness. Defaults to a 30-second deadline. */
|
|
30
|
+
connect(options: WebRTCConnectOptions): Promise<void>;
|
|
31
|
+
/** Idempotently closes owned native resources. It never stops application-supplied media tracks. */
|
|
32
|
+
close(): void;
|
|
33
|
+
private negotiate;
|
|
34
|
+
private checkReady;
|
|
35
|
+
private failSetup;
|
|
36
|
+
private finish;
|
|
37
|
+
private readonly onPeerState;
|
|
38
|
+
private readonly onChannelEvent;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=connection.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.d.mts","sourceRoot":"","sources":["../../src/lib/webrtc/connection.ts"],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AAC;IA2DrD,oGAAoG;IACpG,KAAK,IAAI,IAAI;YAWC,SAAS;IA8CvB,OAAO,CAAC,UAAU;IAgBlB,OAAO,CAAC,SAAS;IAajB,OAAO,CAAC,MAAM;IAed,OAAO,CAAC,QAAQ,CAAC,WAAW,CAmB1B;IAEF,OAAO,CAAC,QAAQ,CAAC,cAAc,CA0B7B;CACH"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { BrowserDataChannel, BrowserPeerConnection, WebRTCConfiguration, WebRTCConnectOptions, WebRTCConnectionEvent, WebRTCState } from "./types.js";
|
|
2
|
+
/** Browser-native WebRTC negotiation with application-owned SDP signaling. */
|
|
3
|
+
export declare class WebRTCConnection<ClientEvent, ServerEvent extends {
|
|
4
|
+
type: string;
|
|
5
|
+
}> {
|
|
6
|
+
/** Native peer connection; configure application-owned media before connect(). */
|
|
7
|
+
readonly peerConnection: BrowserPeerConnection;
|
|
8
|
+
/** Native ordered, reliable channel carrying protocol events. */
|
|
9
|
+
readonly dataChannel: BrowserDataChannel;
|
|
10
|
+
private readonly peer;
|
|
11
|
+
private readonly adapter;
|
|
12
|
+
private readonly connectionEvents;
|
|
13
|
+
private currentState;
|
|
14
|
+
private attempt;
|
|
15
|
+
/** Creates native resources, but does not request media, contact a server, or begin negotiation. */
|
|
16
|
+
constructor(configuration?: WebRTCConfiguration);
|
|
17
|
+
/** Current state, including before subscriptions are registered; notifications are not replayed. */
|
|
18
|
+
get state(): WebRTCState;
|
|
19
|
+
/** Subscribes to one server event type with its generated payload; returns an independent unsubscribe function. */
|
|
20
|
+
on<Type extends ServerEvent['type']>(type: Type, handler: (event: Extract<ServerEvent, {
|
|
21
|
+
type: Type;
|
|
22
|
+
}>) => unknown): () => void;
|
|
23
|
+
/** Subscribes before connecting so initial server events are observable. API errors remain protocol data. */
|
|
24
|
+
onEvent(handler: (event: ServerEvent) => unknown): () => void;
|
|
25
|
+
/** Subscribes to local diagnostics and lifecycle. Setup failures reject connect() instead of emitting errors here. */
|
|
26
|
+
onConnectionEvent(handler: (event: WebRTCConnectionEvent) => unknown): () => void;
|
|
27
|
+
/** Sends without buffering; returning means local submission, not server acceptance. */
|
|
28
|
+
send(event: ClientEvent): void;
|
|
29
|
+
/** Negotiates once and waits for both native connection and data-channel readiness. Defaults to a 30-second deadline. */
|
|
30
|
+
connect(options: WebRTCConnectOptions): Promise<void>;
|
|
31
|
+
/** Idempotently closes owned native resources. It never stops application-supplied media tracks. */
|
|
32
|
+
close(): void;
|
|
33
|
+
private negotiate;
|
|
34
|
+
private checkReady;
|
|
35
|
+
private failSetup;
|
|
36
|
+
private finish;
|
|
37
|
+
private readonly onPeerState;
|
|
38
|
+
private readonly onChannelEvent;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=connection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/lib/webrtc/connection.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,kBAAkB,EAClB,qBAAqB,EACrB,mBAAmB,EACnB,oBAAoB,EACpB,qBAAqB,EAGrB,WAAW,EACZ,mBAAgB;AAWjB,8EAA8E;AAC9E,qBAAa,gBAAgB,CAAC,WAAW,EAAE,WAAW,SAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE;IAC7E,kFAAkF;IAClF,QAAQ,CAAC,cAAc,EAAE,qBAAqB,CAAC;IAC/C,iEAAiE;IACjE,QAAQ,CAAC,WAAW,EAAE,kBAAkB,CAAC;IACzC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAuB;IAC5C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAwC;IAChE,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAA8C;IAC/E,OAAO,CAAC,YAAY,CAAsB;IAC1C,OAAO,CAAC,OAAO,CAAsB;IAErC,oGAAoG;gBACxF,aAAa,CAAC,EAAE,mBAAmB;IA8B/C,oGAAoG;IACpG,IAAI,KAAK,IAAI,WAAW,CAEvB;IAED,mHAAmH;IACnH,EAAE,CAAC,IAAI,SAAS,WAAW,CAAC,MAAM,CAAC,EACjC,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,WAAW,EAAE;QAAE,IAAI,EAAE,IAAI,CAAA;KAAE,CAAC,KAAK,OAAO,GAChE,MAAM,IAAI;IAIb,6GAA6G;IAC7G,OAAO,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,OAAO,GAAG,MAAM,IAAI;IAI7D,sHAAsH;IACtH,iBAAiB,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,qBAAqB,KAAK,OAAO,GAAG,MAAM,IAAI;IAOjF,wFAAwF;IACxF,IAAI,CAAC,KAAK,EAAE,WAAW,GAAG,IAAI;IAI9B,yHAAyH;IACzH,OAAO,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,IAAI,CAAC;IA2DrD,oGAAoG;IACpG,KAAK,IAAI,IAAI;YAWC,SAAS;IA8CvB,OAAO,CAAC,UAAU;IAgBlB,OAAO,CAAC,SAAS;IAajB,OAAO,CAAC,MAAM;IAed,OAAO,CAAC,QAAQ,CAAC,WAAW,CAmB1B;IAEF,OAAO,CAAC,QAAQ,CAAC,cAAc,CA0B7B;CACH"}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WebRTCConnection = void 0;
|
|
4
|
+
const data_channel_1 = require("./data-channel.js");
|
|
5
|
+
const errors_1 = require("./errors.js");
|
|
6
|
+
const subscriptions_1 = require("./subscriptions.js");
|
|
7
|
+
/** Browser-native WebRTC negotiation with application-owned SDP signaling. */
|
|
8
|
+
class WebRTCConnection {
|
|
9
|
+
/** Creates native resources, but does not request media, contact a server, or begin negotiation. */
|
|
10
|
+
constructor(configuration) {
|
|
11
|
+
this.connectionEvents = new subscriptions_1.Subscriptions();
|
|
12
|
+
this.currentState = 'new';
|
|
13
|
+
this.onPeerState = () => {
|
|
14
|
+
if (this.currentState === 'closed') {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (this.peer.connectionState === 'failed' || this.peer.connectionState === 'closed') {
|
|
18
|
+
const failed = this.peer.connectionState === 'failed';
|
|
19
|
+
const error = new errors_1.WebRTCError(failed ? 'transport_failed' : 'connection_closed', 'transport', 'The WebRTC peer connection ended.');
|
|
20
|
+
if (this.attempt) {
|
|
21
|
+
this.failSetup(error, failed ? 'failed' : 'remote');
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
this.finish(failed ? 'failed' : 'remote', failed ? error : undefined);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
this.checkReady();
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
this.onChannelEvent = (event) => {
|
|
32
|
+
if (this.currentState === 'closed') {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (event.type === 'closed') {
|
|
36
|
+
if (this.attempt) {
|
|
37
|
+
this.failSetup(new errors_1.WebRTCError('connection_closed', 'transport', 'The data channel closed during setup.'), 'remote');
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
this.finish('remote');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else if (event.type === 'error') {
|
|
44
|
+
if (event.error.phase === 'transport') {
|
|
45
|
+
if (this.attempt) {
|
|
46
|
+
this.failSetup(event.error, 'failed');
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
this.finish('failed', event.error);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
this.connectionEvents.emit(event);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
this.checkReady();
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const host = globalThis;
|
|
61
|
+
if (!host.RTCPeerConnection) {
|
|
62
|
+
throw new errors_1.WebRTCError('unsupported_environment', 'transport', 'WebRTC requires a browser with RTCPeerConnection.');
|
|
63
|
+
}
|
|
64
|
+
const peer = new host.RTCPeerConnection(configuration);
|
|
65
|
+
try {
|
|
66
|
+
this.adapter = new data_channel_1.DataChannel(peer.createDataChannel('oai-events'));
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
peer.close();
|
|
70
|
+
throw new errors_1.WebRTCError('negotiation_failed', 'transport', 'Could not create the WebRTC data channel.', error);
|
|
71
|
+
}
|
|
72
|
+
this.peer = peer;
|
|
73
|
+
this.peerConnection = peer;
|
|
74
|
+
this.dataChannel = this.adapter.dataChannel;
|
|
75
|
+
peer.addEventListener('connectionstatechange', this.onPeerState);
|
|
76
|
+
this.adapter.onConnectionEvent(this.onChannelEvent);
|
|
77
|
+
}
|
|
78
|
+
/** Current state, including before subscriptions are registered; notifications are not replayed. */
|
|
79
|
+
get state() {
|
|
80
|
+
return this.currentState;
|
|
81
|
+
}
|
|
82
|
+
/** Subscribes to one server event type with its generated payload; returns an independent unsubscribe function. */
|
|
83
|
+
on(type, handler) {
|
|
84
|
+
return this.adapter.on(type, handler);
|
|
85
|
+
}
|
|
86
|
+
/** Subscribes before connecting so initial server events are observable. API errors remain protocol data. */
|
|
87
|
+
onEvent(handler) {
|
|
88
|
+
return this.adapter.onEvent(handler);
|
|
89
|
+
}
|
|
90
|
+
/** Subscribes to local diagnostics and lifecycle. Setup failures reject connect() instead of emitting errors here. */
|
|
91
|
+
onConnectionEvent(handler) {
|
|
92
|
+
if (this.currentState === 'closed') {
|
|
93
|
+
throw new errors_1.WebRTCError('invalid_state', 'transport', 'The WebRTC connection is closed.');
|
|
94
|
+
}
|
|
95
|
+
return this.connectionEvents.add(handler);
|
|
96
|
+
}
|
|
97
|
+
/** Sends without buffering; returning means local submission, not server acceptance. */
|
|
98
|
+
send(event) {
|
|
99
|
+
this.adapter.send(event);
|
|
100
|
+
}
|
|
101
|
+
/** Negotiates once and waits for both native connection and data-channel readiness. Defaults to a 30-second deadline. */
|
|
102
|
+
connect(options) {
|
|
103
|
+
if (this.currentState !== 'new') {
|
|
104
|
+
return Promise.reject(new errors_1.WebRTCError('invalid_state', 'transport', 'connect() may only be called once per connection.'));
|
|
105
|
+
}
|
|
106
|
+
const timeoutMs = options.timeoutMs ?? 30000;
|
|
107
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0 || timeoutMs > 2147483647) {
|
|
108
|
+
return Promise.reject(new errors_1.WebRTCError('invalid_state', 'transport', 'timeoutMs must be a positive finite timer duration.'));
|
|
109
|
+
}
|
|
110
|
+
const controller = new AbortController();
|
|
111
|
+
// oxlint-disable-next-line promise/avoid-new -- Native readiness, timeout, and abort events settle one connection attempt.
|
|
112
|
+
const result = new Promise((resolve, reject) => {
|
|
113
|
+
const onAbort = () => this.failSetup(new errors_1.WebRTCError('aborted', this.attempt?.phase ?? 'offer', 'WebRTC setup was cancelled.', options.signal?.reason), 'aborted');
|
|
114
|
+
const timer = setTimeout(() => this.failSetup(new errors_1.WebRTCError('timeout', this.attempt?.phase ?? 'offer', 'WebRTC setup timed out.'), 'timeout'), timeoutMs);
|
|
115
|
+
this.attempt = {
|
|
116
|
+
controller,
|
|
117
|
+
phase: 'offer',
|
|
118
|
+
answered: false,
|
|
119
|
+
resolve,
|
|
120
|
+
reject,
|
|
121
|
+
cleanup: () => {
|
|
122
|
+
clearTimeout(timer);
|
|
123
|
+
options.signal?.removeEventListener('abort', onAbort);
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
this.currentState = 'connecting';
|
|
127
|
+
options.signal?.addEventListener('abort', onAbort, { once: true });
|
|
128
|
+
if (options.signal?.aborted) {
|
|
129
|
+
onAbort();
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
this.connectionEvents.emit({ type: 'state.changed', state: 'connecting' });
|
|
133
|
+
});
|
|
134
|
+
const { attempt } = this;
|
|
135
|
+
if (attempt) {
|
|
136
|
+
void this.negotiate(options, attempt);
|
|
137
|
+
}
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
/** Idempotently closes owned native resources. It never stops application-supplied media tracks. */
|
|
141
|
+
close() {
|
|
142
|
+
if (this.attempt) {
|
|
143
|
+
this.failSetup(new errors_1.WebRTCError('aborted', this.attempt.phase, 'WebRTC setup was closed by the application.'), 'local');
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
this.finish('local');
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
async negotiate(options, attempt) {
|
|
150
|
+
try {
|
|
151
|
+
const offer = await this.peer.createOffer();
|
|
152
|
+
if (this.attempt !== attempt) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
await this.peer.setLocalDescription(offer);
|
|
156
|
+
if (this.attempt !== attempt) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const sdp = this.peer.localDescription?.sdp;
|
|
160
|
+
if (!sdp) {
|
|
161
|
+
throw new Error('The local description has no SDP.');
|
|
162
|
+
}
|
|
163
|
+
attempt.phase = 'signaling';
|
|
164
|
+
const answer = await options.exchangeSdp(sdp, { signal: attempt.controller.signal });
|
|
165
|
+
if (this.attempt !== attempt) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (typeof answer !== 'string' || answer.length === 0) {
|
|
169
|
+
throw new TypeError('The SDP callback must return a nonempty string.');
|
|
170
|
+
}
|
|
171
|
+
attempt.phase = 'answer';
|
|
172
|
+
await this.peer.setRemoteDescription({ type: 'answer', sdp: answer });
|
|
173
|
+
if (this.attempt !== attempt) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
attempt.answered = true;
|
|
177
|
+
attempt.phase = 'transport';
|
|
178
|
+
this.checkReady();
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
if (this.attempt !== attempt) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
this.failSetup(new errors_1.WebRTCError(attempt.phase === 'signaling' ? 'signaling_failed' : 'negotiation_failed', attempt.phase, 'Could not establish the WebRTC connection.', error), 'failed');
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
checkReady() {
|
|
188
|
+
const { attempt } = this;
|
|
189
|
+
if (!attempt?.answered ||
|
|
190
|
+
this.peer.connectionState !== 'connected' ||
|
|
191
|
+
this.dataChannel.readyState !== 'open') {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
this.attempt = undefined;
|
|
195
|
+
attempt.cleanup();
|
|
196
|
+
this.currentState = 'connected';
|
|
197
|
+
attempt.resolve();
|
|
198
|
+
this.connectionEvents.emit({ type: 'state.changed', state: 'connected' });
|
|
199
|
+
}
|
|
200
|
+
failSetup(error, reason) {
|
|
201
|
+
const { attempt } = this;
|
|
202
|
+
if (!attempt) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
this.attempt = undefined;
|
|
206
|
+
attempt.cleanup();
|
|
207
|
+
attempt.reject(error);
|
|
208
|
+
// Change state and detach first: abort callbacks cannot resurrect setup.
|
|
209
|
+
this.finish(reason);
|
|
210
|
+
attempt.controller.abort(error);
|
|
211
|
+
}
|
|
212
|
+
finish(reason, error) {
|
|
213
|
+
if (this.currentState === 'closed') {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
this.currentState = 'closed';
|
|
217
|
+
this.peer.removeEventListener('connectionstatechange', this.onPeerState);
|
|
218
|
+
this.adapter.dispose();
|
|
219
|
+
this.peer.close();
|
|
220
|
+
if (error) {
|
|
221
|
+
this.connectionEvents.emit({ type: 'error', error, fatal: true });
|
|
222
|
+
}
|
|
223
|
+
this.connectionEvents.emit({ type: 'closed', reason });
|
|
224
|
+
this.connectionEvents.clear();
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
exports.WebRTCConnection = WebRTCConnection;
|
|
228
|
+
//# sourceMappingURL=connection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.js","sourceRoot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,YAAY,GAAG,QAAQ,CAAC;QAC7B,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAC,uBAAuB,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;QACzE,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;QACvB,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;QAClB,IAAI,KAAK,EAAE,CAAC;YACV,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QACpE,CAAC;QACD,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC;QACvD,IAAI,CAAC,gBAAgB,CAAC,KAAK,EAAE,CAAC;IAChC,CAAC;CAkDF;AA3RD,4CA2RC"}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { DataChannel } from "./data-channel.mjs";
|
|
2
|
+
import { WebRTCError } from "./errors.mjs";
|
|
3
|
+
import { Subscriptions } from "./subscriptions.mjs";
|
|
4
|
+
/** Browser-native WebRTC negotiation with application-owned SDP signaling. */
|
|
5
|
+
export class WebRTCConnection {
|
|
6
|
+
/** Creates native resources, but does not request media, contact a server, or begin negotiation. */
|
|
7
|
+
constructor(configuration) {
|
|
8
|
+
this.connectionEvents = new Subscriptions();
|
|
9
|
+
this.currentState = 'new';
|
|
10
|
+
this.onPeerState = () => {
|
|
11
|
+
if (this.currentState === 'closed') {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
if (this.peer.connectionState === 'failed' || this.peer.connectionState === 'closed') {
|
|
15
|
+
const failed = this.peer.connectionState === 'failed';
|
|
16
|
+
const error = new WebRTCError(failed ? 'transport_failed' : 'connection_closed', 'transport', 'The WebRTC peer connection ended.');
|
|
17
|
+
if (this.attempt) {
|
|
18
|
+
this.failSetup(error, failed ? 'failed' : 'remote');
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
this.finish(failed ? 'failed' : 'remote', failed ? error : undefined);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
this.checkReady();
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
this.onChannelEvent = (event) => {
|
|
29
|
+
if (this.currentState === 'closed') {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (event.type === 'closed') {
|
|
33
|
+
if (this.attempt) {
|
|
34
|
+
this.failSetup(new WebRTCError('connection_closed', 'transport', 'The data channel closed during setup.'), 'remote');
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
this.finish('remote');
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else if (event.type === 'error') {
|
|
41
|
+
if (event.error.phase === 'transport') {
|
|
42
|
+
if (this.attempt) {
|
|
43
|
+
this.failSetup(event.error, 'failed');
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
this.finish('failed', event.error);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
this.connectionEvents.emit(event);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
this.checkReady();
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const host = globalThis;
|
|
58
|
+
if (!host.RTCPeerConnection) {
|
|
59
|
+
throw new WebRTCError('unsupported_environment', 'transport', 'WebRTC requires a browser with RTCPeerConnection.');
|
|
60
|
+
}
|
|
61
|
+
const peer = new host.RTCPeerConnection(configuration);
|
|
62
|
+
try {
|
|
63
|
+
this.adapter = new DataChannel(peer.createDataChannel('oai-events'));
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
peer.close();
|
|
67
|
+
throw new WebRTCError('negotiation_failed', 'transport', 'Could not create the WebRTC data channel.', error);
|
|
68
|
+
}
|
|
69
|
+
this.peer = peer;
|
|
70
|
+
this.peerConnection = peer;
|
|
71
|
+
this.dataChannel = this.adapter.dataChannel;
|
|
72
|
+
peer.addEventListener('connectionstatechange', this.onPeerState);
|
|
73
|
+
this.adapter.onConnectionEvent(this.onChannelEvent);
|
|
74
|
+
}
|
|
75
|
+
/** Current state, including before subscriptions are registered; notifications are not replayed. */
|
|
76
|
+
get state() {
|
|
77
|
+
return this.currentState;
|
|
78
|
+
}
|
|
79
|
+
/** Subscribes to one server event type with its generated payload; returns an independent unsubscribe function. */
|
|
80
|
+
on(type, handler) {
|
|
81
|
+
return this.adapter.on(type, handler);
|
|
82
|
+
}
|
|
83
|
+
/** Subscribes before connecting so initial server events are observable. API errors remain protocol data. */
|
|
84
|
+
onEvent(handler) {
|
|
85
|
+
return this.adapter.onEvent(handler);
|
|
86
|
+
}
|
|
87
|
+
/** Subscribes to local diagnostics and lifecycle. Setup failures reject connect() instead of emitting errors here. */
|
|
88
|
+
onConnectionEvent(handler) {
|
|
89
|
+
if (this.currentState === 'closed') {
|
|
90
|
+
throw new WebRTCError('invalid_state', 'transport', 'The WebRTC connection is closed.');
|
|
91
|
+
}
|
|
92
|
+
return this.connectionEvents.add(handler);
|
|
93
|
+
}
|
|
94
|
+
/** Sends without buffering; returning means local submission, not server acceptance. */
|
|
95
|
+
send(event) {
|
|
96
|
+
this.adapter.send(event);
|
|
97
|
+
}
|
|
98
|
+
/** Negotiates once and waits for both native connection and data-channel readiness. Defaults to a 30-second deadline. */
|
|
99
|
+
connect(options) {
|
|
100
|
+
if (this.currentState !== 'new') {
|
|
101
|
+
return Promise.reject(new WebRTCError('invalid_state', 'transport', 'connect() may only be called once per connection.'));
|
|
102
|
+
}
|
|
103
|
+
const timeoutMs = options.timeoutMs ?? 30000;
|
|
104
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0 || timeoutMs > 2147483647) {
|
|
105
|
+
return Promise.reject(new WebRTCError('invalid_state', 'transport', 'timeoutMs must be a positive finite timer duration.'));
|
|
106
|
+
}
|
|
107
|
+
const controller = new AbortController();
|
|
108
|
+
// oxlint-disable-next-line promise/avoid-new -- Native readiness, timeout, and abort events settle one connection attempt.
|
|
109
|
+
const result = new Promise((resolve, reject) => {
|
|
110
|
+
const onAbort = () => this.failSetup(new WebRTCError('aborted', this.attempt?.phase ?? 'offer', 'WebRTC setup was cancelled.', options.signal?.reason), 'aborted');
|
|
111
|
+
const timer = setTimeout(() => this.failSetup(new WebRTCError('timeout', this.attempt?.phase ?? 'offer', 'WebRTC setup timed out.'), 'timeout'), timeoutMs);
|
|
112
|
+
this.attempt = {
|
|
113
|
+
controller,
|
|
114
|
+
phase: 'offer',
|
|
115
|
+
answered: false,
|
|
116
|
+
resolve,
|
|
117
|
+
reject,
|
|
118
|
+
cleanup: () => {
|
|
119
|
+
clearTimeout(timer);
|
|
120
|
+
options.signal?.removeEventListener('abort', onAbort);
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
this.currentState = 'connecting';
|
|
124
|
+
options.signal?.addEventListener('abort', onAbort, { once: true });
|
|
125
|
+
if (options.signal?.aborted) {
|
|
126
|
+
onAbort();
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
this.connectionEvents.emit({ type: 'state.changed', state: 'connecting' });
|
|
130
|
+
});
|
|
131
|
+
const { attempt } = this;
|
|
132
|
+
if (attempt) {
|
|
133
|
+
void this.negotiate(options, attempt);
|
|
134
|
+
}
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
/** Idempotently closes owned native resources. It never stops application-supplied media tracks. */
|
|
138
|
+
close() {
|
|
139
|
+
if (this.attempt) {
|
|
140
|
+
this.failSetup(new WebRTCError('aborted', this.attempt.phase, 'WebRTC setup was closed by the application.'), 'local');
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
this.finish('local');
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async negotiate(options, attempt) {
|
|
147
|
+
try {
|
|
148
|
+
const offer = await this.peer.createOffer();
|
|
149
|
+
if (this.attempt !== attempt) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
await this.peer.setLocalDescription(offer);
|
|
153
|
+
if (this.attempt !== attempt) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const sdp = this.peer.localDescription?.sdp;
|
|
157
|
+
if (!sdp) {
|
|
158
|
+
throw new Error('The local description has no SDP.');
|
|
159
|
+
}
|
|
160
|
+
attempt.phase = 'signaling';
|
|
161
|
+
const answer = await options.exchangeSdp(sdp, { signal: attempt.controller.signal });
|
|
162
|
+
if (this.attempt !== attempt) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (typeof answer !== 'string' || answer.length === 0) {
|
|
166
|
+
throw new TypeError('The SDP callback must return a nonempty string.');
|
|
167
|
+
}
|
|
168
|
+
attempt.phase = 'answer';
|
|
169
|
+
await this.peer.setRemoteDescription({ type: 'answer', sdp: answer });
|
|
170
|
+
if (this.attempt !== attempt) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
attempt.answered = true;
|
|
174
|
+
attempt.phase = 'transport';
|
|
175
|
+
this.checkReady();
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
if (this.attempt !== attempt) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
this.failSetup(new WebRTCError(attempt.phase === 'signaling' ? 'signaling_failed' : 'negotiation_failed', attempt.phase, 'Could not establish the WebRTC connection.', error), 'failed');
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
checkReady() {
|
|
185
|
+
const { attempt } = this;
|
|
186
|
+
if (!attempt?.answered ||
|
|
187
|
+
this.peer.connectionState !== 'connected' ||
|
|
188
|
+
this.dataChannel.readyState !== 'open') {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
this.attempt = undefined;
|
|
192
|
+
attempt.cleanup();
|
|
193
|
+
this.currentState = 'connected';
|
|
194
|
+
attempt.resolve();
|
|
195
|
+
this.connectionEvents.emit({ type: 'state.changed', state: 'connected' });
|
|
196
|
+
}
|
|
197
|
+
failSetup(error, reason) {
|
|
198
|
+
const { attempt } = this;
|
|
199
|
+
if (!attempt) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
this.attempt = undefined;
|
|
203
|
+
attempt.cleanup();
|
|
204
|
+
attempt.reject(error);
|
|
205
|
+
// Change state and detach first: abort callbacks cannot resurrect setup.
|
|
206
|
+
this.finish(reason);
|
|
207
|
+
attempt.controller.abort(error);
|
|
208
|
+
}
|
|
209
|
+
finish(reason, error) {
|
|
210
|
+
if (this.currentState === 'closed') {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
this.currentState = 'closed';
|
|
214
|
+
this.peer.removeEventListener('connectionstatechange', this.onPeerState);
|
|
215
|
+
this.adapter.dispose();
|
|
216
|
+
this.peer.close();
|
|
217
|
+
if (error) {
|
|
218
|
+
this.connectionEvents.emit({ type: 'error', error, fatal: true });
|
|
219
|
+
}
|
|
220
|
+
this.connectionEvents.emit({ type: 'closed', reason });
|
|
221
|
+
this.connectionEvents.clear();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
//# sourceMappingURL=connection.mjs.map
|