@decartai/sdk 0.0.44 → 0.0.45

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/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@ import { ProcessClient } from "./process/client.js";
4
4
  import { JobStatus, JobStatusResponse, JobSubmitResponse, QueueJobResult, QueueSubmitAndPollOptions, QueueSubmitOptions } from "./queue/types.js";
5
5
  import { QueueClient } from "./queue/client.js";
6
6
  import { DecartSDKError, ERROR_CODES } from "./utils/errors.js";
7
- import { ConnectionState } from "./realtime/webrtc-connection.js";
7
+ import { ConnectionState } from "./realtime/types.js";
8
8
  import { SetInput } from "./realtime/methods.js";
9
9
  import { AvatarOptions, Events, RealTimeClient, RealTimeClientConnectOptions, RealTimeClientInitialState } from "./realtime/client.js";
10
10
  import { ModelState } from "./shared/types.js";
@@ -1,4 +1,5 @@
1
1
  import { DecartSDKError } from "../utils/errors.js";
2
+ import { ConnectionState } from "./types.js";
2
3
  import { SetInput } from "./methods.js";
3
4
  import { z } from "zod";
4
5
 
@@ -40,7 +41,7 @@ declare const realTimeClientConnectOptionsSchema: z.ZodObject<{
40
41
  }, z.core.$strip>;
41
42
  type RealTimeClientConnectOptions = z.infer<typeof realTimeClientConnectOptionsSchema>;
42
43
  type Events = {
43
- connectionChange: "connected" | "connecting" | "disconnected" | "reconnecting";
44
+ connectionChange: ConnectionState;
44
45
  error: DecartSDKError;
45
46
  };
46
47
  type RealTimeClient = {
@@ -51,7 +52,7 @@ type RealTimeClient = {
51
52
  enhance?: boolean;
52
53
  }) => Promise<void>;
53
54
  isConnected: () => boolean;
54
- getConnectionState: () => "connected" | "connecting" | "disconnected" | "reconnecting";
55
+ getConnectionState: () => ConnectionState;
55
56
  disconnect: () => void;
56
57
  on: <K extends keyof Events>(event: K, listener: (data: Events[K]) => void) => void;
57
58
  off: <K extends keyof Events>(event: K, listener: (data: Events[K]) => void) => void;
@@ -90,16 +90,33 @@ const createRealTimeClient = (opts) => {
90
90
  text: initialState.prompt.text,
91
91
  enhance: initialState.prompt.enhance
92
92
  } : void 0;
