@cimulate/copilot-sdk 3.2.0 → 3.3.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.
@@ -5,6 +5,7 @@ import { Done } from './Done';
5
5
  import { Error } from './Error';
6
6
  import { FollowUp } from './FollowUp';
7
7
  import { Inquiry } from './Inquiry';
8
+ import { Progress } from './Progress';
8
9
  import { RefinedSearch } from './RefinedSearch';
9
10
  import { ReturnedFields } from "./ReturnedFields";
10
11
  interface CopilotEvent<TReturnedFields extends ReturnedFields = ReturnedFields> {
@@ -12,7 +13,7 @@ interface CopilotEvent<TReturnedFields extends ReturnedFields = ReturnedFields>
12
13
  sessionId: string;
13
14
  name: string;
14
15
  eventSourceId: string;
15
- data: ConnectAck | FollowUp | Inquiry | DisplayProducts<TReturnedFields> | RefinedSearch<TReturnedFields> | Done | Error;
16
+ data: ConnectAck | FollowUp | Inquiry | DisplayProducts<TReturnedFields> | RefinedSearch<TReturnedFields> | Progress | Done | Error;
16
17
  createdAt: string;
17
18
  metadata: CommonMetadata;
18
19
  }
@@ -5,6 +5,7 @@ export * from './CommonMetadata';
5
5
  export * from './CommonRequiredFields';
6
6
  export * from './ConnectAck';
7
7
  export * from './ConnectAckMetadata';
8
+ export * from './ConnectError';
8
9
  export * from './CopilotAPIEvent';
9
10
  export * from './CopilotEvent';
10
11
  export * from './CopilotSearch';
@@ -19,6 +19,10 @@ async function main() {
19
19
  console.error("Error received: ", event);
20
20
  });
21
21
 
22
+ const connectErrorHandler = copilot.on("connect_error", function errorEvent(error) {
23
+ console.error("Error message received: ", error.message);
24
+ });
25
+
22
26
  console.log("Establish a new connection");
23
27
  copilot.connect();
24
28
 
@@ -45,6 +49,7 @@ async function main() {
45
49
  console.log("De-register handler");
46
50
  copilot.off("connect_ack", connectAckHandler);
47
51
  copilot.off("error", errorHandler);
52
+ copilot.off("connect_error", connectErrorHandler);
48
53
 
49
54
  }
50
55
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cimulate/copilot-sdk",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "description": "A lightweight API client SDK for Cimulate Copilot",
5
5
  "files": [
6
6
  "dist/**",
package/src/copilot.ts CHANGED
@@ -6,6 +6,7 @@ import type {
6
6
  CancelAck,
7
7
  CommonRequiredFields,
8
8
  ConnectAck,
9
+ ConnectError,
9
10
  CopilotAPIEvent,
10
11
  CopilotEventName,
11
12
  CopilotSearch,
@@ -96,7 +97,6 @@ export default class CimulateCopilot<
96
97
  this.socket.auth = {
97
98
  [this.sessionIdKey]: event.sessionId,
98
99
  };
99
- this.reconnectAttempts = 0;
100
100
  };
101
101
 
102
102
  private readonly handleDisconnect = (reason: Socket.DisconnectReason) => {
@@ -107,24 +107,29 @@ export default class CimulateCopilot<
107
107
  }
108
108
  };
109
109
 
