@koi-design/callkit 2.0.4 → 2.0.5-beta.10

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.
@@ -3225,6 +3225,10 @@ var WebCall = (() => {
3225
3225
  isLoginOuting = false;
3226
3226
  async login(params) {
3227
3227
  if (this.isLogining) {
3228
+ this.callKit.logger.info("login is already in progress cancel", {
3229
+ caller: "Api.login",
3230
+ content: { userName: params.userName, timestamp: params.timestamp }
3231
+ });
3228
3232
  return;
3229
3233
  }
3230
3234
  this.isLogining = true;
@@ -3234,15 +3238,17 @@ var WebCall = (() => {
3234
3238
  method: "post",
3235
3239
  data: params
3236
3240
  });
3237
- this.isLogining = false;
3238
3241
  return res;
3239
- } catch (error) {
3242
+ } finally {
3240
3243
  this.isLogining = false;
3241
- throw error;
3242
3244
  }
3243
3245
  }
3244
3246
  async loginOut(params) {
3245
3247
  if (this.isLoginOuting) {
3248
+ this.callKit.logger.info("loginOut is already in progress cancel", {
3249
+ caller: "Api.loginOut",
3250
+ content: { sessionId: params.sessionId, timestamp: params.timestamp }
3251
+ });
3246
3252
  return;
3247
3253
  }
3248
3254
  this.isLoginOuting = true;
@@ -3252,18 +3258,17 @@ var WebCall = (() => {
3252
3258
  method: "post",
3253
3259
  data: params
3254
3260
  });
3255
- this.isLoginOuting = false;
3256
3261
  return res;
3257
- } catch (error) {
3262
+ } finally {
3258
3263
  this.isLoginOuting = false;
3259
- throw error;
3260
3264
  }
3261
3265
  }
3262
3266
  async trackLogs(log) {
3263
3267
  return this.post(
3264
3268
  { url: "/agent/user/sdkLog", method: "post", data: { content: [log] } },
3265
3269
  {
3266
- useFormData: true
3270
+ useFormData: true,
3271
+ skipLog: true
3267
3272
  }
3268
3273
  );
3269
3274
  }
@@ -3279,7 +3284,7 @@ var WebCall = (() => {
3279
3284
  data: params
3280
3285
  });
3281
3286
  }
