@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/README.md +19 -7
- package/dist/{bundle.cimulate.copilot-sdk.64b1b191.esm.js → bundle.cimulate.copilot-sdk.a4db65a3.esm.js} +2 -2
- package/dist/{bundle.cimulate.copilot-sdk.a211efa0.umd.js.map → bundle.cimulate.copilot-sdk.a4db65a3.esm.js.map} +1 -1
- package/dist/{bundle.cimulate.copilot-sdk.a211efa0.umd.js → bundle.cimulate.copilot-sdk.d9a76090.umd.js} +2 -2
- package/dist/{bundle.cimulate.copilot-sdk.64b1b191.esm.js.map → bundle.cimulate.copilot-sdk.d9a76090.umd.js.map} +1 -1
- package/dist/{bundle.cimulate.copilot-sdk.79e08d38.cjs.js → bundle.cimulate.copilot-sdk.dc1f3b8d.cjs.js} +2 -2
- package/dist/{bundle.cimulate.copilot-sdk.79e08d38.cjs.js.map → bundle.cimulate.copilot-sdk.dc1f3b8d.cjs.js.map} +1 -1
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/types/copilot.d.ts +12 -2
- package/examples/callbacks.ts +2 -0
- package/examples/reconnect.ts +51 -0
- package/package.json +1 -1
- package/src/copilot.ts +39 -7
- package/src/parser.ts +2 -0
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
|
-
|
|
24
|
-
|
|
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