@gravity-rail/sdk 8.2.0 → 9.2.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.
Files changed (51) hide show
  1. package/README.md +2 -3
  2. package/dist/{agent_standing_job-CQqd8LDQ.d.ts → agent_team-C4ZYR_m1.d.ts} +731 -354
  3. package/dist/{agent_standing_job-DfjwGfg_.d.cts → agent_team-C6FdNgYH.d.cts} +731 -354
  4. package/dist/{chunk-U2U5X3LY.js → chunk-7LQYEV5Y.js} +245 -226
  5. package/dist/chunk-7LQYEV5Y.js.map +1 -0
  6. package/dist/{chunk-IIIRXFKG.js → chunk-MWTQXZGB.js} +1 -1
  7. package/dist/chunk-MWTQXZGB.js.map +1 -0
  8. package/dist/{chunk-6KPGCPGW.js → chunk-ZJJDZUAK.js} +40 -1
  9. package/dist/chunk-ZJJDZUAK.js.map +1 -0
  10. package/dist/{client-Bd-zXY42.d.ts → client-DVF3qwNq.d.ts} +2380 -1113
  11. package/dist/{client-BGGgZhKg.d.cts → client-DwslF70Y.d.cts} +2380 -1113
  12. package/dist/helpers.cjs +41 -0
  13. package/dist/helpers.cjs.map +1 -1
  14. package/dist/helpers.d.cts +2 -2
  15. package/dist/helpers.d.ts +2 -2
  16. package/dist/helpers.js +5 -1
  17. package/dist/helpers.js.map +1 -1
  18. package/dist/index.cjs +290 -305
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.cts +7 -23
  21. package/dist/index.d.ts +7 -23
  22. package/dist/index.js +14 -84
  23. package/dist/index.js.map +1 -1
  24. package/dist/internal.cjs +252 -230
  25. package/dist/internal.cjs.map +1 -1
  26. package/dist/internal.d.cts +775 -999
  27. package/dist/internal.d.ts +775 -999
  28. package/dist/internal.js +12 -9
  29. package/dist/internal.js.map +1 -1
  30. package/dist/member-import-BpcX-swC.d.ts +92 -0
  31. package/dist/member-import-CA9YWv3Y.d.cts +92 -0
  32. package/dist/realtime.cjs +109 -54
  33. package/dist/realtime.cjs.map +1 -1
  34. package/dist/realtime.d.cts +7 -2
  35. package/dist/realtime.d.ts +7 -2
  36. package/dist/realtime.js +109 -54
  37. package/dist/realtime.js.map +1 -1
  38. package/dist/schemas.cjs +24 -6
  39. package/dist/schemas.cjs.map +1 -1
  40. package/dist/schemas.d.cts +53 -39
  41. package/dist/schemas.d.ts +53 -39
  42. package/dist/schemas.js +24 -7
  43. package/dist/schemas.js.map +1 -1
  44. package/dist/{tasks-2LhXIwL8.d.ts → tasks-BGLfmf5y.d.ts} +69 -904
  45. package/dist/{tasks-BTURuIlD.d.cts → tasks-BZb3rt5m.d.cts} +69 -904
  46. package/package.json +10 -9
  47. package/dist/chunk-6KPGCPGW.js.map +0 -1
  48. package/dist/chunk-IIIRXFKG.js.map +0 -1
  49. package/dist/chunk-U2U5X3LY.js.map +0 -1
  50. package/dist/member-import-4VSE3yYo.d.cts +0 -46
  51. package/dist/member-import-BQDFoSyf.d.ts +0 -46
@@ -306,7 +306,11 @@ interface OperatorSessionHandlers {
306
306
  onError?: (message: string) => void;
307
307
  /** The session was refused for want of an org session; the step-up path takes it from here. */
308
308
  onOrgAuthRequired?: (requirement: OrgAuthRequirement) => void;
309
- /** Fired when the server closes the session (e.g. callee hung up). */
309
+ /**
310
+ * Fired once when a logged-in session ends without `disconnect()`: the server
311
+ * ended it (e.g. callee hung up, with the server's reason) or its socket closed
312
+ * with no such notice (reason `'disconnect'`).
313
+ */
310
314
  onSessionClosed?: (reason?: string) => void;
311
315
  }
