@constructor-io/constructorio-ui-pia 2.11.0 → 2.12.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/dist/constructorio-ui-pia-bundled.js +60 -60
- package/lib/cjs/components/CioPia/CioPiaQna.js +2 -1
- package/lib/cjs/components/CioPia/PiaCustomCarousel.js +24 -29
- package/lib/cjs/hooks/useAnswerResults.js +4 -14
- package/lib/cjs/hooks/useCioPia.js +42 -4
- package/lib/cjs/hooks/usePiaClient.js +2 -7
- package/lib/cjs/hooks/useRecsPod.js +10 -5
- package/lib/cjs/hooks/useSuggestedQuestions.js +10 -26
- package/lib/cjs/types.js +0 -2
- package/lib/cjs/version.js +1 -1
- package/lib/mjs/components/CioPia/CioPiaQna.js +2 -1
- package/lib/mjs/components/CioPia/PiaCustomCarousel.js +43 -30
- package/lib/mjs/hooks/useAnswerResults.js +4 -13
- package/lib/mjs/hooks/useCioPia.js +43 -4
- package/lib/mjs/hooks/usePiaClient.js +2 -7
- package/lib/mjs/hooks/useRecsPod.js +9 -5
- package/lib/mjs/hooks/useSuggestedQuestions.js +12 -24
- package/lib/mjs/types.js +0 -1
- package/lib/mjs/version.js +1 -1
- package/lib/types/components/CioPia/types.d.ts +20 -5
- package/lib/types/hooks/useAnswerResults.d.ts +4 -4
- package/lib/types/hooks/useCioPia.d.ts +20 -5
- package/lib/types/hooks/usePiaClient.d.ts +4 -9
- package/lib/types/hooks/useRecsPod.d.ts +2 -2
- package/lib/types/hooks/useSuggestedQuestions.d.ts +3 -4
- package/lib/types/types.d.ts +43 -6
- package/lib/types/version.d.ts +1 -1
- package/package.json +2 -2
- package/lib/cjs/hooks/mocks/MockConstructorIOClient.js +0 -24
- package/lib/cjs/hooks/mocks/agent.js +0 -183
- package/lib/cjs/hooks/mocks/types.js +0 -2
- package/lib/mjs/hooks/mocks/MockConstructorIOClient.js +0 -38
- package/lib/mjs/hooks/mocks/agent.js +0 -173
- package/lib/mjs/hooks/mocks/types.js +0 -1
- package/lib/types/hooks/mocks/MockConstructorIOClient.d.ts +0 -14
- package/lib/types/hooks/mocks/agent.d.ts +0 -23
- package/lib/types/hooks/mocks/types.d.ts +0 -77
|
@@ -1,35 +1,22 @@
|
|
|
1
|
-
import { useCallback, useEffect, useState } from 'react';
|
|
2
|
-
|
|
3
|
-
const response = await client.agent.getSuggestedQuestions({
|
|
4
|
-
itemId,
|
|
5
|
-
variationId,
|
|
6
|
-
threadId,
|
|
7
|
-
parameters,
|
|
8
|
-
requestParameters,
|
|
9
|
-
});
|
|
10
|
-
return response.questions;
|
|
11
|
-
};
|
|
12
|
-
export default function useSuggestedQuestions({ itemId, variationId, threadId, cioClient, parameters, requestParameters, }) {
|
|
1
|
+
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
2
|
+
export default function useSuggestedQuestions({ itemId, variationId, threadId, cioClient, parameters, }) {
|
|
13
3
|
const [questions, setQuestions] = useState([]);
|
|
14
4
|
const [isLoading, setIsLoading] = useState(false);
|
|
15
5
|
const [error, setError] = useState(null);
|
|
16
|
-
|
|
17
|
-
const requestParametersKey = JSON.stringify(requestParameters);
|
|
6
|
+
const serializedParameters = useMemo(() => JSON.stringify(parameters), [parameters]);
|
|
18
7
|
const fetchResult = useCallback(() => {
|
|
19
8
|
if (!cioClient)
|
|
20
9
|
return;
|
|
21
10
|
setIsLoading(true);
|
|
22
11
|
setError(null);
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
itemId,
|
|
26
|
-
variationId,
|
|
12
|
+
cioClient.agent.pia
|
|
13
|
+
.getSuggestedQuestions(itemId, {
|
|
27
14
|
threadId,
|
|
28
|
-
|
|
29
|
-
|
|
15
|
+
variationId,
|
|
16
|
+
...parameters,
|
|
30
17
|
})
|
|
31
|
-
.then((
|
|
32
|
-
setQuestions(
|
|
18
|
+
.then((response) => {
|
|
19
|
+
setQuestions(response.questions);
|
|
33
20
|
setError(null);
|
|
34
21
|
})
|
|
35
22
|
.catch((err) => {
|
|
@@ -38,8 +25,9 @@ export default function useSuggestedQuestions({ itemId, variationId, threadId, c
|
|
|
38
25
|
.finally(() => {
|
|
39
26
|
setIsLoading(false);
|
|
40
27
|
});
|
|
41
|
-
//
|
|
42
|
-
|
|
28
|
+
// parameters is serialized via serializedParameters to prevent refetch on identity changes
|
|
29
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
30
|
+
}, [cioClient, itemId, variationId, threadId, serializedParameters]);
|
|
43
31
|
useEffect(() => {
|
|
44
32
|
fetchResult();
|
|
45
33
|
}, [fetchResult]);
|
package/lib/mjs/types.js
CHANGED
package/lib/mjs/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export default '2.
|
|
1
|
+
export default '2.12.0';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { IncludeComponentOverrides, IncludeRenderProps } from '@constructor-io/constructorio-ui-components';
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
2
|
+
import { CioPiaRenderProps, CioPiaComponentOverrides, Callbacks, CioPiaDisplayConfigs, CioPiaTrackingConfigs, Translations, SuggestedQuestionsParameters, AnswerRequestParameters, RecsPodParameters, Formatters, ProductCardDisplayProps } from '../../types';
|
|
3
|
+
import type { CioClient } from '../../hooks/usePiaClient';
|
|
4
4
|
export interface CioPiaProps extends IncludeRenderProps<CioPiaRenderProps>, IncludeComponentOverrides<CioPiaComponentOverrides> {
|
|
5
5
|
/** Your Constructor.io API key. */
|
|
6
6
|
apiKey: string;
|
|
@@ -13,7 +13,7 @@ export interface CioPiaProps extends IncludeRenderProps<CioPiaRenderProps>, Incl
|
|
|
13
13
|
/** Optional variation ID for the product. */
|
|
14
14
|
variationId?: string;
|
|
15
15
|
/** Optional Constructor.io client instance. If not provided, one will be created internally. */
|
|
16
|
-
cioClient?:
|
|
16
|
+
cioClient?: CioClient;
|
|
17
17
|
/** Display configuration options (mode, type, showFeedback, etc.). */
|
|
18
18
|
displayConfigs?: CioPiaDisplayConfigs;
|
|
19
19
|
/** Tracking configuration options (viewThreshold, etc.). */
|
|
@@ -30,11 +30,26 @@ export interface CioPiaProps extends IncludeRenderProps<CioPiaRenderProps>, Incl
|
|
|
30
30
|
translations?: Translations;
|
|
31
31
|
/** Parameters for the suggested questions request. */
|
|
32
32
|
suggestedQuestionsParameters?: SuggestedQuestionsParameters;
|
|
33
|
+
/** Parameters for the answer request. */
|
|
34
|
+
answerParameters?: AnswerRequestParameters;
|
|
33
35
|
/** Parameters for the recommendations request, used by `mode: 'recommendations'`. */
|
|
34
36
|
recsPodParameters?: RecsPodParameters;
|
|
35
37
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
+
* @deprecated Use `answerParameters` and `suggestedQuestionsParameters` instead.
|
|
39
|
+
*
|
|
40
|
+
* Only the following keys are forwarded (snake_case or camelCase accepted):
|
|
41
|
+
*
|
|
42
|
+
* Suggested-questions endpoint:
|
|
43
|
+
* - `num_results` / `numResults` → numResults
|
|
44
|
+
* - `pre_filter_expression` / `preFilterExpression` → preFilterExpression (JSON-parsed if string)
|
|
45
|
+
*
|
|
46
|
+
* Answer endpoint:
|
|
47
|
+
* - `guard` → guard
|
|
48
|
+
* - `pre_filter_expression` / `preFilterExpression` → preFilterExpression (JSON-parsed if string)
|
|
49
|
+
* - `fmt_options` / `fmtOptions` → fmtOptions (JSON-parsed if string)
|
|
50
|
+
*
|
|
51
|
+
* All other keys (e.g. `ef-*`) are silently dropped.
|
|
52
|
+
* Typed parameters take precedence over values specified here.
|
|
38
53
|
*/
|
|
39
54
|
parameters?: Record<string, string | number | boolean>;
|
|
40
55
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { Nullable } from '@constructor-io/constructorio-client-javascript';
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
2
|
+
import { AnswerRequestParameters, Formatters, Item, GetAnswerResultsResponse } from '../types';
|
|
3
|
+
import type { CioClient } from './usePiaClient';
|
|
4
4
|
export interface UseAnswerResultsProps {
|
|
5
5
|
itemId: string;
|
|
6
6
|
variationId?: string;
|
|
7
7
|
threadId?: string;
|
|
8
|
-
cioClient:
|
|
9
|
-
parameters?:
|
|
8
|
+
cioClient: CioClient;
|
|
9
|
+
parameters?: AnswerRequestParameters;
|
|
10
10
|
formatImageUrl?: Formatters['formatImageUrl'];
|
|
11
11
|
}
|
|
12
12
|
export interface UseAnswerResultsReturn {
|
|
@@ -1,18 +1,33 @@
|
|
|
1
1
|
import { Tracker } from '@constructor-io/constructorio-client-javascript/lib/types/constructorio';
|
|
2
|
-
import { Formatters, SuggestedQuestionsParameters } from '../types';
|
|
3
|
-
import MockConstructorIOClient from './mocks/MockConstructorIOClient';
|
|
2
|
+
import { AnswerRequestParameters, Formatters, SuggestedQuestionsParameters } from '../types';
|
|
4
3
|
import { UseAnswerResultsReturn } from './useAnswerResults';
|
|
4
|
+
import { CioClient } from './usePiaClient';
|
|
5
5
|
import { UseSuggestedQuestionsReturn } from './useSuggestedQuestions';
|
|
6
|
+
export type { CioClient };
|
|
6
7
|
export interface UseCioPiaProps {
|
|
7
8
|
apiKey: string;
|
|
8
9
|
itemId: string;
|
|
9
10
|
variationId?: string;
|
|
10
11
|
threadId?: string;
|
|
11
|
-
cioClient?:
|
|
12
|
+
cioClient?: CioClient;
|
|
12
13
|
suggestedQuestionsParameters?: SuggestedQuestionsParameters;
|
|
14
|
+
answerParameters?: AnswerRequestParameters;
|
|
13
15
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
+
* @deprecated Use `answerParameters` and `suggestedQuestionsParameters` instead.
|
|
17
|
+
*
|
|
18
|
+
* Only the following keys are forwarded (snake_case or camelCase accepted):
|
|
19
|
+
*
|
|
20
|
+
* Suggested-questions endpoint:
|
|
21
|
+
* - `num_results` / `numResults` → numResults
|
|
22
|
+
* - `pre_filter_expression` / `preFilterExpression` → preFilterExpression (JSON-parsed if string)
|
|
23
|
+
*
|
|
24
|
+
* Answer endpoint:
|
|
25
|
+
* - `guard` → guard
|
|
26
|
+
* - `pre_filter_expression` / `preFilterExpression` → preFilterExpression (JSON-parsed if string)
|
|
27
|
+
* - `fmt_options` / `fmtOptions` → fmtOptions (JSON-parsed if string)
|
|
28
|
+
*
|
|
29
|
+
* All other keys (e.g. `ef-*`) are silently dropped.
|
|
30
|
+
* Typed parameters take precedence over values specified here.
|
|
16
31
|
*/
|
|
17
32
|
parameters?: Record<string, string | number | boolean>;
|
|
18
33
|
/** Define outside the component or wrap with useCallback to avoid unnecessary re-renders. */
|
|
@@ -1,17 +1,12 @@
|
|
|
1
|
-
import
|
|
1
|
+
import ConstructorIOClient from '@constructor-io/constructorio-client-javascript';
|
|
2
|
+
export type CioClient = InstanceType<typeof ConstructorIOClient>;
|
|
2
3
|
export interface UsePiaClientProps {
|
|
3
4
|
apiKey: string;
|
|
4
|
-
/** Thread ID for conversation context. Must be a valid UUID. One is generated when absent. */
|
|
5
5
|
threadId?: string;
|
|
6
|
-
cioClient?:
|
|
6
|
+
cioClient?: CioClient;
|
|
7
7
|
}
|
|
8
8
|
export interface UsePiaClientReturn {
|
|
9
|
-
cioClient:
|
|
9
|
+
cioClient: CioClient;
|
|
10
10
|
threadId: string;
|
|
11
11
|
}
|
|
12
|
-
/**
|
|
13
|
-
* Resolves the client and thread ID every mode needs: the caller's client when they supplied
|
|
14
|
-
* one, otherwise a client built from the API key, plus a generated thread ID that stays stable
|
|
15
|
-
* for the life of the component.
|
|
16
|
-
*/
|
|
17
12
|
export default function usePiaClient({ apiKey, threadId: providedThreadId, cioClient: providedClient, }: UsePiaClientProps): UsePiaClientReturn;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type { CioClient } from './usePiaClient';
|
|
2
2
|
import { Formatters, Item, RecsPodParameters, RecsRefinement, Translations } from '../types';
|
|
3
3
|
/**
|
|
4
4
|
* What the shopper did to ask for this refinement.
|
|
@@ -12,7 +12,7 @@ export interface UseRecsPodProps {
|
|
|
12
12
|
itemId: string;
|
|
13
13
|
variationId?: string;
|
|
14
14
|
threadId?: string;
|
|
15
|
-
cioClient?:
|
|
15
|
+
cioClient?: CioClient;
|
|
16
16
|
parameters?: RecsPodParameters;
|
|
17
17
|
formatImageUrl?: Formatters['formatImageUrl'];
|
|
18
18
|
translations?: Translations;
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { Question, SuggestedQuestionsParameters } from '../types';
|
|
2
|
-
import
|
|
2
|
+
import type { CioClient } from './usePiaClient';
|
|
3
3
|
export interface UseSuggestedQuestionsProps {
|
|
4
4
|
itemId: string;
|
|
5
5
|
variationId?: string;
|
|
6
6
|
threadId?: string;
|
|
7
|
-
cioClient?:
|
|
7
|
+
cioClient?: CioClient;
|
|
8
8
|
parameters?: SuggestedQuestionsParameters;
|
|
9
|
-
requestParameters?: Record<string, string | number | boolean>;
|
|
10
9
|
}
|
|
11
10
|
export interface UseSuggestedQuestionsReturn {
|
|
12
11
|
data: Array<Question>;
|
|
@@ -14,4 +13,4 @@ export interface UseSuggestedQuestionsReturn {
|
|
|
14
13
|
error: Error | null;
|
|
15
14
|
getSuggestedQuestions: () => void;
|
|
16
15
|
}
|
|
17
|
-
export default function useSuggestedQuestions({ itemId, variationId, threadId, cioClient, parameters,
|
|
16
|
+
export default function useSuggestedQuestions({ itemId, variationId, threadId, cioClient, parameters, }: UseSuggestedQuestionsProps): UseSuggestedQuestionsReturn;
|
package/lib/types/types.d.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { ReactNode } from 'react';
|
|
2
2
|
import { Product, CarouselOverrides, ComponentOverrideProps } from '@constructor-io/constructorio-ui-components';
|
|
3
|
-
import { ConstructorClientOptions, Nullable } from '@constructor-io/constructorio-client-javascript';
|
|
4
|
-
import {
|
|
5
|
-
import MockConstructorIOClient from './hooks/mocks/MockConstructorIOClient';
|
|
3
|
+
import { ConstructorClientOptions, FilterExpression, ItemData, Nullable } from '@constructor-io/constructorio-client-javascript';
|
|
4
|
+
import type { CioClient } from './hooks/usePiaClient';
|
|
6
5
|
export declare enum FeedbackType {
|
|
7
6
|
UP = "up",
|
|
8
7
|
DOWN = "down"
|
|
9
8
|
}
|
|
10
9
|
export interface PiaContextValue {
|
|
11
|
-
cioClient: Nullable<
|
|
10
|
+
cioClient: Nullable<CioClient>;
|
|
12
11
|
cioClientOptions: CioClientOptions;
|
|
13
12
|
setCioClientOptions: React.Dispatch<CioClientOptions>;
|
|
14
13
|
itemId: string;
|
|
@@ -23,7 +22,7 @@ export interface CioPiaProviderProps {
|
|
|
23
22
|
variationId?: string;
|
|
24
23
|
/** Thread ID for conversation context. Must be a valid UUID (e.g., "550e8400-e29b-41d4-a716-446655440000") */
|
|
25
24
|
threadId?: string;
|
|
26
|
-
cioClient?: Nullable<
|
|
25
|
+
cioClient?: Nullable<CioClient>;
|
|
27
26
|
}
|
|
28
27
|
export type CioPiaMode = 'default' | 'conversation' | 'recommendations';
|
|
29
28
|
export type CioPiaType = 'inline' | 'modal';
|
|
@@ -244,4 +243,42 @@ export interface CioPiaComponentOverrides extends ComponentOverrideProps<CioPiaR
|
|
|
244
243
|
feedback?: ComponentOverrideProps<FeedbackRenderProps>;
|
|
245
244
|
loading?: ComponentOverrideProps<LoadingRenderProps>;
|
|
246
245
|
}
|
|
247
|
-
export
|
|
246
|
+
export interface Question {
|
|
247
|
+
value: string;
|
|
248
|
+
}
|
|
249
|
+
export interface QuestionResponse {
|
|
250
|
+
questions: Array<Question>;
|
|
251
|
+
}
|
|
252
|
+
export interface SuggestedQuestionsParameters {
|
|
253
|
+
numResults?: number;
|
|
254
|
+
preFilterExpression?: FilterExpression;
|
|
255
|
+
}
|
|
256
|
+
export interface AnswerRequestParameters {
|
|
257
|
+
preFilterExpression?: FilterExpression;
|
|
258
|
+
guard?: boolean;
|
|
259
|
+
fmtOptions?: Record<string, any>;
|
|
260
|
+
}
|
|
261
|
+
export interface ApiItemVariation extends Record<string, any> {
|
|
262
|
+
value: string;
|
|
263
|
+
data?: ItemData;
|
|
264
|
+
}
|
|
265
|
+
export interface ApiItem extends Record<string, any> {
|
|
266
|
+
value: string;
|
|
267
|
+
matched_terms: Array<string>;
|
|
268
|
+
data: ItemData;
|
|
269
|
+
variations?: Array<ApiItemVariation> | null;
|
|
270
|
+
variations_map?: Record<string, any> | Array<Record<string, any>> | null;
|
|
271
|
+
}
|
|
272
|
+
export interface AnswerItemResults {
|
|
273
|
+
request?: Record<string, any>;
|
|
274
|
+
response: {
|
|
275
|
+
results: Array<ApiItem>;
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
export interface GetAnswerResultsResponse {
|
|
279
|
+
qna_result_id: string;
|
|
280
|
+
value: string;
|
|
281
|
+
item_results?: AnswerItemResults;
|
|
282
|
+
follow_up_questions?: Array<Question>;
|
|
283
|
+
thread_id?: string;
|
|
284
|
+
}
|
package/lib/types/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: "2.
|
|
1
|
+
declare const _default: "2.12.0";
|
|
2
2
|
export default _default;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@constructor-io/constructorio-ui-pia",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.12.0",
|
|
4
4
|
"description": "Constructor.io AI Product Insights Agent UI library for web applications",
|
|
5
5
|
"author": "Constructor.io Corporation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
"prepare": "husky"
|
|
71
71
|
},
|
|
72
72
|
"peerDependencies": {
|
|
73
|
-
"@constructor-io/constructorio-client-javascript": "^2.
|
|
73
|
+
"@constructor-io/constructorio-client-javascript": "^2.93.0",
|
|
74
74
|
"@constructor-io/constructorio-ui-components": "^1.5.0",
|
|
75
75
|
"react": ">=16.12.0",
|
|
76
76
|
"react-dom": ">=16.12.0",
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
const constructorio_client_javascript_1 = tslib_1.__importDefault(require("@constructor-io/constructorio-client-javascript"));
|
|
5
|
-
const agent_1 = tslib_1.__importDefault(require("./agent"));
|
|
6
|
-
const version_1 = tslib_1.__importDefault(require("../../version"));
|
|
7
|
-
class MockConstructorIOClient {
|
|
8
|
-
constructor(options) {
|
|
9
|
-
const isServer = typeof window === 'undefined' || typeof document === 'undefined';
|
|
10
|
-
const clientOptions = Object.assign(Object.assign(Object.assign({ version: options.version || `cio-ui-pia-${version_1.default}`, serviceUrl: options.serviceUrl || 'https://ac.cnstrc.com', quizzesServiceUrl: options.quizzesServiceUrl || 'https://quizzes.cnstrc.com', agentServiceUrl: options.agentServiceUrl || 'https://agent.cnstrc.com' }, (isServer && {
|
|
11
|
-
clientId: options.clientId || 'this-is-a-random-client-id',
|
|
12
|
-
})), { sendTrackingEvents: options.sendTrackingEvents !== undefined ? options.sendTrackingEvents : true, beaconMode: options.beaconMode !== undefined ? options.beaconMode : true, networkParameters: options.networkParameters || {} }), options);
|
|
13
|
-
const cioClient = new constructorio_client_javascript_1.default(clientOptions);
|
|
14
|
-
const { clientId, sessionId } = cioClient.agent.options;
|
|
15
|
-
this.options = Object.assign(Object.assign({}, clientOptions), { clientId, sessionId });
|
|
16
|
-
this.search = cioClient.search;
|
|
17
|
-
this.browse = cioClient.browse;
|
|
18
|
-
this.recommendations = cioClient.recommendations;
|
|
19
|
-
this.tracker = cioClient.tracker;
|
|
20
|
-
this.quizzes = cioClient.quizzes;
|
|
21
|
-
this.agent = new agent_1.default(this.options);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
exports.default = MockConstructorIOClient;
|
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
const errors_1 = require("../../errors");
|
|
5
|
-
// Create URL for PIA API
|
|
6
|
-
function createAgentUrl({ itemId, threadId, variationId, question, isStreaming = false, options, parameters = {}, }) {
|
|
7
|
-
const { apiKey, agentServiceUrl, clientId, sessionId, userId, segments, version } = options;
|
|
8
|
-
if (!agentServiceUrl)
|
|
9
|
-
throw new Error('Agent service URL is required');
|
|
10
|
-
let baseUrl = `${agentServiceUrl}/v1/item_questions`;
|
|
11
|
-
if (question) {
|
|
12
|
-
baseUrl += `/${encodeURIComponent(question)}/answer`;
|
|
13
|
-
}
|
|
14
|
-
if (isStreaming) {
|
|
15
|
-
baseUrl += '/streaming';
|
|
16
|
-
}
|
|
17
|
-
const url = new URL(baseUrl);
|
|
18
|
-
url.searchParams.append('item_id', itemId);
|
|
19
|
-
url.searchParams.append('key', apiKey);
|
|
20
|
-
if (threadId) {
|
|
21
|
-
url.searchParams.append('thread_id', threadId);
|
|
22
|
-
}
|
|
23
|
-
if (variationId) {
|
|
24
|
-
url.searchParams.append('variation_id', variationId);
|
|
25
|
-
}
|
|
26
|
-
if (clientId) {
|
|
27
|
-
url.searchParams.append('i', clientId);
|
|
28
|
-
}
|
|
29
|
-
if (sessionId != null) {
|
|
30
|
-
url.searchParams.append('s', String(sessionId));
|
|
31
|
-
}
|
|
32
|
-
if (userId) {
|
|
33
|
-
url.searchParams.append('ui', userId);
|
|
34
|
-
}
|
|
35
|
-
if (version) {
|
|
36
|
-
url.searchParams.append('c', version);
|
|
37
|
-
}
|
|
38
|
-
if (segments === null || segments === void 0 ? void 0 : segments.length) {
|
|
39
|
-
segments.forEach((segment) => url.searchParams.append('us', segment));
|
|
40
|
-
}
|
|
41
|
-
// Any additional parameters
|
|
42
|
-
Object.entries(parameters).forEach(([key, value]) => {
|
|
43
|
-
if (value !== undefined) {
|
|
44
|
-
url.searchParams.append(key, String(value));
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
return url.toString();
|
|
48
|
-
}
|
|
49
|
-
// Map camelCase SuggestedQuestionsParameters to snake_case query params expected by the API
|
|
50
|
-
function mapSuggestedQuestionsParams(params) {
|
|
51
|
-
const result = {};
|
|
52
|
-
if (params.numResults !== undefined)
|
|
53
|
-
result.num_results = params.numResults;
|
|
54
|
-
return result;
|
|
55
|
-
}
|
|
56
|
-
class MockAgent {
|
|
57
|
-
constructor(options) {
|
|
58
|
-
this.options = options;
|
|
59
|
-
}
|
|
60
|
-
getSuggestedQuestions({ itemId, variationId, threadId, parameters = {}, requestParameters = {}, }) {
|
|
61
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
62
|
-
if (!itemId)
|
|
63
|
-
throw new Error('Item ID is required');
|
|
64
|
-
if (!this.options.apiKey)
|
|
65
|
-
throw new Error('API key is required');
|
|
66
|
-
const url = createAgentUrl({
|
|
67
|
-
itemId,
|
|
68
|
-
variationId,
|
|
69
|
-
threadId,
|
|
70
|
-
options: this.options,
|
|
71
|
-
parameters: Object.assign(Object.assign({}, requestParameters), mapSuggestedQuestionsParams(parameters)),
|
|
72
|
-
});
|
|
73
|
-
try {
|
|
74
|
-
const response = yield fetch(url);
|
|
75
|
-
if (!response.ok)
|
|
76
|
-
throw new errors_1.AgentRequestError(response.status);
|
|
77
|
-
const data = yield response.json();
|
|
78
|
-
return data;
|
|
79
|
-
}
|
|
80
|
-
catch (error) {
|
|
81
|
-
// Rethrow untouched so the status code and the original stack survive.
|
|
82
|
-
if (error instanceof Error)
|
|
83
|
-
throw error;
|
|
84
|
-
throw new Error(String(error));
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
getAnswerResults({ itemId, variationId, threadId, question, parameters = {}, }) {
|
|
89
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
90
|
-
if (!itemId)
|
|
91
|
-
throw new Error('Item ID is required');
|
|
92
|
-
if (!question)
|
|
93
|
-
throw new Error('Question is required');
|
|
94
|
-
if (!this.options.apiKey)
|
|
95
|
-
throw new Error('API key is required');
|
|
96
|
-
const url = createAgentUrl({
|
|
97
|
-
itemId,
|
|
98
|
-
threadId,
|
|
99
|
-
variationId,
|
|
100
|
-
question,
|
|
101
|
-
options: this.options,
|
|
102
|
-
parameters,
|
|
103
|
-
});
|
|
104
|
-
try {
|
|
105
|
-
const response = yield fetch(url);
|
|
106
|
-
if (!response.ok)
|
|
107
|
-
throw new errors_1.AgentRequestError(response.status);
|
|
108
|
-
const data = yield response.json();
|
|
109
|
-
return data;
|
|
110
|
-
}
|
|
111
|
-
catch (error) {
|
|
112
|
-
// Rethrow untouched so the status code and the original stack survive.
|
|
113
|
-
if (error instanceof Error)
|
|
114
|
-
throw error;
|
|
115
|
-
throw new Error(String(error));
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Fetches one set of recommendations, in the shape the pod renders.
|
|
121
|
-
*
|
|
122
|
-
* Recommendations are not available yet: there is no endpoint to ask, so nothing is requested and
|
|
123
|
-
* an empty result is returned. The pod renders nothing in that state, which leaves whatever the
|
|
124
|
-
* retailer already had in that slot showing through. Supply a `cioClient` with your own
|
|
125
|
-
* `agent.getRecs` to drive the pod from your own data in the meantime.
|
|
126
|
-
*
|
|
127
|
-
* `props` is still declared, because it is the contract a caller's own `getRecs` implements and
|
|
128
|
-
* `useRecsPod` passes it - there is simply nothing here to send it to yet.
|
|
129
|
-
*/
|
|
130
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
131
|
-
getRecs(props) {
|
|
132
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
133
|
-
return { title: '', items: null, refinement: null, status: 'complete' };
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
getAnswerResultsStream({ itemId, threadId, variationId, question, parameters, onStart, onMessage, onEnd, }) {
|
|
137
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
138
|
-
if (!itemId)
|
|
139
|
-
throw new Error('Item ID is required');
|
|
140
|
-
if (!question)
|
|
141
|
-
throw new Error('Question is required');
|
|
142
|
-
if (!this.options.apiKey)
|
|
143
|
-
throw new Error('API key is required');
|
|
144
|
-
const url = createAgentUrl({
|
|
145
|
-
itemId,
|
|
146
|
-
threadId,
|
|
147
|
-
variationId,
|
|
148
|
-
question,
|
|
149
|
-
isStreaming: true,
|
|
150
|
-
options: this.options,
|
|
151
|
-
parameters,
|
|
152
|
-
});
|
|
153
|
-
try {
|
|
154
|
-
const eventSource = new EventSource(url);
|
|
155
|
-
eventSource.addEventListener('open', (event) => {
|
|
156
|
-
const data = JSON.parse(event.data);
|
|
157
|
-
if (onStart)
|
|
158
|
-
onStart(data);
|
|
159
|
-
});
|
|
160
|
-
eventSource.addEventListener('message', (event) => {
|
|
161
|
-
const data = JSON.parse(event.data);
|
|
162
|
-
if (onMessage)
|
|
163
|
-
onMessage(data);
|
|
164
|
-
});
|
|
165
|
-
eventSource.addEventListener('end', (event) => {
|
|
166
|
-
const data = JSON.parse(event.data);
|
|
167
|
-
if (onEnd)
|
|
168
|
-
onEnd(data);
|
|
169
|
-
eventSource.close();
|
|
170
|
-
});
|
|
171
|
-
eventSource.onerror = () => {
|
|
172
|
-
eventSource.close();
|
|
173
|
-
throw new Error('Unexpected error occurred. Please try again.');
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
catch (error) {
|
|
177
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
178
|
-
throw new Error(errorMessage);
|
|
179
|
-
}
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
exports.default = MockAgent;
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import ConstructorioClient from '@constructor-io/constructorio-client-javascript';
|
|
2
|
-
import MockAgent from './agent';
|
|
3
|
-
import version from '../../version';
|
|
4
|
-
class MockConstructorIOClient {
|
|
5
|
-
options;
|
|
6
|
-
search;
|
|
7
|
-
browse;
|
|
8
|
-
recommendations;
|
|
9
|
-
tracker;
|
|
10
|
-
quizzes;
|
|
11
|
-
agent;
|
|
12
|
-
constructor(options) {
|
|
13
|
-
const isServer = typeof window === 'undefined' || typeof document === 'undefined';
|
|
14
|
-
const clientOptions = {
|
|
15
|
-
version: options.version || `cio-ui-pia-${version}`,
|
|
16
|
-
serviceUrl: options.serviceUrl || 'https://ac.cnstrc.com',
|
|
17
|
-
quizzesServiceUrl: options.quizzesServiceUrl || 'https://quizzes.cnstrc.com',
|
|
18
|
-
agentServiceUrl: options.agentServiceUrl || 'https://agent.cnstrc.com',
|
|
19
|
-
...(isServer && {
|
|
20
|
-
clientId: options.clientId || 'this-is-a-random-client-id',
|
|
21
|
-
}),
|
|
22
|
-
sendTrackingEvents: options.sendTrackingEvents !== undefined ? options.sendTrackingEvents : true,
|
|
23
|
-
beaconMode: options.beaconMode !== undefined ? options.beaconMode : true,
|
|
24
|
-
networkParameters: options.networkParameters || {},
|
|
25
|
-
...options,
|
|
26
|
-
};
|
|
27
|
-
const cioClient = new ConstructorioClient(clientOptions);
|
|
28
|
-
const { clientId, sessionId } = cioClient.agent.options;
|
|
29
|
-
this.options = { ...clientOptions, clientId, sessionId };
|
|
30
|
-
this.search = cioClient.search;
|
|
31
|
-
this.browse = cioClient.browse;
|
|
32
|
-
this.recommendations = cioClient.recommendations;
|
|
33
|
-
this.tracker = cioClient.tracker;
|
|
34
|
-
this.quizzes = cioClient.quizzes;
|
|
35
|
-
this.agent = new MockAgent(this.options);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
export default MockConstructorIOClient;
|