@cimulate/copilot-sdk 2.0.1 → 2.0.2
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/dist/bundle.cimulate.copilot-sdk.08830891.cjs.js +2 -0
- package/dist/bundle.cimulate.copilot-sdk.08830891.cjs.js.map +1 -0
- package/dist/bundle.cimulate.copilot-sdk.20fcb32a.umd.js +2 -0
- package/dist/bundle.cimulate.copilot-sdk.20fcb32a.umd.js.map +1 -0
- package/dist/bundle.cimulate.copilot-sdk.3e439753.esm.js +2 -0
- package/dist/bundle.cimulate.copilot-sdk.3e439753.esm.js.map +1 -0
- 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 +1 -1
- package/dist/types/model/customization.d.ts +7 -0
- package/dist/types/model/index.d.ts +1 -0
- package/dist/types/parser.d.ts +1 -0
- package/package.json +3 -3
- package/src/copilot.ts +217 -0
- package/src/generator.ts +56 -0
- package/src/index.ts +3 -0
- package/src/model/Cancel.ts +6 -0
- package/src/model/CancelAck.ts +10 -0
- package/src/model/CommonFields.ts +9 -0
- package/src/model/CommonMetadata.ts +7 -0
- package/src/model/CommonRequiredFields.ts +7 -0
- package/src/model/ConnectAck.ts +11 -0
- package/src/model/ConnectAckMetadata.ts +6 -0
- package/src/model/CopilotAPIEvent.ts +23 -0
- package/src/model/CopilotEvent.ts +20 -0
- package/src/model/CopilotSearch.ts +6 -0
- package/src/model/CopilotSearchAck.ts +10 -0
- package/src/model/CopilotSearchSuggestion.ts +7 -0
- package/src/model/DisplayProducts.ts +12 -0
- package/src/model/Done.ts +10 -0
- package/src/model/Error.ts +12 -0
- package/src/model/Facet.ts +7 -0
- package/src/model/FacetValue.ts +6 -0
- package/src/model/FacetedNavigation.ts +5 -0
- package/src/model/FacetedNavigationAck.ts +10 -0
- package/src/model/FacetedNavigationSuggestion.ts +7 -0
- package/src/model/FollowUp.ts +12 -0
- package/src/model/Inquiry.ts +10 -0
- package/src/model/OperationAck.ts +10 -0
- package/src/model/PartialInquiry.ts +12 -0
- package/src/model/RefinedSearch.ts +17 -0
- package/src/model/ReturnedFields.ts +6 -0
- package/src/model/SearchParams.ts +8 -0
- package/src/model/SessionData.ts +8 -0
- package/src/model/SessionEventStatusReply.ts +5 -0
- package/src/model/SessionInformation.ts +11 -0
- package/src/model/WorkflowMetadata.ts +6 -0
- package/src/model/customization.ts +7 -0
- package/src/model/index.ts +32 -0
- package/src/parser.ts +63 -0
- package/dist/bundle.cimulate.copilot-sdk.5e12bca4.cjs.js +0 -2
- package/dist/bundle.cimulate.copilot-sdk.5e12bca4.cjs.js.map +0 -1
- package/dist/bundle.cimulate.copilot-sdk.820437d6.esm.js +0 -2
- package/dist/bundle.cimulate.copilot-sdk.820437d6.esm.js.map +0 -1
- package/dist/bundle.cimulate.copilot-sdk.cc0f99da.umd.js +0 -2
- package/dist/bundle.cimulate.copilot-sdk.cc0f99da.umd.js.map +0 -1
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ConnectAckMetadata } from './ConnectAckMetadata';
|
|
2
|
+
interface ConnectAck {
|
|
3
|
+
sessionId: string;
|
|
4
|
+
eventId: string;
|
|
5
|
+
timestamp: string;
|
|
6
|
+
status: number;
|
|
7
|
+
message: string;
|
|
8
|
+
metadata: ConnectAckMetadata;
|
|
9
|
+
eventName: 'connect_ack';
|
|
10
|
+
}
|
|
11
|
+
export { ConnectAck };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ConnectAck } from "./ConnectAck";
|
|
2
|
+
import { DisplayProducts } from "./DisplayProducts";
|
|
3
|
+
import { Done } from "./Done";
|
|
4
|
+
import { Error } from "./Error";
|
|
5
|
+
import { FollowUp } from "./FollowUp";
|
|
6
|
+
import { Inquiry } from "./Inquiry";
|
|
7
|
+
import { PartialInquiry } from "./PartialInquiry";
|
|
8
|
+
import { RefinedSearch } from "./RefinedSearch";
|
|
9
|
+
import { ReturnedFields } from "./ReturnedFields";
|
|
10
|
+
|
|
11
|
+
export type CopilotAPIEvent<
|
|
12
|
+
TReturnedFields extends ReturnedFields = ReturnedFields
|
|
13
|
+
> =
|
|
14
|
+
| ConnectAck
|
|
15
|
+
| DisplayProducts<TReturnedFields>
|
|
16
|
+
| Done
|
|
17
|
+
| Error
|
|
18
|
+
| FollowUp
|
|
19
|
+
| Inquiry
|
|
20
|
+
| PartialInquiry
|
|
21
|
+
| RefinedSearch<TReturnedFields>;
|
|
22
|
+
|
|
23
|
+
export type CopilotEventName = CopilotAPIEvent["eventName"];
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { CommonMetadata } from './CommonMetadata';
|
|
2
|
+
import { ConnectAck } from './ConnectAck';
|
|
3
|
+
import { DisplayProducts } from './DisplayProducts';
|
|
4
|
+
import { Done } from './Done';
|
|
5
|
+
import { Error } from './Error';
|
|
6
|
+
import { FollowUp } from './FollowUp';
|
|
7
|
+
import { Inquiry } from './Inquiry';
|
|
8
|
+
import { RefinedSearch } from './RefinedSearch';
|
|
9
|
+
import { ReturnedFields } from "./ReturnedFields";
|
|
10
|
+
|
|
11
|
+
interface CopilotEvent<TReturnedFields extends ReturnedFields = ReturnedFields> {
|
|
12
|
+
id: string;
|
|
13
|
+
sessionId: string;
|
|
14
|
+
name: string;
|
|
15
|
+
eventSourceId: string;
|
|
16
|
+
data: ConnectAck | FollowUp | Inquiry | DisplayProducts<TReturnedFields> | RefinedSearch<TReturnedFields> | Done | Error;
|
|
17
|
+
createdAt: string;
|
|
18
|
+
metadata: CommonMetadata;
|
|
19
|
+
}
|
|
20
|
+
export { CopilotEvent };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ReturnedFields } from "./ReturnedFields";
|
|
2
|
+
|
|
3
|
+
interface DisplayProducts<TReturnedFields extends ReturnedFields = ReturnedFields> {
|
|
4
|
+
sessionId: string;
|
|
5
|
+
eventId: string;
|
|
6
|
+
timestamp: string;
|
|
7
|
+
eventSourceId: string;
|
|
8
|
+
message: string;
|
|
9
|
+
eventName: 'display_products';
|
|
10
|
+
products: TReturnedFields;
|
|
11
|
+
}
|
|
12
|
+
export { DisplayProducts };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { FacetedNavigation } from './FacetedNavigation';
|
|
2
|
+
interface FacetedNavigationAck {
|
|
3
|
+
eventName: 'faceted_navigation';
|
|
4
|
+
request: FacetedNavigation;
|
|
5
|
+
sessionId: string;
|
|
6
|
+
eventId: string;
|
|
7
|
+
timestamp: string;
|
|
8
|
+
status: string;
|
|
9
|
+
}
|
|
10
|
+
export { FacetedNavigationAck };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { CopilotSearchSuggestion } from './CopilotSearchSuggestion';
|
|
2
|
+
import { FacetedNavigationSuggestion } from './FacetedNavigationSuggestion';
|
|
3
|
+
interface FollowUp {
|
|
4
|
+
sessionId: string;
|
|
5
|
+
eventId: string;
|
|
6
|
+
timestamp: string;
|
|
7
|
+
eventSourceId: string;
|
|
8
|
+
message: string;
|
|
9
|
+
suggestions: (CopilotSearchSuggestion | FacetedNavigationSuggestion)[];
|
|
10
|
+
eventName: 'follow_up';
|
|
11
|
+
}
|
|
12
|
+
export { FollowUp };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Facet } from './Facet';
|
|
2
|
+
import { ReturnedFields } from "./ReturnedFields";
|
|
3
|
+
import { SearchParams } from './SearchParams';
|
|
4
|
+
|
|
5
|
+
interface RefinedSearch<TReturnedFields extends ReturnedFields = ReturnedFields> {
|
|
6
|
+
sessionId: string;
|
|
7
|
+
eventId: string;
|
|
8
|
+
timestamp: string;
|
|
9
|
+
eventSourceId: string;
|
|
10
|
+
message: string;
|
|
11
|
+
eventName: 'refined_search';
|
|
12
|
+
searchParams: SearchParams;
|
|
13
|
+
hits?: TReturnedFields;
|
|
14
|
+
totalHits?: number;
|
|
15
|
+
facets?: Facet[];
|
|
16
|
+
}
|
|
17
|
+
export { RefinedSearch };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { CopilotEvent } from './CopilotEvent';
|
|
2
|
+
import { ReturnedFields } from "./ReturnedFields";
|
|
3
|
+
|
|
4
|
+
interface SessionData<TReturnedFields extends ReturnedFields = ReturnedFields> {
|
|
5
|
+
events?: Record<string, CopilotEvent<TReturnedFields>>;
|
|
6
|
+
eventSourceMap?: Record<string, string[]>;
|
|
7
|
+
}
|
|
8
|
+
export { SessionData };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ReturnedFields } from "./ReturnedFields";
|
|
2
|
+
import { SessionData } from './SessionData';
|
|
3
|
+
|
|
4
|
+
interface SessionInformation<TReturnedFields extends ReturnedFields = ReturnedFields> {
|
|
5
|
+
sessionId?: string;
|
|
6
|
+
createdAt?: string;
|
|
7
|
+
lastActiveAt?: string;
|
|
8
|
+
status?: string;
|
|
9
|
+
data?: SessionData<TReturnedFields>;
|
|
10
|
+
}
|
|
11
|
+
export { SessionInformation };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export * from './Cancel';
|
|
2
|
+
export * from './CancelAck';
|
|
3
|
+
export * from './CommonFields';
|
|
4
|
+
export * from './CommonMetadata';
|
|
5
|
+
export * from './CommonRequiredFields';
|
|
6
|
+
export * from './ConnectAck';
|
|
7
|
+
export * from './ConnectAckMetadata';
|
|
8
|
+
export * from './CopilotAPIEvent';
|
|
9
|
+
export * from './CopilotEvent';
|
|
10
|
+
export * from './CopilotSearch';
|
|
11
|
+
export * from './CopilotSearchAck';
|
|
12
|
+
export * from './CopilotSearchSuggestion';
|
|
13
|
+
export * from './customization';
|
|
14
|
+
export * from './DisplayProducts';
|
|
15
|
+
export * from './Done';
|
|
16
|
+
export * from './Error';
|
|
17
|
+
export * from './Facet';
|
|
18
|
+
export * from './FacetedNavigation';
|
|
19
|
+
export * from './FacetedNavigationAck';
|
|
20
|
+
export * from './FacetedNavigationSuggestion';
|
|
21
|
+
export * from './FacetValue';
|
|
22
|
+
export * from './FollowUp';
|
|
23
|
+
export * from './Inquiry';
|
|
24
|
+
export * from './OperationAck';
|
|
25
|
+
export * from './PartialInquiry';
|
|
26
|
+
export * from './RefinedSearch';
|
|
27
|
+
export * from './ReturnedFields';
|
|
28
|
+
export * from './SearchParams';
|
|
29
|
+
export * from './SessionData';
|
|
30
|
+
export * from './SessionEventStatusReply';
|
|
31
|
+
export * from './SessionInformation';
|
|
32
|
+
export * from './WorkflowMetadata';
|
package/src/parser.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Decoder, Encoder, Packet } from "socket.io-parser";
|
|
2
|
+
import { RAW_PROPERTY_VALUES } from "./model/customization";
|
|
3
|
+
|
|
4
|
+
class SnakeCaseEncoder extends Encoder {
|
|
5
|
+
encode(packet: Packet) {
|
|
6
|
+
return super.encode({
|
|
7
|
+
...packet,
|
|
8
|
+
data:
|
|
9
|
+
packet.data?.length == 2
|
|
10
|
+
? [packet.data[0], encode(packet.data[1])]
|
|
11
|
+
: packet.data?.length == 1
|
|
12
|
+
? [packet.data[0]]
|
|
13
|
+
: undefined,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
class SnakeCaseDecoder extends Decoder {
|
|
19
|
+
constructor() {
|
|
20
|
+
super((_, value) =>
|
|
21
|
+
typeof value === "object" && value !== null && !Array.isArray(value)
|
|
22
|
+
? decode(value)
|
|
23
|
+
: value
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type Transform = (key: string) => string;
|
|
29
|
+
|
|
30
|
+
const keyTransformer = (
|
|
31
|
+
transform: Transform,
|
|
32
|
+
normalize: Transform,
|
|
33
|
+
stopAt?: string[]
|
|
34
|
+
) =>
|
|
35
|
+
function traverse(obj: object, ancestors: string[] = []) {
|
|
36
|
+
const result = Object.entries(obj).map(
|
|
37
|
+
([key, value]): [string, unknown] => {
|
|
38
|
+
const path = [...ancestors, normalize(key)];
|
|
39
|
+
return [
|
|
40
|
+
transform(key),
|
|
41
|
+
value instanceof Object && !stopAt?.includes(path.join("."))
|
|
42
|
+
? traverse(value, path)
|
|
43
|
+
: value,
|
|
44
|
+
];
|
|
45
|
+
}
|
|
46
|
+
);
|
|
47
|
+
return Array.isArray(obj)
|
|
48
|
+
? result.map((i) => i[1])
|
|
49
|
+
: Object.fromEntries(result);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const noop = (str: string) => str;
|
|
53
|
+
const snakeCase = (str: string) => str.replace(/([A-Z])/g, "_$1").toLowerCase();
|
|
54
|
+
const camelCase = (str: string) =>
|
|
55
|
+
str.replace(/_([a-z])/g, (_, c) => c.toUpperCase());
|
|
56
|
+
|
|
57
|
+
const encode = keyTransformer(snakeCase, noop, RAW_PROPERTY_VALUES);
|
|
58
|
+
const decode = keyTransformer(camelCase, camelCase, RAW_PROPERTY_VALUES);
|
|
59
|
+
|
|
60
|
+
export const SnakeCaseParser = {
|
|
61
|
+
Encoder: SnakeCaseEncoder,
|
|
62
|
+
Decoder: SnakeCaseDecoder,
|
|
63
|
+
};
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}Object.defineProperty(exports,"__esModule",{value:!0});var t=e(require("socket.io-client")),r=function(e){return function(e){return!!e&&"object"==typeof e}(e)&&!function(e){var t=Object.prototype.toString.call(e);return"[object RegExp]"===t||"[object Date]"===t||function(e){return e.$$typeof===n}(e)}(e)};var n="function"==typeof Symbol&&Symbol.for?Symbol.for("react.element"):60103;function o(e,t){return!1!==t.clone&&t.isMergeableObject(e)?u((r=e,Array.isArray(r)?[]:{}),e,t):e;var r}function c(e,t,r){return e.concat(t).map((function(e){return o(e,r)}))}function s(e){return Object.keys(e).concat(function(e){return Object.getOwnPropertySymbols?Object.getOwnPropertySymbols(e).filter((function(t){return Object.propertyIsEnumerable.call(e,t)})):[]}(e))}function a(e,t){try{return t in e}catch(e){return!1}}function i(e,t,r){var n={};return r.isMergeableObject(e)&&s(e).forEach((function(t){n[t]=o(e[t],r)})),s(t).forEach((function(c){(function(e,t){return a(e,t)&&!(Object.hasOwnProperty.call(e,t)&&Object.propertyIsEnumerable.call(e,t))})(e,c)||(a(e,c)&&r.isMergeableObject(t[c])?n[c]=function(e,t){if(!t.customMerge)return u;var r=t.customMerge(e);return"function"==typeof r?r:u}(c,r)(e[c],t[c],r):n[c]=o(t[c],r))})),n}function u(e,t,n){(n=n||{}).arrayMerge=n.arrayMerge||c,n.isMergeableObject=n.isMergeableObject||r,n.cloneUnlessOtherwiseSpecified=o;var s=Array.isArray(t);return s===Array.isArray(e)?s?n.arrayMerge(e,t,n):i(e,t,n):o(t,n)}u.all=function(e,t){if(!Array.isArray(e))throw new Error("first argument should be an array");return e.reduce((function(e,r){return u(e,r,t)}),{})};var l=u;function f(e){if(e)return function(e){for(var t in f.prototype)e[t]=f.prototype[t];return e}(e)}f.prototype.on=f.prototype.addEventListener=function(e,t){return this._callbacks=this._callbacks||{},(this._callbacks["$"+e]=this._callbacks["$"+e]||[]).push(t),this},f.prototype.once=function(e,t){function r(){this.off(e,r),t.apply(this,arguments)}return r.fn=t,this.on(e,r),this},f.prototype.off=f.prototype.removeListener=f.prototype.removeAllListeners=f.prototype.removeEventListener=function(e,t){if(this._callbacks=this._callbacks||{},0==arguments.length)return this._callbacks={},this;var r,n=this._callbacks["$"+e];if(!n)return this;if(1==arguments.length)return delete this._callbacks["$"+e],this;for(var o=0;o<n.length;o++)if((r=n[o])===t||r.fn===t){n.splice(o,1);break}return 0===n.length&&delete this._callbacks["$"+e],this},f.prototype.emit=function(e){this._callbacks=this._callbacks||{};for(var t=new Array(arguments.length-1),r=this._callbacks["$"+e],n=1;n<arguments.length;n++)t[n-1]=arguments[n];if(r){n=0;for(var o=(r=r.slice(0)).length;n<o;++n)r[n].apply(this,t)}return this},f.prototype.emitReserved=f.prototype.emit,f.prototype.listeners=function(e){return this._callbacks=this._callbacks||{},this._callbacks["$"+e]||[]},f.prototype.hasListeners=function(e){return!!this.listeners(e).length};const p="function"==typeof ArrayBuffer,y=Object.prototype.toString,h="function"==typeof Blob||"undefined"!=typeof Blob&&"[object BlobConstructor]"===y.call(Blob),b="function"==typeof File||"undefined"!=typeof File&&"[object FileConstructor]"===y.call(File);function d(e){return p&&(e instanceof ArrayBuffer||(e=>"function"==typeof ArrayBuffer.isView?ArrayBuffer.isView(e):e.buffer instanceof ArrayBuffer)(e))||h&&e instanceof Blob||b&&e instanceof File}function A(e,t){if(!e||"object"!=typeof e)return!1;if(Array.isArray(e)){for(let t=0,r=e.length;t<r;t++)if(A(e[t]))return!0;return!1}if(d(e))return!0;if(e.toJSON&&"function"==typeof e.toJSON&&1===arguments.length)return A(e.toJSON(),!0);for(const t in e)if(Object.prototype.hasOwnProperty.call(e,t)&&A(e[t]))return!0;return!1}function m(e){const t=[],r=e.data,n=e;return n.data=g(r,t),n.attachments=t.length,{packet:n,buffers:t}}function g(e,t){if(!e)return e;if(d(e)){const r={_placeholder:!0,num:t.length};return t.push(e),r}if(Array.isArray(e)){const r=new Array(e.length);for(let n=0;n<e.length;n++)r[n]=g(e[n],t);return r}if("object"==typeof e&&!(e instanceof Date)){const r={};for(const n in e)Object.prototype.hasOwnProperty.call(e,n)&&(r[n]=g(e[n],t));return r}return e}function k(e,t){return e.data=j(e.data,t),delete e.attachments,e}function j(e,t){if(!e)return e;if(e&&!0===e._placeholder){if("number"==typeof e.num&&e.num>=0&&e.num<t.length)return t[e.num];throw new Error("illegal attachments")}if(Array.isArray(e))for(let r=0;r<e.length;r++)e[r]=j(e[r],t);else if("object"==typeof e)for(const r in e)Object.prototype.hasOwnProperty.call(e,r)&&(e[r]=j(e[r],t));return e}const N=["connect","connect_error","disconnect","disconnecting","newListener","removeListener"];var C;!function(e){e[e.CONNECT=0]="CONNECT",e[e.DISCONNECT=1]="DISCONNECT",e[e.EVENT=2]="EVENT",e[e.ACK=3]="ACK",e[e.CONNECT_ERROR=4]="CONNECT_ERROR",e[e.BINARY_EVENT=5]="BINARY_EVENT",e[e.BINARY_ACK=6]="BINARY_ACK"}(C||(C={}));class O{constructor(e){this.replacer=e}encode(e){return e.type!==C.EVENT&&e.type!==C.ACK||!A(e)?[this.encodeAsString(e)]:this.encodeAsBinary({type:e.type===C.EVENT?C.BINARY_EVENT:C.BINARY_ACK,nsp:e.nsp,data:e.data,id:e.id})}encodeAsString(e){let t=""+e.type;return e.type!==C.BINARY_EVENT&&e.type!==C.BINARY_ACK||(t+=e.attachments+"-"),e.nsp&&"/"!==e.nsp&&(t+=e.nsp+","),null!=e.id&&(t+=e.id),null!=e.data&&(t+=JSON.stringify(e.data,this.replacer)),t}encodeAsBinary(e){const t=m(e),r=this.encodeAsString(t.packet),n=t.buffers;return n.unshift(r),n}}function E(e){return"[object Object]"===Object.prototype.toString.call(e)}class v extends f{constructor(e){super(),this.reviver=e}add(e){let t;if("string"==typeof e){if(this.reconstructor)throw new Error("got plaintext data when reconstructing a packet");t=this.decodeString(e);const r=t.type===C.BINARY_EVENT;r||t.type===C.BINARY_ACK?(t.type=r?C.EVENT:C.ACK,this.reconstructor=new _(t),0===t.attachments&&super.emitReserved("decoded",t)):super.emitReserved("decoded",t)}else{if(!d(e)&&!e.base64)throw new Error("Unknown type: "+e);if(!this.reconstructor)throw new Error("got binary data when not reconstructing a packet");t=this.reconstructor.takeBinaryData(e),t&&(this.reconstructor=null,super.emitReserved("decoded",t))}}decodeString(e){let t=0;const r={type:Number(e.charAt(0))};if(void 0===C[r.type])throw new Error("unknown packet type "+r.type);if(r.type===C.BINARY_EVENT||r.type===C.BINARY_ACK){const n=t+1;for(;"-"!==e.charAt(++t)&&t!=e.length;);const o=e.substring(n,t);if(o!=Number(o)||"-"!==e.charAt(t))throw new Error("Illegal attachments");r.attachments=Number(o)}if("/"===e.charAt(t+1)){const n=t+1;for(;++t;){if(","===e.charAt(t))break;if(t===e.length)break}r.nsp=e.substring(n,t)}else r.nsp="/";const n=e.charAt(t+1);if(""!==n&&Number(n)==n){const n=t+1;for(;++t;){const r=e.charAt(t);if(null==r||Number(r)!=r){--t;break}if(t===e.length)break}r.id=Number(e.substring(n,t+1))}if(e.charAt(++t)){const n=this.tryParse(e.substr(t));if(!v.isPayloadValid(r.type,n))throw new Error("invalid payload");r.data=n}return r}tryParse(e){try{return JSON.parse(e,this.reviver)}catch(e){return!1}}static isPayloadValid(e,t){switch(e){case C.CONNECT:return E(t);case C.DISCONNECT:return void 0===t;case C.CONNECT_ERROR:return"string"==typeof t||E(t);case C.EVENT:case C.BINARY_EVENT:return Array.isArray(t)&&("number"==typeof t[0]||"string"==typeof t[0]&&-1===N.indexOf(t[0]));case C.ACK:case C.BINARY_ACK:return Array.isArray(t)}}destroy(){this.reconstructor&&(this.reconstructor.finishedReconstruction(),this.reconstructor=null)}}class _{constructor(e){this.packet=e,this.buffers=[],this.reconPack=e}takeBinaryData(e){if(this.buffers.push(e),this.buffers.length===this.reconPack.attachments){const e=k(this.reconPack,this.buffers);return this.finishedReconstruction(),e}return null}finishedReconstruction(){this.reconPack=null,this.buffers=[]}}var w={},T={};function R(e,t){if(null==e||"object"!=typeof e)return e;const r=Array.isArray(e)?[]:{};for(const[n,o]of Object.entries(e))r[t(n)]=Array.isArray(o)?o.map((e=>"object"!=typeof e||e instanceof Uint8Array||e instanceof Date?e:R(e,t))):o instanceof Uint8Array||o instanceof Date?o:"object"==typeof o?R(o,t):o;return r}function P(e){return 1===e.length?e.toLowerCase():e.replace(/^([A-Z])/,(e=>e[0].toLowerCase())).replace(/[_-]([a-z0-9])/g,(e=>e[1].toUpperCase()))}function S(e){var t,r;let n=e,o=0;for(;((null===(t=/([a-z])([0-9])/.exec(n))||void 0===t?void 0:t.length)||0)>2&&o<10;)n=n.replace(/([a-z])([0-9])/,((e,t,r)=>`${t.toLowerCase()}_${r.toLowerCase()}`)),o+=1;for(;((null===(r=/(.+?)([A-Z])/.exec(n))||void 0===r?void 0:r.length)||0)>2&&o<10;)n=n.replace(/(.+?)([A-Z])/,((e,t,r)=>`${t.toLowerCase()}_${r.toLowerCase()}`)),o+=1;return n.toLowerCase()}function B(e){return P(e).replace(/^([a-z])/,(e=>e[0].toUpperCase()))}Object.defineProperty(T,"__esModule",{value:!0}),T.objectToPascal=T.toPascal=T.objectToSnake=T.toSnake=T.objectToCamel=T.toCamel=void 0,T.toCamel=P,T.objectToCamel=function(e){return R(e,P)},T.toSnake=S,T.objectToSnake=function(e){return R(e,S)},T.toPascal=B,T.objectToPascal=function(e){return R(e,B)},function(e){Object.defineProperty(e,"__esModule",{value:!0}),e.objectToPascal=e.toPascal=e.toCamel=e.toSnake=e.objectToSnake=e.objectToCamel=void 0;var t=T;Object.defineProperty(e,"objectToCamel",{enumerable:!0,get:function(){return t.objectToCamel}}),Object.defineProperty(e,"objectToSnake",{enumerable:!0,get:function(){return t.objectToSnake}}),Object.defineProperty(e,"toSnake",{enumerable:!0,get:function(){return t.toSnake}}),Object.defineProperty(e,"toCamel",{enumerable:!0,get:function(){return t.toCamel}}),Object.defineProperty(e,"toPascal",{enumerable:!0,get:function(){return t.toPascal}}),Object.defineProperty(e,"objectToPascal",{enumerable:!0,get:function(){return t.objectToPascal}})}(w);const I={Encoder:class extends O{encode(e){return super.encode({...e,data:2==e.data?.length?[e.data[0],w.objectToSnake(e.data[1])]:void 0})}},Decoder:class extends v{constructor(){super(((e,t)=>"object"!=typeof t||null===t||Array.isArray(t)?t:w.objectToCamel(t)))}}};const V={path:"/api/v1/socket.io",autoConnect:!1,transports:["polling","websocket"],upgrade:!0,timeout:1e4};exports.CimulateCopilot=class{constructor({apiKey:e,baseUrl:r,namespace:n="/copilot",socketOptions:o={}}){const c=`${r}${n}`,s=[V,o,{parser:I,withCredentials:!0,transportOptions:{polling:{extraHeaders:{"x-cimulate-api-key":e}}}}];this.socket=t.default(c,l.all(s))}connect(){this.socket.connect()}async search(e,t){return this.asyncResponse("copilot_search",e,t)}async facetedNavigation(e,t){return this.asyncResponse("copilot_search",e,t)}cancelRequest(e,t){return this.asyncResponse("copilot_search",e,t)}async requestSessionInformation(e){this.socket.emit("session_info",e)}on(e,t){return this.socket.on(e,t),t}once(e,t){this.socket.once(e,t)}off(e,t){this.socket.off(e,t)}disconnect(){this.socket.disconnect()}onDisconnect(e){this.socket.on("disconnect",e)}async asyncResponse(e,t,r){if(r)return void this.socket.emit("copilot_search",t,r);let n="";const o=async function*(e){const t=[];let r=!1,n=null;const o=await e({emit:e=>{t.push(e),n&&(n(),n=null)},cancel:()=>{r=!0}});try{for(;!r;)t.length>0?yield t.shift():await new Promise((e=>{n=e}));for(;t.length>0;)yield t.shift()}finally{await(o?.())}}((({emit:e,cancel:t})=>{const r=(r,o)=>{"done"==o.eventName&&t(),o.eventSourceId==n&&e(o)};return this.socket.onAny(r),async()=>{this.socket.offAny(r)}}));return{result:await new Promise((r=>this.socket.emit(e,t,(e=>{n=e.eventId,r(e)})))),events:o}}};
|
|
2
|
-
//# sourceMappingURL=bundle.cimulate.copilot-sdk.5e12bca4.cjs.js.map
|