312
316
  declare class OperatorSessionClient {
@@ -325,7 +329,7 @@ declare class OperatorSessionClient {
325
329
  private receivedRemoteAudio;
326
330
  private heartbeatTimer;
327
331
  private sessionCloseNotified;
328
- private intentionalDisconnect;
332
+ private cancelPendingLogin;
329
333
  /** Bumped by every connect and disconnect; a stale connect stops at its next await. */
330
334
  private connectGeneration;
331
335
  constructor(handlers?: OperatorSessionHandlers);
@@ -392,6 +396,7 @@ declare class OperatorSessionClient {
392
396
  private playPcm16;
393
397
  private stopPlayback;
394
398
  disconnect(): Promise<void>;
399
+ private closeSession;
395
400
  }
396
401
 
397
402
  export { AccountRealtimeClient, AccountRealtimeSession, ChannelEvent, ChannelListener, LOGIN_TOKEN_TIMEOUT_MS, OperatorCallPhase, OperatorSessionClient, OperatorSessionState, OrgAuthRequirement, OrgSessionsUpdate, RealtimeAuthSession, RealtimeSessionBase, RealtimeSocketClient, Unsubscribe, WorkspaceRealtimeClient, buildAccountWsUrl, buildWorkspaceWsUrl, editorCollaborationChannel, editorCollaborationItemKey, initAccountSocket, initWorkspaceSocket, isAccountSocketConnected, isWorkspaceSocketConnected, onAccountSocketConnection, onWorkspaceSocketConnection, parseEditorCollaborationEvent, subscribeAccountChannel, subscribeWorkspaceChannel, workspaceChannel };
@@ -306,7 +306,11 @@ interface OperatorSessionHandlers {
306
306
  onError?: (message: string) => void;
307
307
  /** The session was refused for want of an org session; the step-up path takes it from here. */
308
308
  onOrgAuthRequired?: (requirement: OrgAuthRequirement) => void;
309
- /** Fired when the server closes the session (e.g. callee hung up). */
309
+ /**
310
+ * Fired once when a logged-in session ends without `disconnect()`: the server
311
+ * ended it (e.g. callee hung up, with the server's reason) or its socket closed
312
+ * with no such notice (reason `'disconnect'`).
313
+ */
310
314
  onSessionClosed?: (reason?: string) => void;
311
315
  }
312
316
  declare class OperatorSessionClient {
@@ -325,7 +329,7 @@ declare class OperatorSessionClient {
325
329
  private receivedRemoteAudio;
326
330
  private heartbeatTimer;
327
331
  private sessionCloseNotified;
328
- private intentionalDisconnect;
332
+ private cancelPendingLogin;
329
333
  /** Bumped by every connect and disconnect; a stale connect stops at its next await. */
330
334
  private connectGeneration;
331
335
  constructor(handlers?: OperatorSessionHandlers);
@@ -392,6 +396,7 @@ declare class OperatorSessionClient {
392
396
  private playPcm16;
393
397
  private stopPlayback;
394
398
  disconnect(): Promise<void>;
399
+ private closeSession;
395
400
  }
396
401
 
397
402
  export { AccountRealtimeClient, AccountRealtimeSession, ChannelEvent, ChannelListener, LOGIN_TOKEN_TIMEOUT_MS, OperatorCallPhase, OperatorSessionClient, OperatorSessionState, OrgAuthRequirement, OrgSessionsUpdate, RealtimeAuthSession, RealtimeSessionBase, RealtimeSocketClient, Unsubscribe, WorkspaceRealtimeClient, buildAccountWsUrl, buildWorkspaceWsUrl, editorCollaborationChannel, editorCollaborationItemKey, initAccountSocket, initWorkspaceSocket, isAccountSocketConnected, isWorkspaceSocketConnected, onAccountSocketConnection, onWorkspaceSocketConnection, parseEditorCollaborationEvent, subscribeAccountChannel, subscribeWorkspaceChannel, workspaceChannel };
package/dist/realtime.js CHANGED
@@ -759,6 +759,7 @@ function parseEditorCollaborationEvent(event) {
759
759
  }
760
760
 
761
761
  // src/realtime/operator-session-client.ts
762
+ var CONNECT_SUPERSEDED = "Operator session connect superseded";
762
763
  var OperatorSessionClient = class {
763
764
  constructor(handlers = {}) {
764
765
  this.handlers = handlers;
@@ -778,7 +779,7 @@ var OperatorSessionClient = class {
778
779
  receivedRemoteAudio = false;
779
780
  heartbeatTimer = null;
780
781
  sessionCloseNotified = false;
781
- intentionalDisconnect = false;
782
+ cancelPendingLogin = null;
782
783
  /** Bumped by every connect and disconnect; a stale connect stops at its next await. */
783
784
  connectGeneration = 0;
784
785
  getSessionState() {
@@ -804,8 +805,11 @@ var OperatorSessionClient = class {
804
805
  if (typeof window === "undefined" || typeof WebSocket === "undefined") {
805
806
  throw new Error("Operator session WebSocket is not available in this environment");
806
807
  }
807
- await this.disconnect();
808
808
  const generation = ++this.connectGeneration;
809
+ await this.closeSession();
810
+ if (generation !== this.connectGeneration) {
811
+ throw new Error(CONNECT_SUPERSEDED);
812
+ }
809
813
  this.sessionCloseNotified = false;
810
814
  this.setSessionState("connecting");
811
815
  this.setCallPhase("connecting");
@@ -814,7 +818,7 @@ var OperatorSessionClient = class {
814
818
  const wsUrl = `${isSsl ? "wss" : "ws"}://${host}/api/v2/w/${session.workspaceUuid}/operator/chats/${chatId}/session-ws`;
815
819
  const token = await mintLoginToken(session.getLoginToken);
816
820
  if (generation !== this.connectGeneration) {
817
- throw new Error("Operator session connect superseded");
821
+ throw new Error(CONNECT_SUPERSEDED);
818
822
  }
819
823
  if (!token) {
820
824
  this.setSessionState("error");
@@ -824,39 +828,62 @@ var OperatorSessionClient = class {
824
828
  await new Promise((resolve, reject) => {
825
829
  const ws = new WebSocket(wsUrl);
826
830
  this.ws = ws;
831
+ let loginSettled = false;
832
+ let loggedIn = false;
833
+ const settleLogin = (error) => {
834
+ if (loginSettled) return;
835
+ loginSettled = true;
836
+ if (this.cancelPendingLogin === cancelLogin) this.cancelPendingLogin = null;
837
+ if (error) {
838
+ reject(error);
839
+ } else {
840
+ loggedIn = true;
841
+ resolve();
842
+ }
843
+ };
844
+ const cancelLogin = () => settleLogin(new Error(CONNECT_SUPERSEDED));
845
+ this.cancelPendingLogin = cancelLogin;
827
846
  ws.onopen = () => {
847
+ if (this.ws !== ws) return;
828
848
  ws.send(JSON.stringify({ type: "login", token }));
829
849
  };
830
850
  ws.onmessage = (event) => {
851
+ if (this.ws !== ws) return;
831
852
  try {
832
853
  const msg = JSON.parse(String(event.data));
833
- this.handleMessage(msg, resolve, reject);
854
+ this.handleMessage(msg, () => settleLogin(), settleLogin);
834
855
  } catch {
835
856
  }
836
857
  };
837
858
  ws.onerror = () => {
859
+ if (this.ws !== ws) return;
838
860
  const message = "Operator session WebSocket error";
839
861
  const wasConnecting = this.sessionState === "connecting";
840
862
  this.setSessionState("error");
841
863
  if (wasConnecting) {
842
864
  this.setCallPhase("idle");
843
- reject(new Error(message));
865
+ settleLogin(new Error(message));
844
866
  } else {
845
867
  this.setCallPhase("failed");
846
868
  this.handlers.onError?.(message);
847
869
  }
848
870
  };
849
871
  ws.onclose = () => {
872
+ if (this.ws !== ws) {
873
+ settleLogin(new Error(CONNECT_SUPERSEDED));
874
+ return;
875
+ }
876
+ this.ws = null;
850
877
  this.clearHeartbeat();
851
878
  this.stopMicrophone();
852
879
  this.stopPlayback();
853
- const wasConnected = this.sessionState === "connected";
854
880
  this.setSessionState("idle");
855
881
  this.setCallPhase("idle");
856
- if (wasConnected && !this.intentionalDisconnect) {
882
+ if (loggedIn) {
857
883
  this.notifySessionClosed("disconnect");
884
+ } else {
885
+ settleLogin(new Error("Operator session closed before login"));
858
886
  }
859
- this.intentionalDisconnect = false;
860
887
  };
861
888
  });
862
889
  }
@@ -1064,57 +1091,81 @@ var OperatorSessionClient = class {
1064
1091
  throw new Error("Microphone not available in this browser");
1065
1092
  }
1066
1093
  if (this.micActive) return;
1067
- this.mediaStream = await navigator.mediaDevices.getUserMedia({
1094
+ const ws = this.ws;
1095
+ const mediaStream = await navigator.mediaDevices.getUserMedia({
1068
1096
  audio: {
1069
1097
  echoCancellation: true,
1070
1098
  noiseSuppression: true,
1071
1099
  autoGainControl: true
1072
1100
  }
1073
1101
  });
1074
- const Ctx = window.AudioContext ?? window.webkitAudioContext;
1075
- if (!Ctx) throw new Error("Web Audio not supported");
1076
- this.audioContext = new Ctx();
1077
- if (this.audioContext.state === "suspended") {
1078
- await this.audioContext.resume();
1102
+ if (this.ws !== ws) {
1103
+ for (const track of mediaStream.getTracks()) track.stop();
1104
+ throw new Error("Operator session WebSocket is not connected");
1079
1105
  }
1080
- const source = this.audioContext.createMediaStreamSource(this.mediaStream);
1081
- const processor = this.audioContext.createScriptProcessor(4096, 1, 1);
1082
- this.processor = processor;
1083
- processor.onaudioprocess = (event) => {
1084
- if (!this.ws || this.ws.readyState !== WebSocket.OPEN) return;
1085
- if (this.onHold) return;
1086
- if (this.muted) return;
1087
- if (this.callPhase !== "dialing" && this.callPhase !== "live" && this.callPhase !== "onHold") {
1088
- return;
1106
+ this.mediaStream = mediaStream;
1107
+ let audioContext = null;
1108
+ let processor = null;
1109
+ try {
1110
+ const Ctx = window.AudioContext ?? window.webkitAudioContext;
1111
+ if (!Ctx) throw new Error("Web Audio not supported");
1112
+ audioContext = new Ctx();
1113
+ this.audioContext = audioContext;
1114
+ if (audioContext.state === "suspended") {
1115
+ await audioContext.resume();
1089
1116
  }
1090
- const input = event.inputBuffer.getChannelData(0);
1091
- const copy = new Float32Array(input.length);
1092
- copy.set(input);
1093
- const sampleRate = this.audioContext?.sampleRate ?? TARGET_SAMPLE_RATE;
1094
- let pcm = copy;
1095
- if (sampleRate !== TARGET_SAMPLE_RATE) {
1096
- const ratio = sampleRate / TARGET_SAMPLE_RATE;
1097
- const newLen = Math.floor(copy.length / ratio);
1098
- const down = new Float32Array(newLen);
1099
- for (let i = 0; i < newLen; i += 1) {
1100
- down[i] = copy[Math.floor(i * ratio)];
1117
+ if (this.ws !== ws) throw new Error("Operator session WebSocket is not connected");
1118
+ const source = audioContext.createMediaStreamSource(mediaStream);
1119
+ processor = audioContext.createScriptProcessor(4096, 1, 1);
1120
+ this.processor = processor;
1121
+ processor.onaudioprocess = (event) => {
1122
+ if (!this.ws || this.ws.readyState !== WebSocket.OPEN) return;
1123
+ if (this.onHold) return;
1124
+ if (this.muted) return;
1125
+ if (this.callPhase !== "dialing" && this.callPhase !== "live" && this.callPhase !== "onHold") {
1126
+ return;
1101
1127
  }
1102
- pcm = down;
1103
- }
1104
- this.ws.send(
1105
- JSON.stringify({
1106
- type: "CUSTOM",
1107
- name: "voice.audio",
1108
- value: {
1109
- data: float32ArrayToBase64(pcm),
1110
- format: "pcm16",
1111
- sample_rate: TARGET_SAMPLE_RATE
1128
+ const input = event.inputBuffer.getChannelData(0);
1129
+ const copy = new Float32Array(input.length);
1130
+ copy.set(input);
1131
+ const sampleRate = this.audioContext?.sampleRate ?? TARGET_SAMPLE_RATE;
1132
+ let pcm = copy;
1133
+ if (sampleRate !== TARGET_SAMPLE_RATE) {
1134
+ const ratio = sampleRate / TARGET_SAMPLE_RATE;
1135
+ const newLen = Math.floor(copy.length / ratio);
1136
+ const down = new Float32Array(newLen);
1137
+ for (let i = 0; i < newLen; i += 1) {
1138
+ down[i] = copy[Math.floor(i * ratio)];
1112
1139
  }
1113
- })
1114
- );
1115
- };
1116
- source.connect(processor);
1117
- processor.connect(this.audioContext.destination);
1140
+ pcm = down;
1141
+ }
1142
+ this.ws.send(
1143
+ JSON.stringify({
1144
+ type: "CUSTOM",
1145
+ name: "voice.audio",
1146
+ value: {
1147
+ data: float32ArrayToBase64(pcm),
1148
+ format: "pcm16",
1149
+ sample_rate: TARGET_SAMPLE_RATE
1150
+ }
1151
+ })
1152
+ );
1153
+ };
1154
+ source.connect(processor);
1155
+ processor.connect(audioContext.destination);
1156
+ } catch (err) {
1157
+ if (processor && this.processor === processor) {
1158
+ processor.disconnect();
1159
+ this.processor = null;
1160
+ }
1161
+ for (const track of mediaStream.getTracks()) track.stop();
1162
+ if (this.mediaStream === mediaStream) this.mediaStream = null;
1163
+ if (audioContext && this.audioContext === audioContext) {
1164
+ this.audioContext = null;
1165
+ void audioContext.close().catch(() => void 0);
1166
+ }
1167
+ throw err;
1168
+ }
1118
1169
  this.micActive = true;
1119
1170
  }
1120
1171
  stopMicrophone() {
@@ -1163,17 +1214,21 @@ var OperatorSessionClient = class {
1163
1214
  }
1164
1215
  async disconnect() {
1165
1216
  this.connectGeneration += 1;
1166
- this.intentionalDisconnect = true;
1217
+ await this.closeSession();
1218
+ }
1219
+ async closeSession() {
1220
+ const cancelLogin = this.cancelPendingLogin;
1221
+ this.cancelPendingLogin = null;
1222
+ cancelLogin?.();
1167
1223
  if (this.callPhase === "dialing" || this.callPhase === "live" || this.callPhase === "onHold") {
1168
1224
  this.hangUp();
1169
1225
  }
1170
1226
  this.clearHeartbeat();
1171
1227
  this.stopMicrophone();
1172
1228
  this.stopPlayback();
1173
- if (this.ws) {
1174
- this.ws.close();
1175
- this.ws = null;
1176
- }
1229
+ const ws = this.ws;
1230
+ this.ws = null;
1231
+ ws?.close();
1177
1232
  if (this.audioContext && this.audioContext.state !== "closed") {
1178
1233
  await this.audioContext.close().catch(() => void 0);
1179
1234
  }