93
+ const url = `${baseUrl}${options.model.urlPath}`;
94
+ const eventBuffer = [];
95
+ let buffering = true;
96
+ const emitOrBuffer = (event, data) => {
97
+ if (buffering) eventBuffer.push({
98
+ event,
99
+ data
100
+ });
101
+ else eventEmitter.emit(event, data);
102
+ };
103
+ const flushBufferedEvents = () => {
104
+ setTimeout(() => {
105
+ buffering = false;
106
+ for (const { event, data } of eventBuffer) eventEmitter.emit(event, data);
107
+ eventBuffer.length = 0;
108
+ }, 0);
109
+ };
93
110
  webrtcManager = new WebRTCManager({
94
- webrtcUrl: `${`${baseUrl}${options.model.urlPath}`}?api_key=${encodeURIComponent(apiKey)}&model=${encodeURIComponent(options.model.name)}`,
111
+ webrtcUrl: `${url}?api_key=${encodeURIComponent(apiKey)}&model=${encodeURIComponent(options.model.name)}`,
95
112
  integration,
96
113
  onRemoteStream,
97
114
  onConnectionStateChange: (state) => {
98
- eventEmitter.emit("connectionChange", state);
115
+ emitOrBuffer("connectionChange", state);
99
116
  },
100
117
  onError: (error) => {
101
118
  console.error("WebRTC error:", error);
102
- eventEmitter.emit("error", createWebrtcError(error));
119
+ emitOrBuffer("error", createWebrtcError(error));
103
120
  },
104
121
  customizeOffer: options.customizeOffer,
105
122
  vp8MinBitrate: 300,
@@ -121,6 +138,8 @@ const createRealTimeClient = (opts) => {
121
138
  isConnected: () => manager.isConnected(),
122
139
  getConnectionState: () => manager.getConnectionState(),
123
140
  disconnect: () => {
141
+ buffering = false;
142
+ eventBuffer.length = 0;
124
143
  manager.cleanup();
125
144
  audioStreamManager?.cleanup();
126
145
  },
@@ -137,6 +156,7 @@ const createRealTimeClient = (opts) => {
137
156
  const manager$1 = audioStreamManager;
138
157
  client.playAudio = (audio) => manager$1.playAudio(audio);
139
158
  }
159
+ flushBufferedEvents();
140
160
  return client;
141
161
  } catch (error) {
142
162
  webrtcManager?.cleanup();
@@ -20,7 +20,7 @@ const setPromptInputSchema = z.object({
20
20
  const realtimeMethods = (webrtcManager, imageToBase64) => {
21
21
  const assertConnected = () => {
22
22
  const state = webrtcManager.getConnectionState();
23
- if (state !== "connected") throw new Error(`Cannot send message: connection is ${state}`);
23
+ if (state !== "connected" && state !== "generating") throw new Error(`Cannot send message: connection is ${state}`);
24
24
  };
25
25
  const set = async (input) => {
26
26
  assertConnected();
@@ -0,0 +1,5 @@
1
+ //#region src/realtime/types.d.ts
2
+
3
+ type ConnectionState = "connecting" | "connected" | "generating" | "disconnected" | "reconnecting";
4
+ //#endregion
5
+ export { ConnectionState };
@@ -58,7 +58,7 @@ var WebRTCConnection = class {
58
58
  await this.setupNewPeerConnection();
59
59
  await Promise.race([new Promise((resolve, reject) => {
60
60
  const checkConnection = setInterval(() => {
61
- if (this.state === "connected") {
61
+ if (this.state === "connected" || this.state === "generating") {
62
62
  clearInterval(checkConnection);
63
63
  resolve();
64
64
  } else if (this.state === "disconnected") {
@@ -94,6 +94,10 @@ var WebRTCConnection = class {
94
94
  this.websocketMessagesEmitter.emit("promptAck", msg);
95
95
  return;
96
96
  }
97
+ if (msg.type === "generation_started") {
98
+ this.setState("generating");
99
+ return;
100
+ }
97
101
  if (!this.pc) return;
98
102
  switch (msg.type) {
99
103
  case "ready": {
@@ -252,7 +256,9 @@ var WebRTCConnection = class {
252
256
  this.pc.onconnectionstatechange = () => {
253
257
  if (!this.pc) return;
254
258
  const s = this.pc.connectionState;
255
- this.setState(s === "connected" ? "connected" : ["connecting", "new"].includes(s) ? "connecting" : "disconnected");
259
+ const nextState = s === "connected" ? "connected" : ["connecting", "new"].includes(s) ? "connecting" : "disconnected";
260
+ if (this.state === "generating" && nextState !== "disconnected") return;
261
+ this.setState(nextState);
256
262
  };
257
263
  this.pc.oniceconnectionstatechange = () => {
258
264
  if (this.pc?.iceConnectionState === "failed") {
@@ -43,7 +43,7 @@ var WebRTCManager = class {
43
43
  emitState(state) {
44
44
  if (this.managerState !== state) {
45
45
  this.managerState = state;
46
- if (state === "connected") this.hasConnected = true;
46
+ if (state === "connected" || state === "generating") this.hasConnected = true;
47
47
  this.config.onConnectionStateChange?.(state);
48
48
  }
49
49
  }
@@ -53,9 +53,9 @@ var WebRTCManager = class {
53
53
  return;
54
54
  }
55
55
  if (this.isReconnecting) {
56
- if (state === "connected") {
56
+ if (state === "connected" || state === "generating") {
57
57
  this.isReconnecting = false;
58
- this.emitState("connected");
58
+ this.emitState(state);
59
59
  }
60
60
  return;
61
61
  }
@@ -137,7 +137,7 @@ var WebRTCManager = class {
137
137
  this.emitState("disconnected");
138
138
  }
139
139
  isConnected() {
140
- return this.managerState === "connected";
140
+ return this.managerState === "connected" || this.managerState === "generating";
141
141
  }
142
142
  getConnectionState() {
143
143
  return this.managerState;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decartai/sdk",
3
- "version": "0.0.44",
3
+ "version": "0.0.45",
4
4
  "description": "Decart's JavaScript SDK",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -1,7 +0,0 @@
1
- import "mitt";
2
-
3
- //#region src/realtime/webrtc-connection.d.ts
4
-
5
- type ConnectionState = "connecting" | "connected" | "disconnected" | "reconnecting";
6
- //#endregion
7
- export { ConnectionState };