@cimulate/copilot-sdk 2.0.5 → 2.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.
package/src/copilot.ts CHANGED
@@ -20,13 +20,17 @@ import { asyncGenerator } from "./generator";
20
20
 
21
21
  type SocketIOOptions = Partial<ManagerOptions & SocketOptions>;
22
22
 
23
- interface CimulateCopilotOptions {
24
- apiKey: string;
23
+ type ApiKeyAuth = { apiKey: string; apiToken?: never };
24
+ type ApiTokenAuth = { apiToken: string; apiKey?: never };
25
+
26
+ interface CommonOptions {
25
27
  baseUrl: string;
26
28
  namespace?: string;
27
29
  socketOptions?: Omit<SocketIOOptions, "withCredentials" | "parser">;
28
30
  }
29
31
 
32
+ type CimulateCopilotOptions = (ApiKeyAuth | ApiTokenAuth) & CommonOptions;
33
+
30
34
  const socketDefaults: SocketIOOptions = {
31
35
  path: "/api/v1/socket.io",
32
36
  autoConnect: false,
@@ -44,15 +48,30 @@ export default class CimulateCopilot<
44
48
  T extends ReturnedFields = ReturnedFields
45
49
  > {
46
50
  private socket: Socket;
51
+ private sessionIdKey: string = "x-cimulate-copilot-session-id";
47
52
 
48
53
  constructor({
49
54
  apiKey,
55
+ apiToken,
50
56
  baseUrl,
51
57
  namespace = "/copilot",
52
58
  socketOptions = {},
53
59
  }: CimulateCopilotOptions) {
60
+
61
+ if ((apiKey && apiToken) || (!apiKey && !apiToken)) {
62
+ throw new Error("Provide exactly one authentication method: either 'apiKey' or 'apiToken', but not both.");
63
+ }
64
+
54
65
  const socketioEndpoint = `${baseUrl}${namespace}`;
55
66
 
67
+ const extraHeaders: Record<string, string> = {};
68
+
69
+ if (apiKey) {
70
+ extraHeaders["x-cimulate-api-key"] = apiKey;
71
+ } else if (apiToken) {
72
+ extraHeaders["Authorization"] = apiToken;
73
+ }
74
+
56
75
  const options: SocketIOOptions[] = [
57
76
  socketDefaults,
58
77
  socketOptions,
@@ -60,11 +79,7 @@ export default class CimulateCopilot<
60
79
  parser: SnakeCaseParser,
61
80
  withCredentials: true, // enables cookies
62
81
  transportOptions: {
63
- polling: {
64
- extraHeaders: {
65
- "x-cimulate-api-key": apiKey,
66
- },
67
- },
82
+ polling: { extraHeaders },
68
83
  },
69
84
  },
70
85
  ];
@@ -73,7 +88,24 @@ export default class CimulateCopilot<
73
88
  }
74
89
 
75
90
  connect() {
91
+
92
+ if (this.socket.auth && this.sessionIdKey in this.socket.auth) {
93
+ this.socket.auth = {};
94
+ }
95
+
76
96
  this.socket.connect();
97
+
98
+ this.on("connect_ack", (event) => {
99
+ this.socket.auth = {
100
+ [this.sessionIdKey]: event.sessionId
101
+ };
102
+ });
103
+ }
104
+
105
+ reconnect() {
106
+ if (!this.socket.active) {
107
+ this.socket.connect();
108
+ }
77
109
  }
78
110
 
79
111
  async search(
package/src/parser.ts CHANGED
@@ -9,6 +9,8 @@ class SnakeCaseEncoder extends Encoder {
9
9
  ? [packet.data[0], encode(packet.data[1])]
10
10
  : packet.data?.length == 1
11
11
  ? [packet.data[0]]
12
+ : typeof packet.data == "object" && packet.data != null
13
+ ? packet.data
12
14
  : undefined,
13
15
  });
14
16
  }