110
- private readonly handleConnectError = (err: Error) => {
110
+ private readonly handleConnectError = (err: ConnectError) => {
111
111
  console.error(`[Copilot SDK] Connect error: ${err.message}`);
112
112
 
113
- if (err.message === "Invalid API Key") {
113
+ const authErrors = ["InvalidApiKey", "InvalidToken", "BadRequest"];
114
+
115
+ // Use regex to extract error type
116
+ const errorMatch = err.message.match(/['"]error['"]:\s*['"](\w+)['"]/);
117
+ const errorType = errorMatch?.[1];
118
+
119
+ if (errorType && authErrors.includes(errorType)) {
114
120
  this.disconnect();
115
121
  }
116
-
117
122
  // Trigger reconnect logic with backoff
118
- if (!this.socket.connected && !this.socket.active) {
123
+ else if (!this.socket.connected && !this.socket.active) {
119
124
  console.log(`[Copilot SDK] Retrying connect attempt...`);
120
125
  this.retryConnect();
121
126
  }
122
- };
127
+ };
123
128
 
124
129
  private readonly offInternalHandlers = () => {
125
130
  this.socket.off("connect_ack", this.handleConnectAck);
126
131
  this.socket.off("disconnect", this.handleDisconnect);
127
- this.socket.off("connect_error", this.handleConnectError);
132
+ this.off("connect_error", this.handleConnectError);
128
133
  }
129
134
 
130
135
  connect() {
@@ -132,6 +137,7 @@ export default class CimulateCopilot<
132
137
  if (this.reconnectTimeout) {
133
138
  clearTimeout(this.reconnectTimeout);
134
139
  this.reconnectTimeout = undefined;
140
+ this.reconnectAttempts = 0;
135
141
  }
136
142
 
137
143
  // Clear old handlers to avoid duplicates
@@ -140,7 +146,7 @@ export default class CimulateCopilot<
140
146
  // Add fresh handlers
141
147
  this.on("connect_ack", this.handleConnectAck);
142
148
  this.socket.on("disconnect", this.handleDisconnect);
143
- this.socket.on("connect_error", this.handleConnectError);
149
+ this.on("connect_error", this.handleConnectError);
144
150
 
145
151
  // Clear old auth state
146
152
  if (this.socket.auth && this.sessionIdKey in this.socket.auth) {
@@ -186,6 +192,7 @@ export default class CimulateCopilot<
186
192
  if (this.reconnectTimeout) {
187
193
  clearTimeout(this.reconnectTimeout);
188
194
  this.reconnectTimeout = undefined;
195
+ this.reconnectAttempts = 0;
189
196
  }
190
197
 
191
198
  this.socket.connect();
@@ -313,7 +320,7 @@ export default class CimulateCopilot<
313
320
  const events = asyncGenerator<CopilotAPIEvent<T>>(({ emit, cancel }) => {
314
321
  const handler = (
315
322
  event: string,
316
- payload: Exclude<CopilotAPIEvent<T>, ConnectAck>
323
+ payload: Exclude<CopilotAPIEvent<T>, ConnectAck | ConnectError>
317
324
  ) => {
318
325
  if (payload.name == "done") cancel();
319
326
  if (payload.eventSourceId == eventSourceId) emit(payload);
@@ -0,0 +1,12 @@
1
+ import { ConnectAckMetadata } from './ConnectAckMetadata';
2
+ interface ConnectError {
3
+ sessionId: string;
4
+ id: string;
5
+ createdAt: string;
6
+ status: number;
7
+ error: string;
8
+ message: string;
9
+ metadata: ConnectAckMetadata;
10
+ name: 'connect_error';
11
+ }
12
+ export { ConnectError };
@@ -1,10 +1,16 @@
1
+ /**
2
+ This not generated automatically by the generate model script.
3
+ For any new event, manually create an entry here.
4
+ */
1
5
  import { ConnectAck } from "./ConnectAck";
6
+ import { ConnectError } from "./ConnectError";
2
7
  import { DisplayProducts } from "./DisplayProducts";
3
8
  import { Done } from "./Done";
4
9
  import { Error } from "./Error";
5
10
  import { FollowUp } from "./FollowUp";
6
11
  import { Inquiry } from "./Inquiry";
7
12
  import { PartialInquiry } from "./PartialInquiry";
13
+ import { Progress } from "./Progress";
8
14
  import { RefinedSearch } from "./RefinedSearch";
9
15
  import { ReturnedFields } from "./ReturnedFields";
10
16
 
@@ -12,12 +18,14 @@ export type CopilotAPIEvent<
12
18
  TReturnedFields extends ReturnedFields = ReturnedFields
13
19
  > =
14
20
  | ConnectAck
21
+ | ConnectError
15
22
  | DisplayProducts<TReturnedFields>
16
23
  | Done
17
24
  | Error
18
25
  | FollowUp
19
26
  | Inquiry
20
27
  | PartialInquiry
28
+ | Progress
21
29
  | RefinedSearch<TReturnedFields>;
22
30
 
23
31
  export type CopilotEventName = CopilotAPIEvent["name"];
@@ -5,6 +5,7 @@ import { Done } from './Done';
5
5
  import { Error } from './Error';
6
6
  import { FollowUp } from './FollowUp';
7
7
  import { Inquiry } from './Inquiry';
8
+ import { Progress } from './Progress';
8
9
  import { RefinedSearch } from './RefinedSearch';
9
10
  import { ReturnedFields } from "./ReturnedFields";
10
11
 
@@ -13,7 +14,7 @@ interface CopilotEvent<TReturnedFields extends ReturnedFields = ReturnedFields>
13
14
  sessionId: string;
14
15
  name: string;
15
16
  eventSourceId: string;
16
- data: ConnectAck | FollowUp | Inquiry | DisplayProducts<TReturnedFields> | RefinedSearch<TReturnedFields> | Done | Error;
17
+ data: ConnectAck | FollowUp | Inquiry | DisplayProducts<TReturnedFields> | RefinedSearch<TReturnedFields> | Progress | Done | Error;
17
18
  createdAt: string;
18
19
  metadata: CommonMetadata;
19
20
  }
@@ -5,6 +5,7 @@ export * from './CommonMetadata';
5
5
  export * from './CommonRequiredFields';
6
6
  export * from './ConnectAck';
7
7
  export * from './ConnectAckMetadata';
8
+ export * from './ConnectError';
8
9
  export * from './CopilotAPIEvent';
9
10
  export * from './CopilotEvent';
10
11
  export * from './CopilotSearch';