3282
- async post(config, extra = {}) {
3287
+ async post(config, extra = { skipLog: false }) {
3283
3288
  const { userInfo, host } = this.callKit.config.getConfig();
3284
3289
  const { sessionId } = userInfo;
3285
3290
  config.url = `${host}${config.url}`;
@@ -3302,11 +3307,37 @@ var WebCall = (() => {
3302
3307
  if (sessionId) {
3303
3308
  config.headers.sessionId = sessionId;
3304
3309
  }
3310
+ const startTime = Date.now();
3311
+ if (!extra.skipLog) {
3312
+ this.callKit.logger.info("API Request Start", {
3313
+ type: "API",
3314
+ caller: "API.Request",
3315
+ content: {
3316
+ url: config.url,
3317
+ headers: config.headers,
3318
+ data: config.data,
3319
+ extra,
3320
+ startTime
3321
+ }
3322
+ });
3323
+ }
3305
3324
  const res = await axios_default(config).catch(() => {
3306
3325
  this.callKit.config.reset();
3307
3326
  });
3327
+ const endTime = Date.now();
3328
+ if (!extra.skipLog) {
3329
+ this.callKit.logger.info("API Request Finish", {
3330
+ type: "API",
3331
+ caller: "API.Request",
3332
+ content: {
3333
+ url: config.url,
3334
+ duration: `${endTime - startTime}ms`,
3335
+ response: res
3336
+ }
3337
+ });
3338
+ }
3308
3339
  if (!res) {
3309
- this.callKit.config.reset();
3340
+ this.callKit.reset();
3310
3341
  throw new Error("Network error");
3311
3342
  }
3312
3343
  const { code, data, message } = res;
@@ -3627,6 +3658,10 @@ var WebCall = (() => {
3627
3658
  * Agent no answer
3628
3659
  */
3629
3660
  AGENT_NO_ANSWER: "AGENT_NO_ANSWER",
3661
+ /**
3662
+ * Agent hang up
3663
+ */
3664
+ AGENT_HANG_UP: "AGENT_HANG_UP",
3630
3665
  /**
3631
3666
  * Call detail record push
3632
3667
  */
@@ -3668,6 +3703,18 @@ var WebCall = (() => {
3668
3703
  interval: 5e3,
3669
3704
  maxSize: 8192
3670
3705
  };
3706
+ var SOCKET_RECONNECT_CONFIG = {
3707
+ enabled: true,
3708
+ maxAttempts: 3,
3709
+ delay: 1e3,
3710
+ pingInterval: 3e4,
3711
+ pingTimeout: 5e3
3712
+ };
3713
+ var SIP_RECONNECT_CONFIG = {
3714
+ enabled: true,
3715
+ maxAttempts: 3,
3716
+ delay: 1e3
3717
+ };
3671
3718
 
3672
3719
  // core/call.ts
3673
3720
  var Call = class {
@@ -3681,14 +3728,14 @@ var WebCall = (() => {
3681
3728
  this.callKit.logger.info("callStart", {
3682
3729
  caller: "Call.callStart",
3683
3730
  content: {
3684
- startConfirm: this.callKit.socket.satrtConfirm
3731
+ startConfirm: this.callKit.socket.startConfirm
3685
3732
  }
3686
3733
  });
3687
- if (!this.callKit.socket.satrtConfirm) {
3734
+ if (!this.callKit.socket.startConfirm) {
3688
3735
  this.callKit.logger.warn("server not confirm start", {
3689
3736
  caller: "Call.callStart",
3690
3737
  content: {
3691
- startConfirm: this.callKit.socket.satrtConfirm
3738
+ startConfirm: this.callKit.socket.startConfirm
3692
3739
  }
3693
3740
  });
3694
3741
  return;
@@ -3810,14 +3857,14 @@ var WebCall = (() => {
3810
3857
  // package.json
3811
3858
  var package_default = {
3812
3859
  name: "@koi-design/callkit",
3813
- version: "2.0.4",
3860
+ version: "2.0.5-beta.10",
3814
3861
  description: "callkit",
3815
3862
  author: "koi",
3816
3863
  license: "ISC",
3817
3864
  scripts: {
3818
3865
  build: "tsup",
3819
- "build:watch": "tsup --watch",
3820
- dev: "vite",
3866
+ start: "vite",
3867
+ dev: "tsup --watch",
3821
3868
  lint: "eslint -c ../../.eslintrc.js --ext .jsx,.js,.tsx,.ts ./package --fix",
3822
3869
  release: "tsup && node scripts/pkg.js"
3823
3870
  },
@@ -3874,6 +3921,10 @@ var WebCall = (() => {
3874
3921
  audioRef: void 0,
3875
3922
  constrains: constrainsDefault,
3876
3923
  socket: "",
3924
+ reconnect: {
3925
+ sip: SIP_RECONNECT_CONFIG,
3926
+ incall: SOCKET_RECONNECT_CONFIG
3927
+ },
3877
3928
  userInfo: {
3878
3929
  wsUrl: "",
3879
3930
  logGather: false,
@@ -3958,7 +4009,16 @@ var WebCall = (() => {
3958
4009
  return true;
3959
4010
  }
3960
4011
  getTrackLogsConfig() {
3961
- return this.config?.trackLogs ?? trackLogsDefaultConfig;
4012
+ return {
4013
+ ...trackLogsDefaultConfig,
4014
+ ...this.config?.trackLogs
4015
+ };
4016
+ }
4017
+ getReconnectConfig(type) {
4018
+ const config = this.config?.reconnect?.[type] ?? (type === "sip" ? SIP_RECONNECT_CONFIG : SOCKET_RECONNECT_CONFIG);
4019
+ return {
4020
+ ...config
4021
+ };
3962
4022
  }
3963
4023
  enableTrackLogs(enabled) {
3964
4024
  this.config.trackLogs.enabled = enabled;
@@ -4084,16 +4144,22 @@ var WebCall = (() => {
4084
4144
  }
4085
4145
  catchLog(msg, extra, level) {
4086
4146
  const now = /* @__PURE__ */ new Date();
4147
+ const { enabled } = this.callKit.config.getTrackLogsConfig();
4148
+ const { userInfo } = this.callKit.config.getConfig();
4149
+ const content = {
4150
+ agentId: userInfo?.agentId,
4151
+ sessionId: userInfo?.sessionId,
4152
+ ...extra?.content ?? {}
4153
+ };
4087
4154
  const log = {
4088
4155
  timestamp: now.toLocaleString().replace("T", " ").replace(".000Z", ""),
4089
4156
  level,
4090
4157
  message: msg,
4091
4158
  caller: extra?.caller,
4092
4159
  type: extra?.type,
4093
- content: extra?.content ?? {}
4160
+ content
4094
4161
  };
4095
4162
  const logString = transformLog(log);
4096
- const { enabled } = this.callKit.config.getTrackLogsConfig();
4097
4163
  if (enabled) {
4098
4164
  this.pendingTrackLogs.push(logString);
4099
4165
  }
@@ -18519,10 +18585,6 @@ var WebCall = (() => {
18519
18585
  };
18520
18586
 
18521
18587
  // core/connect.ts
18522
- var DEFAULT_RECONNECT_CONFIG = {
18523
- maxAttempts: 3,
18524
- delay: 500
18525
- };
18526
18588
  var MAX_HEARTBEAT_COUNT = 6;
18527
18589
  function convertObjectStringToJSON(input) {
18528
18590
  const corrected = input.replace(/(\w+):\s*'(.*?)'/g, '"$1": "$2"').replace(/'/g, '"');
@@ -18554,10 +18616,6 @@ var WebCall = (() => {
18554
18616
  };
18555
18617
  var Connect = class {
18556
18618
  callKit;
18557
- /**
18558
- *@description Reconnect config
18559
- */
18560
- reconnectConfig;
18561
18619
  /**
18562
18620
  *@description Whether muted
18563
18621
  */
@@ -18597,14 +18655,26 @@ var WebCall = (() => {
18597
18655
  hasInvite = false;
18598
18656
  constructor(callKit) {
18599
18657
  this.callKit = callKit;
18600
- const { reconnect = {} } = this.callKit.config.getConfig();
18601
- this.reconnectConfig = {
18602
- ...DEFAULT_RECONNECT_CONFIG,
18603
- ...reconnect
18604
- };
18658
+ }
18659
+ get reconnectConfig() {
18660
+ return this.callKit.config.getReconnectConfig("sip");
18605
18661
  }
18606
18662
  // current call id for invite data
18607
18663
  currentCallId = null;
18664
+ getCurrentCallId() {
18665
+ return this.currentCallId;
18666
+ }
18667
+ setOutgoing(outgoing) {
18668
+ if (this.isOutgoing === outgoing)
18669
+ return;
18670
+ this.callKit.logger.info("setOutgoing", {
18671
+ caller: "Connect.setOutgoing",
18672
+ content: {
18673
+ outgoing
18674
+ }
18675
+ });
18676
+ this.isOutgoing = outgoing;
18677
+ }
18608
18678
  setCallId(callId) {
18609
18679
  this.callKit.logger.info("setCallId", {
18610
18680
  caller: "Connect.setCallId",
@@ -18616,6 +18686,9 @@ var WebCall = (() => {
18616
18686
  this.callKit.trigger(KitEvent.KIT_CALL_ID_CHANGE, callId);
18617
18687
  }
18618
18688
  async reset() {
18689
+ this.setOutgoing(false);
18690
+ this.isUnprompted = false;
18691
+ this.hasInvite = false;
18619
18692
  if (this.isHolding()) {
18620
18693
  await this.setHold(false);
18621
18694
  }
@@ -18635,9 +18708,6 @@ var WebCall = (() => {
18635
18708
  this.mediaStream = void 0;
18636
18709
  this.userAgent = void 0;
18637
18710
  this.registerer = void 0;
18638
- this.isOutgoing = false;
18639
- this.isUnprompted = false;
18640
- this.hasInvite = false;
18641
18711
  if (this.mediaStream) {
18642
18712
  try {
18643
18713
  closeStream(this.mediaStream);
@@ -18959,7 +19029,8 @@ var WebCall = (() => {
18959
19029
  caller: "Connect.register.onInvite",
18960
19030
  content: {
18961
19031
  invite,
18962
- isRegistered: this.isRegistered()
19032
+ isRegistered: this.isRegistered(),
19033
+ isOutgoing: this.isOutgoing
18963
19034
  }
18964
19035
  });
18965
19036
  this.currentSession = invite;
@@ -19096,9 +19167,7 @@ var WebCall = (() => {
19096
19167
  version: `${this.callKit.config.getConfig().version}`
19097
19168
  }
19098
19169
  });
19099
- await this.registerer.register().then(() => {
19100
- this.callKit.socket.send(SocketSendEvent.START);
19101
- }).catch(async (err) => {
19170
+ await this.registerer.register().catch(async (err) => {
19102
19171
  this.callKit.reset();
19103
19172
  this.callKit.logger.error(err?.message, {
19104
19173
  caller: "Connect.register",
@@ -19110,9 +19179,8 @@ var WebCall = (() => {
19110
19179
  });
19111
19180
  },
19112
19181
  onDisconnect: (error) => {
19113
- console.log("onDisconnect", error);
19114
19182
  if (error) {
19115
- this.callKit.logger.info("SIP User Agent Disconnected with error", {
19183
+ this.callKit.logger.warn("SIP User Agent Disconnected with error", {
19116
19184
  caller: "Connect.register",
19117
19185
  type: "SIP",
19118
19186
  content: {
@@ -19122,7 +19190,7 @@ var WebCall = (() => {
19122
19190
  });
19123
19191
  this.startReconnectTimer();
19124
19192
  } else {
19125
- this.callKit.logger.info("SIP User Agent Disconnected", {
19193
+ this.callKit.logger.warn("SIP User Agent Disconnected", {
19126
19194
  caller: "Connect.register",
19127
19195
  type: "SIP",
19128
19196
  content: {}
@@ -19226,14 +19294,15 @@ var WebCall = (() => {
19226
19294
  return;
19227
19295
  }
19228
19296
  await this.registerer.unregister({ all: true }).catch((err) => {
19229
- this.callKit.reset();
19230
- this.callKit.logger.error(err, {
19297
+ this.callKit.logger.warn(err, {
19231
19298
  caller: "Connect.unregister",
19232
19299
  type: "SIP",
19233
19300
  content: {
19234
19301
  errCode: ErrorCode.WEBRTC_CANCEL_REGISTER_ERROR
19235
19302
  }
19236
19303
  });
19304
+ }).finally(() => {
19305
+ this.setRegister(false);
19237
19306
  });
19238
19307
  await this.userAgent?.stop().catch((err) => {
19239
19308
  this.callKit.logger.warn(err, {
@@ -19253,7 +19322,7 @@ var WebCall = (() => {
19253
19322
  callback
19254
19323
  }
19255
19324
  });
19256
- this.isOutgoing = true;
19325
+ this.setOutgoing(true);
19257
19326
  if (!this.isRegistered()) {
19258
19327
  await this.register();
19259
19328
  }
@@ -19267,6 +19336,8 @@ var WebCall = (() => {
19267
19336
  * @param register
19268
19337
  */
19269
19338
  setRegister(register) {
19339
+ if (this.isRegister === register)
19340
+ return;
19270
19341
  this.callKit.logger.info("connect setRegister", {
19271
19342
  caller: "Connect.setRegister",
19272
19343
  type: "SIP",
@@ -19308,7 +19379,7 @@ var WebCall = (() => {
19308
19379
  connectStatus: this.connectStatus
19309
19380
  }
19310
19381
  });
19311
- this.isOutgoing = false;
19382
+ this.setOutgoing(false);
19312
19383
  this.isUnprompted = isUnprompted;
19313
19384
  this.setHold(false);
19314
19385
  this.setMute(false);
@@ -19336,6 +19407,7 @@ var WebCall = (() => {
19336
19407
  }
19337
19408
  this.setConnectStatus(CallStatus.init);
19338
19409
  this.callKit.trigger(KitEvent.CALL_END, /* @__PURE__ */ new Date());
19410
+ this.setCallId(null);
19339
19411
  } catch (err) {
19340
19412
  this.callKit.trigger(KitEvent.CALL_END, /* @__PURE__ */ new Date());
19341
19413
  this.callKit.reset();
@@ -19479,33 +19551,46 @@ var WebCall = (() => {
19479
19551
  };
19480
19552
 
19481
19553
  // core/socket.ts
19482
- var RECONNECT_CONFIG = {
19483
- enabled: true,
19484
- maxAttempts: 3,
19485
- delay: 1e3,
19486
- pingInterval: 3e4,
19487
- pingTimeout: 5e3
19488
- };
19489
19554
  var Socket = class {
19490
19555
  callKit;
19491
19556
  ws;
19492
- socketConfig;
19493
19557
  lastPingTime = void 0;
19494
- isConnected = false;
19495
19558
  pingTimer;
19496
- // Whether received start confirmation
19497
- satrtConfirm = false;
19559
+ /**
19560
+ * @description reconnect timer
19561
+ */
19498
19562
  reconnectTimer;
19499
- isReconnecting = false;
19563
+ /**
19564
+ * @description reconnect attempts
19565
+ */
19500
19566
  reconnectAttempts = 0;
19501
- socketError = false;
19567
+ /**
19568
+ * @description connect auth state
19569
+ * @default {
19570
+ * startConfirm: false,
19571
+ * isConnected: false,
19572
+ * isReconnecting: false,
19573
+ * isAuthenticated: false,
19574
+ * isError: false
19575
+ * }
19576
+ */
19577
+ connectAuthState = {
19578
+ startConfirm: false,
19579
+ isConnected: false,
19580
+ isReconnecting: false,
19581
+ isError: false
19582
+ };
19583
+ get startConfirm() {
19584
+ return this.connectAuthState.startConfirm;
19585
+ }
19586
+ get isError() {
19587
+ return this.connectAuthState.isError;
19588
+ }
19502
19589
  constructor(callKit) {
19503
19590
  this.callKit = callKit;
19504
- const { reconnect } = this.callKit.config.getConfig();
19505
- this.socketConfig = {
19506
- ...RECONNECT_CONFIG,
19507
- ...reconnect
19508
- };
19591
+ }
19592
+ get reconnectConfig() {
19593
+ return this.callKit.config.getReconnectConfig("incall");
19509
19594
  }
19510
19595
  init() {
19511
19596
  const { socket } = this.callKit.config.getConfig();
@@ -19518,19 +19603,25 @@ var WebCall = (() => {
19518
19603
  });
19519
19604
  this.connect(socket);
19520
19605
  }
19606
+ setConnectAuthState(key, value) {
19607
+ if (this.connectAuthState[key] === value)
19608
+ return;
19609
+ this.connectAuthState[key] = value;
19610
+ }
19521
19611
  handleDisconnect() {
19522
- this.isConnected = false;
19523
- if (!this.callKit.config.isLogin() || !this.socketConfig.enabled) {
19524
- this.reset();
19612
+ this.setConnectAuthState("isConnected", false);
19613
+ const { enabled } = this.reconnectConfig;
19614
+ if (!this.callKit.config.isLogin() || !enabled) {
19615
+ this.callKit.reset();
19525
19616
  this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
19526
19617
  event: "INCALL_NOT_CONNECTED"
19527
19618
  });
19528
19619
  return;
19529
19620
  }
19530
- if (this.socketError) {
19621
+ if (this.connectAuthState.isReconnecting) {
19531
19622
  return;
19532
19623
  }
19533
- if (this.isReconnecting) {
19624
+ if (this.connectAuthState.isError) {
19534
19625
  return;
19535
19626
  }
19536
19627
  this.attemptReconnect();
@@ -19551,7 +19642,7 @@ var WebCall = (() => {
19551
19642
  });
19552
19643
  }
19553
19644
  this.ws = void 0;
19554
- this.isConnected = false;
19645
+ this.setConnectAuthState("isConnected", false);
19555
19646
  }
19556
19647
  connect(socketUrl) {
19557
19648
  if (this.ws) {
@@ -19569,11 +19660,12 @@ var WebCall = (() => {
19569
19660
  type: "INCALL",
19570
19661
  content: { ev }
19571
19662
  });
19572
- this.socketError = false;
19573
- this.isConnected = true;
19663
+ this.setConnectAuthState("isConnected", true);
19574
19664
  this.lastPingTime = Date.now();
19575
19665
  this.checkPing();
19576
- if (this.isReconnecting) {
19666
+ this.send(SocketSendEvent.START);
19667
+ if (this.connectAuthState.isReconnecting) {
19668
+ this.setConnectAuthState("isReconnecting", false);
19577
19669
  this.callKit.logger.info("reconnect success", {
19578
19670
  caller: "Socket.onOpen",
19579
19671
  type: "INCALL",
@@ -19586,15 +19678,32 @@ var WebCall = (() => {
19586
19678
  event: "INCALL_RECONNECT_SUCCESS"
19587
19679
  });
19588
19680
  }
19589
- this.resetReconnectState();
19590
19681
  }
19591
- resetReconnectState() {
19592
- this.isReconnecting = false;
19682
+ cleanReconnectState() {
19593
19683
  this.reconnectAttempts = 0;
19594
19684
  if (this.reconnectTimer) {
19595
19685
  clearTimeout(this.reconnectTimer);
19596
19686
  this.reconnectTimer = void 0;
19597
19687
  }
19688
+ this.setConnectAuthState("isReconnecting", false);
19689
+ this.setConnectAuthState("isError", false);
19690
+ }
19691
+ resetConnectState() {
19692
+ this.connectAuthState = {
19693
+ startConfirm: false,
19694
+ isConnected: false,
19695
+ isReconnecting: false,
19696
+ isError: false
19697
+ };
19698
+ this.cleanReconnectState();
19699
+ this.callKit.logger.info("reset connect state", {
19700
+ caller: "Socket.resetConnectState",
19701
+ type: "INCALL",
19702
+ content: {
19703
+ reconnectAttempts: this.reconnectAttempts,
19704
+ connectAuthState: this.connectAuthState
19705
+ }
19706
+ });
19598
19707
  }
19599
19708
  onClose(ev) {
19600
19709
  this.callKit.logger.info("socket onClose", {
@@ -19605,7 +19714,6 @@ var WebCall = (() => {
19605
19714
  this.handleDisconnect();
19606
19715
  }
19607
19716
  onError(ev) {
19608
- this.socketError = true;
19609
19717
  this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
19610
19718
  event: "INCALL_CONNECT_ERROR",
19611
19719
  err: ev
@@ -19644,7 +19752,7 @@ var WebCall = (() => {
19644
19752
  });
19645
19753
  content = data.data;
19646
19754
  }
19647
- this.callKit.logger.info("socket onMessage", {
19755
+ this.callKit.logger.info(`socket onMessage: ${data.event}`, {
19648
19756
  caller: "Socket.onMessage",
19649
19757
  type: "INCALL",
19650
19758
  content: {
@@ -19653,178 +19761,105 @@ var WebCall = (() => {
19653
19761
  }
19654
19762
  });
19655
19763
  this.confirmAck(data);
19764
+ const callUuid = content?.callUuid || "";
19656
19765
  if (data.event === SocketReceiveEvent.PONG) {
19657
19766
  this.lastPingTime = Date.now();
19658
19767
  return;
19659
19768
  }
19660
19769
  if (data.event === SocketReceiveEvent.START_CONFIRM) {
19661
- this.callKit.logger.info("start confirm success", {
19662
- caller: "Socket.onMessage",
19663
- type: "INCALL",
19664
- content: {
19665
- data: content,
19666
- event: SocketReceiveEvent.START_CONFIRM
19667
- }
19668
- });
19669
- this.satrtConfirm = true;
19670
- }
19671
- if (data.event === SocketReceiveEvent.CALL_SUCCESS) {
19672
- this.callKit.logger.info("call success", {
19673
- caller: "Socket.onMessage",
19674
- type: "INCALL",
19675
- content: {
19676
- data: content,
19677
- event: SocketReceiveEvent.CALL_SUCCESS
19678
- }
19679
- });
19680
- }
19681
- if (data.event === SocketReceiveEvent.CALL_FAILED) {
19682
- this.callKit.logger.info(data.msg, {
19683
- caller: "Socket.onMessage",
19684
- type: "INCALL",
19685
- content: {
19686
- data: content,
19687
- errCode: ErrorCode.SOCKET_CALL_ERROR
19688
- }
19689
- });
19770
+ this.setConnectAuthState("startConfirm", true);
19771
+ this.cleanReconnectState();
19690
19772
  }
19691
19773
  if (data.event === SocketReceiveEvent.CUSTOMER_RINGING) {
19692
- this.callKit.trigger(KitEvent.CALL_RINGING, /* @__PURE__ */ new Date());
19693
- this.callKit.logger.info(data.msg, {
19694
- caller: `Socket.onMessage:${data.event}`,
19695
- type: "INCALL",
19696
- content: {
19697
- data: content,
19698
- event: SocketReceiveEvent.CUSTOMER_RINGING
19699
- }
19774
+ this.callKit.trigger(KitEvent.CALL_RINGING, {
19775
+ time: /* @__PURE__ */ new Date(),
19776
+ callUuid
19700
19777
  });
19701
19778
  }
19702
19779
  if (data.event === SocketReceiveEvent.CUSTOMER_PICK_UP) {
19703
- this.callKit.logger.info(data.msg, {
19704
- caller: "Socket.onMessage",
19705
- type: "INCALL",
19706
- content: {
19707
- data: content,
19708
- event: SocketReceiveEvent.CUSTOMER_PICK_UP
19709
- }
19780
+ this.callKit.trigger(KitEvent.CALL_PICK_UP, {
19781
+ time: /* @__PURE__ */ new Date(),
19782
+ callUuid
19710
19783
  });
19711
- this.callKit.trigger(KitEvent.CALL_PICK_UP, /* @__PURE__ */ new Date());
19712
19784
  }
19713
19785
  if (data.event === SocketReceiveEvent.AGENT_PICK_UP) {
19714
- this.callKit.logger.info(data.msg, {
19715
- caller: "Socket.onMessage",
19716
- type: "INCALL",
19717
- content: {
19718
- data: content,
19719
- event: SocketReceiveEvent.AGENT_PICK_UP
19720
- }
19786
+ this.callKit.trigger(KitEvent.AGENT_PICK_UP, {
19787
+ time: /* @__PURE__ */ new Date(),
19788
+ callUuid
19721
19789
  });
19722
- this.callKit.trigger(KitEvent.AGENT_PICK_UP, /* @__PURE__ */ new Date());
19723
19790
  }
19724
19791
  if (data.event === SocketReceiveEvent.CUSTOMER_HANG_UP) {
19725
- this.callKit.logger.info(data.msg, {
19726
- caller: `Socket.onMessage:${data.event}`,
19727
- type: "INCALL",
19728
- content: {
19729
- data: content,
19730
- event: SocketReceiveEvent.CUSTOMER_HANG_UP
19731
- }
19792
+ this.callKit.trigger(KitEvent.CALL_HANG_UP, {
19793
+ time: /* @__PURE__ */ new Date(),
19794
+ callUuid
19732
19795
  });
19733
- this.callKit.trigger(KitEvent.CALL_HANG_UP, /* @__PURE__ */ new Date());
19734
- if (content?.callUuid) {
19735
- this.callKit.connect.socketTriggerHangup(content.callUuid);
19796
+ if (callUuid) {
19797
+ this.callKit.connect.socketTriggerHangup(callUuid);
19736
19798
  }
19737
19799
  }
19738
19800
  if (data.event === SocketReceiveEvent.CUSTOMER_NO_ANSWER) {
19739
- this.callKit.logger.info(data.msg, {
19740
- caller: "Socket.onMessage",
19741
- type: "INCALL",
19742
- content: {
19743
- data: content,
19744
- event: SocketReceiveEvent.CUSTOMER_NO_ANSWER
19745
- }
19801
+ this.callKit.trigger(KitEvent.CALL_NO_ANSWER, {
19802
+ time: /* @__PURE__ */ new Date(),
19803
+ callUuid
19746
19804
  });
19747
- this.callKit.trigger(KitEvent.CALL_NO_ANSWER);
19748
- if (content?.callUuid) {
19749
- this.callKit.connect.socketTriggerHangup(content.callUuid);
19805
+ if (callUuid) {
19806
+ this.callKit.connect.socketTriggerHangup(callUuid);
19750
19807
  }
19751
19808
  }
19752
19809
  if (data.event === SocketReceiveEvent.CALL_CDR) {
19753
- this.callKit.logger.info(data.msg, {
19754
- caller: `Socket.onMessage:${data.event}`,
19755
- type: "INCALL",
19756
- content: {
19757
- data: content,
19758
- event: SocketReceiveEvent.CALL_CDR
19759
- }
19760
- });
19761
- this.callKit.trigger(KitEvent.CALL_CDR, content);
19762
- }
19763
- if (data.event === SocketReceiveEvent.STOP_CONFIRM) {
19764
- this.callKit.logger.info(data.msg, {
19765
- caller: `Socket.onMessage:${data.event}`,
19766
- type: "INCALL",
19767
- content: {
19768
- data: content,
19769
- event: SocketReceiveEvent.STOP_CONFIRM
19770
- }
19810
+ this.callKit.trigger(KitEvent.CALL_CDR, {
19811
+ time: /* @__PURE__ */ new Date(),
19812
+ callUuid,
19813
+ ...content
19771
19814
  });
19772
19815
  }
19773
19816
  if (data.event === SocketReceiveEvent.CLOSE) {
19774
19817
  const { userInfo } = this.callKit.config.getConfig();
19775
- this.callKit.logger.info(data.msg, {
19776
- caller: `Socket.onMessage:${data.event}`,
19777
- type: "INCALL",
19778
- content: {
19779
- data: content,
19780
- event: SocketReceiveEvent.CLOSE
19781
- }
19782
- });
19783
- this.send(SocketSendEvent.END, {
19784
- agentId: userInfo.agentId
19785
- });
19818
+ this.send(SocketSendEvent.END, { agentId: userInfo.agentId, callUuid });
19786
19819
  }
19787
19820
  if (data.event === SocketReceiveEvent.ERROR) {
19788
- this.socketError = true;
19821
+ this.setConnectAuthState("isError", true);
19789
19822
  this.callKit.reset();
19790
19823
  this.callKit.logger.error(data.msg, {
19791
19824
  caller: `Socket.onMessage:${data.event}`,
19792
19825
  type: "INCALL",
19793
19826
  content: {
19794
19827
  errCode: ErrorCode.SOKET_SERVER_ERROR,
19795
- data: content
19828
+ data: content,
19829
+ callUuid
19796
19830
  }
19797
19831
  });
19798
19832
  }
19799
19833
  if (data.event === SocketReceiveEvent.SESSION_ERROR) {
19800
- this.socketError = true;
19834
+ this.setConnectAuthState("isError", true);
19801
19835
  this.callKit.reset();
19802
19836
  this.callKit.logger.error(data.msg, {
19803
19837
  caller: `Socket.onMessage:${data.event}`,
19804
19838
  type: "INCALL",
19805
19839
  content: {
19806
19840
  data: content,
19807
- event: SocketReceiveEvent.SESSION_ERROR
19841
+ event: SocketReceiveEvent.SESSION_ERROR,
19842
+ callUuid
19808
19843
  }
19809
19844
  });
19810
19845
  }
19811
19846
  if (data.event === SocketReceiveEvent.AGENT_NO_ANSWER) {
19812
- this.callKit.logger.info(data.msg, {
19813
- caller: `Socket.onMessage:${data.event}`,
19814
- type: "INCALL",
19815
- content: {
19816
- data: content,
19817
- event: SocketReceiveEvent.AGENT_NO_ANSWER
19818
- }
19819
- });
19820
- if (content?.callUuid) {
19821
- this.callKit.connect.socketTriggerHangup(content.callUuid);
19847
+ if (callUuid) {
19848
+ this.callKit.connect.socketTriggerHangup(callUuid);
19822
19849
  }
19823
19850
  }
19824
- this.callKit.trigger(KitEvent.SERVER_SOCKET_EVENT, data);
19851
+ if (data.event === SocketReceiveEvent.AGENT_HANG_UP) {
19852
+ if (callUuid) {
19853
+ this.callKit.connect.socketTriggerHangup(callUuid);
19854
+ }
19855
+ }
19856
+ this.callKit.trigger(KitEvent.SERVER_SOCKET_EVENT, {
19857
+ ...data,
19858
+ callUuid
19859
+ });
19825
19860
  }
19826
19861
  send(event, message) {
19827
- if (!this.isConnected) {
19862
+ if (!this.connectAuthState.isConnected) {
19828
19863
  this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
19829
19864
  event: "INCALL_NOT_CONNECTED"
19830
19865
  });
@@ -19883,23 +19918,16 @@ var WebCall = (() => {
19883
19918
  }
19884
19919
  }
19885
19920
  ping() {
19886
- if (!this.isConnected)
19921
+ if (!this.connectAuthState.isConnected)
19887
19922
  return;
19888
19923
  this.send(SocketSendEvent.PING);
19889
- this.callKit.logger.info(`socket ping`, {
19890
- caller: "Socket.ping",
19891
- type: "INCALL",
19892
- content: {
19893
- lastPingTime: this.lastPingTime
19894
- }
19895
- });
19896
19924
  const now = Date.now();
19897
- const { pingInterval, pingTimeout } = this.socketConfig;
19925
+ const { pingInterval, pingTimeout } = this.reconnectConfig;
19898
19926
  if (now - this.lastPingTime > pingInterval + pingTimeout) {
19899
- if (this.ws && this.isConnected) {
19927
+ if (this.ws && this.connectAuthState.isConnected) {
19900
19928
  this.ws.close(4001, "ping timeout");
19901
19929
  } else {
19902
- this.reset();
19930
+ this.callKit.reset();
19903
19931
  }
19904
19932
  this.callKit.logger.error("socket ping timeout", {
19905
19933
  caller: "Socket.ping",
@@ -19915,21 +19943,28 @@ var WebCall = (() => {
19915
19943
  clearInterval(this.pingTimer);
19916
19944
  }
19917
19945
  this.ping();
19946
+ const { pingInterval } = this.reconnectConfig;
19918
19947
  this.pingTimer = setInterval(() => {
19919
19948
  this.ping();
19920
- }, this.socketConfig.pingInterval);
19949
+ }, pingInterval);
19921
19950
  }
19922
19951
  /**
19923
19952
  * reset socket connection and all states
19924
19953
  */
19925
- async reset() {
19954
+ async reset(config) {
19955
+ const { force = false } = config || {};
19926
19956
  if (this.pingTimer) {
19927
19957
  clearInterval(this.pingTimer);
19928
19958
  this.pingTimer = void 0;
19929
19959
  }
19930
- this.resetReconnectState();
19960
+ if (force) {
19961
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
19962
+ event: "INCALL_RESET"
19963
+ });
19964
+ this.resetConnectState();
19965
+ }
19931
19966
  this.lastPingTime = void 0;
19932
- this.satrtConfirm = false;
19967
+ this.setConnectAuthState("startConfirm", false);
19933
19968
  this.clearWebSocket();
19934
19969
  }
19935
19970
  attemptReconnect() {
@@ -19937,11 +19972,12 @@ var WebCall = (() => {
19937
19972
  clearTimeout(this.reconnectTimer);
19938
19973
  this.reconnectTimer = void 0;
19939
19974
  }
19940
- if (this.reconnectAttempts >= this.socketConfig.maxAttempts) {
19975
+ const { maxAttempts } = this.reconnectConfig;
19976
+ if (this.reconnectAttempts >= maxAttempts) {
19941
19977
  this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
19942
19978
  event: "INCALL_RECONNECT_ERROR"
19943
19979
  });
19944
- this.reset();
19980
+ this.callKit.reset();
19945
19981
  this.callKit.logger.error("Maximum reconnection attempts reached", {
19946
19982
  caller: "Socket.attemptReconnect",
19947
19983
  type: "INCALL",
@@ -19957,17 +19993,17 @@ var WebCall = (() => {
19957
19993
  event: "INCALL_RECONNECT_START"
19958
19994
  });
19959
19995
  }
19960
- this.isReconnecting = true;
19996
+ this.setConnectAuthState("isReconnecting", true);
19961
19997
  this.reconnectAttempts += 1;
19962
- const { delay } = this.socketConfig;
19998
+ const { delay } = this.reconnectConfig;
19963
19999
  this.callKit.logger.info(
19964
- `Preparing reconnection attempt ${this.reconnectAttempts}/${this.socketConfig.maxAttempts}, delay: ${delay}ms`,
20000
+ `Preparing reconnection attempt ${this.reconnectAttempts}/${maxAttempts}, delay: ${delay}ms`,
19965
20001
  {
19966
20002
  caller: "Socket.attemptReconnect",
19967
20003
  type: "INCALL",
19968
20004
  content: {
19969
20005
  reconnectAttempts: this.reconnectAttempts,
19970
- maxAttempts: this.socketConfig.maxAttempts,
20006
+ maxAttempts,
19971
20007
  delay
19972
20008
  }
19973
20009
  }
@@ -19994,17 +20030,34 @@ var WebCall = (() => {
19994
20030
  this.connect = new Connect(this);
19995
20031
  this.callCenter = new Call(this);
19996
20032
  this.socket = new Socket(this);
19997
- this.config.setConfig("log", options.log);
19998
- this.config.setConfig("trackLogs", options.trackLogs);
19999
- this.config.setConfig("audioRef", options.audioRef);
20000
- this.config.setConfig("host", options.host);
20033
+ this.logger = new Logger(this, options.log);
20034
+ if (options.log) {
20035
+ this.config.setConfig("log", options.log);
20036
+ }
20037
+ if (options.trackLogs) {
20038
+ this.config.setConfig("trackLogs", {
20039
+ ...trackLogsDefaultConfig,
20040
+ ...options.trackLogs
20041
+ });
20042
+ }
20043
+ if (options.audioRef) {
20044
+ this.config.setConfig("audioRef", options.audioRef);
20045
+ }
20046
+ if (options.host) {
20047
+ this.config.setConfig("host", options.host);
20048
+ }
20001
20049
  this.config.setConfig(
20002
20050
  "constrains",
20003
20051
  options.constrains || constrainsDefault
20004
20052
  );
20005
20053
  this.config.setConfig("socket", options.socket);
20006
- this.config.setConfig("reconnect", options.reconnect);
20007
- this.logger = new Logger(this, options.log);
20054
+ this.config.setConfig("reconnect", {
20055
+ sip: { ...SIP_RECONNECT_CONFIG, ...options.reconnect?.sip || {} },
20056
+ incall: {
20057
+ ...SOCKET_RECONNECT_CONFIG,
20058
+ ...options.reconnect?.incall || {}
20059
+ }
20060
+ });
20008
20061
  this.logger.info("callKit init", {
20009
20062
  caller: "CallKit.init",
20010
20063
  content: options
@@ -20042,10 +20095,18 @@ var WebCall = (() => {
20042
20095
  encryptionPassword
20043
20096
  }
20044
20097
  });
20098
+ if (this.socket.isError) {
20099
+ this.logger.warn("socket is error", {
20100
+ caller: "CallKit.login",
20101
+ content: { username, password, extra, socketError: this.socket.isError }
20102
+ });
20103
+ return;
20104
+ }
20045
20105
  try {
20046
20106
  const user = await this.api.login({
20047
20107
  userName: username,
20048
- password: encryptionPassword
20108
+ password: encryptionPassword,
20109
+ timestamp: Date.now()
20049
20110
  });
20050
20111
  if (user) {
20051
20112
  this.config.setConfig("userInfo", {
@@ -20091,9 +20152,9 @@ var WebCall = (() => {
20091
20152
  if (this.config.isLogin()) {
20092
20153
  const { sessionId } = userInfo;
20093
20154
  try {
20094
- await this.api.loginOut({ sessionId });
20155
+ await this.api.loginOut({ sessionId, timestamp: Date.now() });
20095
20156
  } catch (error) {
20096
- this.logger.error(error, {
20157
+ this.logger.warn(error, {
20097
20158
  caller: "CallKit.logout",
20098
20159
  content: {
20099
20160
  errCode: ErrorCode.API_USER_LOGOUT_ERROR
@@ -20103,8 +20164,9 @@ var WebCall = (() => {
20103
20164
  }
20104
20165
  if (isReset) {
20105
20166
  await this.reset();
20167
+ } else {
20168
+ this.config.reset();
20106
20169
  }
20107
- this.trigger(KitEvent.KIT_LOGIN_CHANGE, false);
20108
20170
  }
20109
20171
  async call(extno = "", options = {
20110
20172
  sourceType: CallSourceType.phoneNum,
@@ -20169,9 +20231,6 @@ var WebCall = (() => {
20169
20231
  });
20170
20232
  this.connect.unregister();
20171
20233
  }
20172
- async stop() {
20173
- await this.connect.stop();
20174
- }
20175
20234
  async hangup() {
20176
20235
  if (!this.config.check())
20177
20236
  return;
@@ -20240,7 +20299,7 @@ var WebCall = (() => {
20240
20299
  * set userstatus
20241
20300
  * @param status
20242
20301
  */
20243
- async setUserStatus(status) {
20302
+ async setUserStatus(status, extra = {}) {
20244
20303
  const { agentId } = this.config.getConfig().userInfo;
20245
20304
  this.logger.info("setUserStatus", {
20246
20305
  caller: "CallKit.setUserStatus",
@@ -20251,14 +20310,24 @@ var WebCall = (() => {
20251
20310
  });
20252
20311
  await this.api.updateUserStatus({
20253
20312
  agentId,
20254
- userStatus: status
20313
+ userStatus: status,
20314
+ timestamp: Date.now(),
20315
+ ...extra
20255
20316
  });
20256
20317
  }
20257
- async reset() {
20318
+ /**
20319
+ * reset callkit
20320
+ * @description recover the callkit to the initial state
20321
+ * @default force is false
20322
+ * @param config.force is true, the callkit reset socket connection and all states
20323
+ */
20324
+ async reset(config) {
20325
+ const { force = false } = config || {};
20258
20326
  this.logger.info("reset", {
20259
20327
  caller: "CallKit.reset",
20260
20328
  content: {
20261
- connectStatus: this.connect.connectStatus
20329
+ connectStatus: this.connect.connectStatus,
20330
+ force
20262
20331
  }
20263
20332
  });
20264
20333
  if (this.connect.isCalling()) {
@@ -20267,9 +20336,10 @@ var WebCall = (() => {
20267
20336
  await this.connect.reset();
20268
20337
  if (this.config.isLogin()) {
20269
20338
  await this.logout({ isReset: false });
20339
+ } else {
20340
+ await this.config.reset();
20270
20341
  }
20271
- await this.config.reset();
20272
- await this.socket.reset();
20342
+ await this.socket.reset({ force });
20273
20343
  }
20274
20344
  on(event, callback) {
20275
20345
  this.listener.push({