@constructor-io/constructorio-ui-pia 2.10.2 → 2.10.4
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 +56 -56
- package/lib/cjs/components/CioPia/CioPia.js +2 -1
- package/lib/cjs/components/Input/Input.js +2 -6
- package/lib/cjs/components/SuggestedQuestion/SuggestedQuestion.js +3 -7
- package/lib/cjs/components/icons/QuestionIcon.js +9 -0
- package/lib/cjs/components/icons/SendIcon.js +9 -0
- package/lib/cjs/components/icons/SparklesIcon.js +11 -0
- package/lib/cjs/components/icons/index.js +12 -0
- package/lib/cjs/errors.js +19 -0
- package/lib/cjs/hooks/mocks/agent.js +13 -8
- package/lib/cjs/hooks/useAnswerResults.js +6 -19
- package/lib/cjs/hooks/useCioPia.js +3 -1
- package/lib/cjs/hooks/useSuggestedQuestions.js +8 -4
- package/lib/cjs/index.js +4 -1
- package/lib/cjs/styles.css +0 -1
- package/lib/cjs/utils/transformers.js +22 -1
- package/lib/cjs/version.js +1 -1
- package/lib/mjs/components/CioPia/CioPia.js +2 -1
- package/lib/mjs/components/Input/Input.js +1 -5
- package/lib/mjs/components/SuggestedQuestion/SuggestedQuestion.js +3 -7
- package/lib/mjs/components/icons/QuestionIcon.js +5 -0
- package/lib/mjs/components/icons/SendIcon.js +5 -0
- package/lib/mjs/components/icons/SparklesIcon.js +7 -0
- package/lib/mjs/components/icons/index.js +3 -0
- package/lib/mjs/errors.js +16 -0
- package/lib/mjs/hooks/mocks/agent.js +13 -8
- package/lib/mjs/hooks/useAnswerResults.js +6 -18
- package/lib/mjs/hooks/useCioPia.js +3 -1
- package/lib/mjs/hooks/useSuggestedQuestions.js +8 -4
- package/lib/mjs/index.js +2 -0
- package/lib/mjs/styles.css +0 -1
- package/lib/mjs/utils/transformers.js +19 -0
- package/lib/mjs/version.js +1 -1
- package/lib/types/components/CioPia/CioPia.d.ts +5 -0
- package/lib/types/components/icons/QuestionIcon.d.ts +2 -0
- package/lib/types/components/icons/SendIcon.d.ts +2 -0
- package/lib/types/components/icons/SparklesIcon.d.ts +2 -0
- package/lib/types/components/icons/index.d.ts +3 -0
- package/lib/types/errors.d.ts +12 -0
- package/lib/types/hooks/mocks/agent.d.ts +1 -1
- package/lib/types/hooks/mocks/types.d.ts +2 -0
- package/lib/types/hooks/useAnswerResults.d.ts +2 -2
- package/lib/types/hooks/useCioPia.d.ts +5 -0
- package/lib/types/hooks/useSuggestedQuestions.d.ts +2 -1
- package/lib/types/index.d.ts +1 -0
- package/lib/types/utils/transformers.d.ts +8 -1
- package/lib/types/version.d.ts +1 -1
- package/package.json +2 -2
|
@@ -19,7 +19,7 @@ const PiaConversation_1 = tslib_1.__importDefault(require("../PiaConversation/Pi
|
|
|
19
19
|
// eslint-disable-next-line complexity
|
|
20
20
|
function CioPia(props) {
|
|
21
21
|
var _a;
|
|
22
|
-
const { apiKey, itemId, itemName, threadId, variationId, cioClient, displayConfigs, componentOverrides, callbacks, formatters, productCardProps, children, translations, suggestedQuestionsParameters, } = props;
|
|
22
|
+
const { apiKey, itemId, itemName, threadId, variationId, cioClient, displayConfigs, componentOverrides, callbacks, formatters, productCardProps, children, translations, suggestedQuestionsParameters, parameters, } = props;
|
|
23
23
|
const { learnMoreUrl, showFeedback, mode = 'default', type = 'inline', showPreviousItems, disclaimerPosition = 'bottom', } = displayConfigs || {};
|
|
24
24
|
const isConversation = mode === 'conversation' || type === 'modal';
|
|
25
25
|
const { priceCurrency } = productCardProps || {};
|
|
@@ -30,6 +30,7 @@ function CioPia(props) {
|
|
|
30
30
|
variationId,
|
|
31
31
|
cioClient,
|
|
32
32
|
suggestedQuestionsParameters,
|
|
33
|
+
parameters,
|
|
33
34
|
formatImageUrl: formatters === null || formatters === void 0 ? void 0 : formatters.formatImageUrl,
|
|
34
35
|
});
|
|
35
36
|
const tracking = (0, useTracking_1.default)({
|
|
@@ -4,11 +4,7 @@ const tslib_1 = require("tslib");
|
|
|
4
4
|
const react_1 = tslib_1.__importStar(require("react"));
|
|
5
5
|
const constructorio_ui_components_1 = require("@constructor-io/constructorio-ui-components");
|
|
6
6
|
const translate_1 = require("../../utils/translate");
|
|
7
|
-
|
|
8
|
-
return (react_1.default.createElement("svg", { width: '16', height: '16', viewBox: '0 0 16 16', fill: 'none', xmlns: 'http://www.w3.org/2000/svg' },
|
|
9
|
-
react_1.default.createElement("g", { id: '.icon-send' },
|
|
10
|
-
react_1.default.createElement("path", { id: 'Vector', d: 'M14.8934 7.09533L14.8884 7.09314L1.54219 1.55752C1.42993 1.51053 1.30778 1.49211 1.18666 1.50388C1.06554 1.51566 0.949225 1.55728 0.848125 1.62502C0.741311 1.69501 0.653573 1.79045 0.592805 1.90277C0.532037 2.01509 0.500145 2.14075 0.5 2.26845V5.80877C0.50006 5.98335 0.561019 6.15243 0.67237 6.28689C0.783722 6.42134 0.938491 6.51274 1.11 6.54533L8.38906 7.89127C8.41767 7.89669 8.44348 7.91193 8.46205 7.93435C8.48062 7.95677 8.49078 7.98497 8.49078 8.01408C8.49078 8.04319 8.48062 8.07139 8.46205 8.09381C8.44348 8.11623 8.41767 8.13147 8.38906 8.13689L1.11031 9.48283C0.938851 9.51533 0.784092 9.60661 0.67269 9.74094C0.561288 9.87528 0.500219 10.0443 0.5 10.2188V13.7597C0.499917 13.8816 0.530111 14.0017 0.587871 14.1091C0.645632 14.2165 0.729152 14.3079 0.830938 14.375C0.953375 14.4564 1.09706 14.4998 1.24406 14.5C1.34626 14.4999 1.4474 14.4794 1.54156 14.4397L14.8875 8.93564L14.8934 8.93283C15.0731 8.85563 15.2262 8.72745 15.3337 8.56415C15.4413 8.40086 15.4986 8.20961 15.4986 8.01408C15.4986 7.81855 15.4413 7.6273 15.3337 7.46401C15.2262 7.30071 15.0731 7.17253 14.8934 7.09533Z', fill: 'white' }))));
|
|
11
|
-
}
|
|
7
|
+
const icons_1 = require("../icons");
|
|
12
8
|
function Input({ value: providedValue, placeholder, disabled = false, onSubmit, onFocus, translations, componentOverride, }) {
|
|
13
9
|
const [value, setValue] = (0, react_1.useState)(providedValue || '');
|
|
14
10
|
(0, react_1.useEffect)(() => {
|
|
@@ -45,6 +41,6 @@ function Input({ value: providedValue, placeholder, disabled = false, onSubmit,
|
|
|
45
41
|
react_1.default.createElement("input", { type: 'text', value: value, onChange: (e) => setValue(e.target.value), onKeyDown: handleSubmitOnEnter, onFocus: onFocus, placeholder: resolvedPlaceholder, disabled: disabled, className: 'cio-pia-input' }),
|
|
46
42
|
react_1.default.createElement("button", { type: 'button', onClick: () => handleSubmit(value), className: 'cio-pia-send-button', disabled: disabled },
|
|
47
43
|
(0, translate_1.translate)('Send', translations),
|
|
48
|
-
react_1.default.createElement(SendIcon, null)))));
|
|
44
|
+
react_1.default.createElement(icons_1.SendIcon, null)))));
|
|
49
45
|
}
|
|
50
46
|
exports.default = Input;
|
|
@@ -2,15 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
const react_1 = tslib_1.__importDefault(require("react"));
|
|
5
|
-
|
|
6
|
-
return (react_1.default.createElement("div", { className: 'cio-pia-suggested-question-icon' },
|
|
7
|
-
react_1.default.createElement("svg", { width: '16', height: '16', viewBox: '0 0 16 16', fill: 'none', xmlns: 'http://www.w3.org/2000/svg' },
|
|
8
|
-
react_1.default.createElement("g", { id: 'help' },
|
|
9
|
-
react_1.default.createElement("path", { id: 'Vector', d: 'M8.00004 1.33331C4.32004 1.33331 1.33337 4.31998 1.33337 7.99998C1.33337 11.68 4.32004 14.6666 8.00004 14.6666C11.68 14.6666 14.6667 11.68 14.6667 7.99998C14.6667 4.31998 11.68 1.33331 8.00004 1.33331ZM8.66671 12.6666H7.33337V11.3333H8.66671V12.6666ZM10.0467 7.49998L9.44671 8.11331C8.96671 8.59998 8.66671 8.99998 8.66671 9.99998H7.33337V9.66665C7.33337 8.93331 7.63337 8.26665 8.11337 7.77998L8.94004 6.93998C9.18671 6.69998 9.33337 6.36665 9.33337 5.99998C9.33337 5.26665 8.73337 4.66665 8.00004 4.66665C7.26671 4.66665 6.66671 5.26665 6.66671 5.99998H5.33337C5.33337 4.52665 6.52671 3.33331 8.00004 3.33331C9.47337 3.33331 10.6667 4.52665 10.6667 5.99998C10.6667 6.58665 10.4267 7.11998 10.0467 7.49998Z', fill: '#0F1324', fillOpacity: '0.6' })))));
|
|
10
|
-
}
|
|
5
|
+
const icons_1 = require("../icons");
|
|
11
6
|
function SuggestedQuestion({ question, onClick }) {
|
|
12
7
|
return (react_1.default.createElement("button", { type: 'button', className: 'cio-pia-suggested-question', onClick: onClick },
|
|
13
|
-
react_1.default.createElement(
|
|
8
|
+
react_1.default.createElement("div", { className: 'cio-pia-suggested-question-icon' },
|
|
9
|
+
react_1.default.createElement(icons_1.QuestionIcon, null)),
|
|
14
10
|
question));
|
|
15
11
|
}
|
|
16
12
|
exports.default = SuggestedQuestion;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const react_1 = tslib_1.__importDefault(require("react"));
|
|
5
|
+
function QuestionIcon() {
|
|
6
|
+
return (react_1.default.createElement("svg", { "aria-hidden": 'true', width: '16', height: '16', viewBox: '0 0 16 16', fill: 'none', xmlns: 'http://www.w3.org/2000/svg' },
|
|
7
|
+
react_1.default.createElement("path", { d: 'M8.00004 1.33331C4.32004 1.33331 1.33337 4.31998 1.33337 7.99998C1.33337 11.68 4.32004 14.6666 8.00004 14.6666C11.68 14.6666 14.6667 11.68 14.6667 7.99998C14.6667 4.31998 11.68 1.33331 8.00004 1.33331ZM8.66671 12.6666H7.33337V11.3333H8.66671V12.6666ZM10.0467 7.49998L9.44671 8.11331C8.96671 8.59998 8.66671 8.99998 8.66671 9.99998H7.33337V9.66665C7.33337 8.93331 7.63337 8.26665 8.11337 7.77998L8.94004 6.93998C9.18671 6.69998 9.33337 6.36665 9.33337 5.99998C9.33337 5.26665 8.73337 4.66665 8.00004 4.66665C7.26671 4.66665 6.66671 5.26665 6.66671 5.99998H5.33337C5.33337 4.52665 6.52671 3.33331 8.00004 3.33331C9.47337 3.33331 10.6667 4.52665 10.6667 5.99998C10.6667 6.58665 10.4267 7.11998 10.0467 7.49998Z', fill: '#0F1324', fillOpacity: '0.6' })));
|
|
8
|
+
}
|
|
9
|
+
exports.default = QuestionIcon;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const react_1 = tslib_1.__importDefault(require("react"));
|
|
5
|
+
function SendIcon() {
|
|
6
|
+
return (react_1.default.createElement("svg", { "aria-hidden": 'true', width: '16', height: '16', viewBox: '0 0 16 16', fill: 'none', xmlns: 'http://www.w3.org/2000/svg' },
|
|
7
|
+
react_1.default.createElement("path", { d: 'M14.8934 7.09533L14.8884 7.09314L1.54219 1.55752C1.42993 1.51053 1.30778 1.49211 1.18666 1.50388C1.06554 1.51566 0.949225 1.55728 0.848125 1.62502C0.741311 1.69501 0.653573 1.79045 0.592805 1.90277C0.532037 2.01509 0.500145 2.14075 0.5 2.26845V5.80877C0.50006 5.98335 0.561019 6.15243 0.67237 6.28689C0.783722 6.42134 0.938491 6.51274 1.11 6.54533L8.38906 7.89127C8.41767 7.89669 8.44348 7.91193 8.46205 7.93435C8.48062 7.95677 8.49078 7.98497 8.49078 8.01408C8.49078 8.04319 8.48062 8.07139 8.46205 8.09381C8.44348 8.11623 8.41767 8.13147 8.38906 8.13689L1.11031 9.48283C0.938851 9.51533 0.784092 9.60661 0.67269 9.74094C0.561288 9.87528 0.500219 10.0443 0.5 10.2188V13.7597C0.499917 13.8816 0.530111 14.0017 0.587871 14.1091C0.645632 14.2165 0.729152 14.3079 0.830938 14.375C0.953375 14.4564 1.09706 14.4998 1.24406 14.5C1.34626 14.4999 1.4474 14.4794 1.54156 14.4397L14.8875 8.93564L14.8934 8.93283C15.0731 8.85563 15.2262 8.72745 15.3337 8.56415C15.4413 8.40086 15.4986 8.20961 15.4986 8.01408C15.4986 7.81855 15.4413 7.6273 15.3337 7.46401C15.2262 7.30071 15.0731 7.17253 14.8934 7.09533Z', fill: 'white' })));
|
|
8
|
+
}
|
|
9
|
+
exports.default = SendIcon;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const react_1 = tslib_1.__importDefault(require("react"));
|
|
5
|
+
function SparklesIcon() {
|
|
6
|
+
return (react_1.default.createElement("svg", { "aria-hidden": 'true', width: '16', height: '16', viewBox: '0 0 16 16', fill: 'none', xmlns: 'http://www.w3.org/2000/svg' },
|
|
7
|
+
react_1.default.createElement("path", { d: 'M12.6667 6.0013L13.5001 4.16797L15.3334 3.33464L13.5001 2.5013L12.6667 0.667969L11.8334 2.5013L10.0001 3.33464L11.8334 4.16797L12.6667 6.0013Z', fill: '#1C1917' }),
|
|
8
|
+
react_1.default.createElement("path", { d: 'M12.6667 10.0013L11.8334 11.8346L10.0001 12.668L11.8334 13.5013L12.6667 15.3346L13.5001 13.5013L15.3334 12.668L13.5001 11.8346L12.6667 10.0013Z', fill: '#1C1917' }),
|
|
9
|
+
react_1.default.createElement("path", { d: 'M7.66675 6.33464L6.00008 2.66797L4.33341 6.33464L0.666748 8.0013L4.33341 9.66797L6.00008 13.3346L7.66675 9.66797L11.3334 8.0013L7.66675 6.33464ZM6.66008 8.6613L6.00008 10.1146L5.34008 8.6613L3.88675 8.0013L5.34008 7.3413L6.00008 5.88797L6.66008 7.3413L8.11341 8.0013L6.66008 8.6613Z', fill: '#1C1917' })));
|
|
10
|
+
}
|
|
11
|
+
exports.default = SparklesIcon;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SparklesIcon = exports.SendIcon = exports.QuestionIcon = void 0;
|
|
7
|
+
var QuestionIcon_1 = require("./QuestionIcon");
|
|
8
|
+
Object.defineProperty(exports, "QuestionIcon", { enumerable: true, get: function () { return __importDefault(QuestionIcon_1).default; } });
|
|
9
|
+
var SendIcon_1 = require("./SendIcon");
|
|
10
|
+
Object.defineProperty(exports, "SendIcon", { enumerable: true, get: function () { return __importDefault(SendIcon_1).default; } });
|
|
11
|
+
var SparklesIcon_1 = require("./SparklesIcon");
|
|
12
|
+
Object.defineProperty(exports, "SparklesIcon", { enumerable: true, get: function () { return __importDefault(SparklesIcon_1).default; } });
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AgentRequestError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Thrown when a Constructor agent request returns a non-2xx response.
|
|
6
|
+
*
|
|
7
|
+
* `status` is the HTTP status code, so callers can act on the kind of failure instead of
|
|
8
|
+
* parsing the message. A 422 means the request itself was rejected as unsuitable, a 429
|
|
9
|
+
* means rate limiting, and anything 500 or above means the service failed and a retry may
|
|
10
|
+
* succeed. This is the error that arrives in `CioPiaRenderProps.error` when a request fails.
|
|
11
|
+
*/
|
|
12
|
+
class AgentRequestError extends Error {
|
|
13
|
+
constructor(status) {
|
|
14
|
+
super(`Request failed with status ${status}`);
|
|
15
|
+
this.name = 'AgentRequestError';
|
|
16
|
+
this.status = status;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.AgentRequestError = AgentRequestError;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
|
+
const errors_1 = require("../../errors");
|
|
4
5
|
// Create URL for PIA API
|
|
5
6
|
function createAgentUrl({ itemId, threadId, variationId, question, isStreaming = false, options, parameters = {}, }) {
|
|
6
7
|
const { apiKey, agentServiceUrl, clientId, sessionId, userId, segments, version } = options;
|
|
@@ -56,7 +57,7 @@ class MockAgent {
|
|
|
56
57
|
constructor(options) {
|
|
57
58
|
this.options = options;
|
|
58
59
|
}
|
|
59
|
-
getSuggestedQuestions({ itemId, variationId, threadId, parameters = {}, }) {
|
|
60
|
+
getSuggestedQuestions({ itemId, variationId, threadId, parameters = {}, requestParameters = {}, }) {
|
|
60
61
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
61
62
|
if (!itemId)
|
|
62
63
|
throw new Error('Item ID is required');
|
|
@@ -67,18 +68,20 @@ class MockAgent {
|
|
|
67
68
|
variationId,
|
|
68
69
|
threadId,
|
|
69
70
|
options: this.options,
|
|
70
|
-
parameters: mapSuggestedQuestionsParams(parameters),
|
|
71
|
+
parameters: Object.assign(Object.assign({}, requestParameters), mapSuggestedQuestionsParams(parameters)),
|
|
71
72
|
});
|
|
72
73
|
try {
|
|
73
74
|
const response = yield fetch(url);
|
|
74
75
|
if (!response.ok)
|
|
75
|
-
throw new
|
|
76
|
+
throw new errors_1.AgentRequestError(response.status);
|
|
76
77
|
const data = yield response.json();
|
|
77
78
|
return data;
|
|
78
79
|
}
|
|
79
80
|
catch (error) {
|
|
80
|
-
|
|
81
|
-
|
|
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));
|
|
82
85
|
}
|
|
83
86
|
});
|
|
84
87
|
}
|
|
@@ -101,13 +104,15 @@ class MockAgent {
|
|
|
101
104
|
try {
|
|
102
105
|
const response = yield fetch(url);
|
|
103
106
|
if (!response.ok)
|
|
104
|
-
throw new
|
|
107
|
+
throw new errors_1.AgentRequestError(response.status);
|
|
105
108
|
const data = yield response.json();
|
|
106
109
|
return data;
|
|
107
110
|
}
|
|
108
111
|
catch (error) {
|
|
109
|
-
|
|
110
|
-
|
|
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));
|
|
111
116
|
}
|
|
112
117
|
});
|
|
113
118
|
}
|
|
@@ -3,41 +3,28 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
const react_1 = require("react");
|
|
5
5
|
const transformers_1 = require("../utils/transformers");
|
|
6
|
-
const
|
|
7
|
-
var _a, _b;
|
|
8
|
-
if (!((_b = (_a = data === null || data === void 0 ? void 0 : data.item_results) === null || _a === void 0 ? void 0 : _a.response) === null || _b === void 0 ? void 0 : _b.results)) {
|
|
9
|
-
return null;
|
|
10
|
-
}
|
|
11
|
-
const { results } = data.item_results.response;
|
|
12
|
-
if (!Array.isArray(results) || results.length === 0) {
|
|
13
|
-
return null;
|
|
14
|
-
}
|
|
15
|
-
const transformedItems = results
|
|
16
|
-
.map((item) => (0, transformers_1.transformResultItem)(item, formatImageUrl))
|
|
17
|
-
.filter((item) => item !== null);
|
|
18
|
-
return transformedItems.length > 0 ? transformedItems : null;
|
|
19
|
-
};
|
|
20
|
-
const fetchAnswerResults = ({ client, itemId, question, variationId, threadId, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
6
|
+
const fetchAnswerResults = ({ client, itemId, question, variationId, threadId, parameters, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
21
7
|
const response = yield client.agent.getAnswerResults({
|
|
22
8
|
itemId,
|
|
23
9
|
variationId,
|
|
24
10
|
threadId,
|
|
25
11
|
question,
|
|
12
|
+
parameters,
|
|
26
13
|
});
|
|
27
14
|
return response;
|
|
28
15
|
});
|
|
29
|
-
function useAnswerResults({ itemId, variationId, threadId, cioClient, formatImageUrl, }) {
|
|
16
|
+
function useAnswerResults({ itemId, variationId, threadId, cioClient, parameters, formatImageUrl, }) {
|
|
30
17
|
const [answerResults, setAnswerResults] = (0, react_1.useState)(null);
|
|
31
18
|
const [isLoading, setIsLoading] = (0, react_1.useState)(false);
|
|
32
19
|
const [error, setError] = (0, react_1.useState)(null);
|
|
33
|
-
const items = (0, react_1.useMemo)(() => extractAndTransformItems(answerResults, formatImageUrl), [answerResults, formatImageUrl]);
|
|
20
|
+
const items = (0, react_1.useMemo)(() => (0, transformers_1.extractAndTransformItems)(answerResults, formatImageUrl), [answerResults, formatImageUrl]);
|
|
34
21
|
const fetchResult = (0, react_1.useCallback)((question) => {
|
|
35
22
|
if (!cioClient)
|
|
36
23
|
return;
|
|
37
24
|
setIsLoading(true);
|
|
38
25
|
setError(null);
|
|
39
26
|
setAnswerResults(null);
|
|
40
|
-
fetchAnswerResults({ client: cioClient, itemId, question, variationId, threadId })
|
|
27
|
+
fetchAnswerResults({ client: cioClient, itemId, question, variationId, threadId, parameters })
|
|
41
28
|
.then((fetchedAnswerResults) => {
|
|
42
29
|
setAnswerResults(fetchedAnswerResults);
|
|
43
30
|
setError(null);
|
|
@@ -49,7 +36,7 @@ function useAnswerResults({ itemId, variationId, threadId, cioClient, formatImag
|
|
|
49
36
|
.finally(() => {
|
|
50
37
|
setIsLoading(false);
|
|
51
38
|
});
|
|
52
|
-
}, [cioClient, itemId, variationId, threadId]);
|
|
39
|
+
}, [cioClient, itemId, variationId, threadId, parameters]);
|
|
53
40
|
return {
|
|
54
41
|
data: answerResults,
|
|
55
42
|
items,
|
|
@@ -7,7 +7,7 @@ const useAnswerResults_1 = tslib_1.__importDefault(require("./useAnswerResults")
|
|
|
7
7
|
const useSuggestedQuestions_1 = tslib_1.__importDefault(require("./useSuggestedQuestions"));
|
|
8
8
|
const version_1 = tslib_1.__importDefault(require("../version"));
|
|
9
9
|
function useCioPia(props) {
|
|
10
|
-
const { apiKey, itemId, variationId, threadId: providedThreadId, cioClient: providedClient, suggestedQuestionsParameters, formatImageUrl, } = props;
|
|
10
|
+
const { apiKey, itemId, variationId, threadId: providedThreadId, cioClient: providedClient, suggestedQuestionsParameters, parameters, formatImageUrl, } = props;
|
|
11
11
|
const [generatedThreadId] = (0, react_1.useState)(() => crypto.randomUUID());
|
|
12
12
|
const threadId = providedThreadId || generatedThreadId;
|
|
13
13
|
const client = (0, react_1.useMemo)(() => {
|
|
@@ -25,12 +25,14 @@ function useCioPia(props) {
|
|
|
25
25
|
threadId,
|
|
26
26
|
cioClient: client,
|
|
27
27
|
parameters: suggestedQuestionsParameters,
|
|
28
|
+
requestParameters: parameters,
|
|
28
29
|
});
|
|
29
30
|
const answers = (0, useAnswerResults_1.default)({
|
|
30
31
|
itemId,
|
|
31
32
|
variationId,
|
|
32
33
|
threadId,
|
|
33
34
|
cioClient: client,
|
|
35
|
+
parameters,
|
|
34
36
|
formatImageUrl,
|
|
35
37
|
});
|
|
36
38
|
return {
|
|
@@ -2,19 +2,22 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
const react_1 = require("react");
|
|
5
|
-
const fetchSuggestedQuestions = ({ client, itemId, variationId, threadId, parameters, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
5
|
+
const fetchSuggestedQuestions = ({ client, itemId, variationId, threadId, parameters, requestParameters, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
6
6
|
const response = yield client.agent.getSuggestedQuestions({
|
|
7
7
|
itemId,
|
|
8
8
|
variationId,
|
|
9
9
|
threadId,
|
|
10
10
|
parameters,
|
|
11
|
+
requestParameters,
|
|
11
12
|
});
|
|
12
13
|
return response.questions;
|
|
13
14
|
});
|
|
14
|
-
function useSuggestedQuestions({ itemId, variationId, threadId, cioClient, parameters, }) {
|
|
15
|
+
function useSuggestedQuestions({ itemId, variationId, threadId, cioClient, parameters, requestParameters, }) {
|
|
15
16
|
const [questions, setQuestions] = (0, react_1.useState)([]);
|
|
16
17
|
const [isLoading, setIsLoading] = (0, react_1.useState)(false);
|
|
17
18
|
const [error, setError] = (0, react_1.useState)(null);
|
|
19
|
+
// Serialize to a primitive so an object-literal prop doesn't retrigger the auto-fetch effect each render.
|
|
20
|
+
const requestParametersKey = JSON.stringify(requestParameters);
|
|
18
21
|
const fetchResult = (0, react_1.useCallback)(() => {
|
|
19
22
|
if (!cioClient)
|
|
20
23
|
return;
|
|
@@ -26,6 +29,7 @@ function useSuggestedQuestions({ itemId, variationId, threadId, cioClient, param
|
|
|
26
29
|
variationId,
|
|
27
30
|
threadId,
|
|
28
31
|
parameters,
|
|
32
|
+
requestParameters,
|
|
29
33
|
})
|
|
30
34
|
.then((fetchedQuestions) => {
|
|
31
35
|
setQuestions(fetchedQuestions);
|
|
@@ -37,8 +41,8 @@ function useSuggestedQuestions({ itemId, variationId, threadId, cioClient, param
|
|
|
37
41
|
.finally(() => {
|
|
38
42
|
setIsLoading(false);
|
|
39
43
|
});
|
|
40
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps -- primitive
|
|
41
|
-
}, [cioClient, itemId, variationId, threadId, parameters === null || parameters === void 0 ? void 0 : parameters.numResults]);
|
|
44
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- primitive deps prevent refetch loop
|
|
45
|
+
}, [cioClient, itemId, variationId, threadId, parameters === null || parameters === void 0 ? void 0 : parameters.numResults, requestParametersKey]);
|
|
42
46
|
(0, react_1.useEffect)(() => {
|
|
43
47
|
fetchResult();
|
|
44
48
|
}, [fetchResult]);
|
package/lib/cjs/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.renderMarkdown = exports.sanitizeHtml = exports.CioPia = void 0;
|
|
3
|
+
exports.AgentRequestError = exports.renderMarkdown = exports.sanitizeHtml = exports.CioPia = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const CioPia_1 = tslib_1.__importDefault(require("./components/CioPia"));
|
|
6
6
|
// Components
|
|
@@ -10,6 +10,9 @@ Object.defineProperty(exports, "CioPia", { enumerable: true, get: function () {
|
|
|
10
10
|
var contentTransformers_1 = require("./utils/contentTransformers");
|
|
11
11
|
Object.defineProperty(exports, "sanitizeHtml", { enumerable: true, get: function () { return contentTransformers_1.sanitizeHtml; } });
|
|
12
12
|
Object.defineProperty(exports, "renderMarkdown", { enumerable: true, get: function () { return contentTransformers_1.renderMarkdown; } });
|
|
13
|
+
// Errors
|
|
14
|
+
var errors_1 = require("./errors");
|
|
15
|
+
Object.defineProperty(exports, "AgentRequestError", { enumerable: true, get: function () { return errors_1.AgentRequestError; } });
|
|
13
16
|
// Default
|
|
14
17
|
tslib_1.__exportStar(require("./types"), exports);
|
|
15
18
|
exports.default = CioPia_1.default;
|
package/lib/cjs/styles.css
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
@import '@constructor-io/constructorio-ui-components/styles.css';
|
|
2
|
-
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;700&display=swap');
|
|
3
2
|
@import './components/SuggestedQuestion/SuggestedQuestionStyles.css';
|
|
4
3
|
@import './components/SuggestedQuestionsContainer/SuggestedQuestionsContainerStyles.css';
|
|
5
4
|
@import './components/Input/InputStyles.css';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.transformResultItem = void 0;
|
|
3
|
+
exports.extractAndTransformItems = exports.transformResultItem = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
/**
|
|
6
6
|
* Converts a raw ApiItem from the Get Answers API response into an Item object
|
|
@@ -35,3 +35,24 @@ function transformResultItem(resultItem, formatImageUrl) {
|
|
|
35
35
|
badge, data: Object.assign({}, otherMetadataFields) }, otherFields);
|
|
36
36
|
}
|
|
37
37
|
exports.transformResultItem = transformResultItem;
|
|
38
|
+
/**
|
|
39
|
+
* Pulls the product results out of an answers response and converts them to Items.
|
|
40
|
+
*
|
|
41
|
+
* Returns `null` rather than an empty array whenever there is nothing to render, so callers
|
|
42
|
+
* can treat "no products" as a single case.
|
|
43
|
+
*/
|
|
44
|
+
function extractAndTransformItems(data, formatImageUrl) {
|
|
45
|
+
var _a, _b;
|
|
46
|
+
if (!((_b = (_a = data === null || data === void 0 ? void 0 : data.item_results) === null || _a === void 0 ? void 0 : _a.response) === null || _b === void 0 ? void 0 : _b.results)) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
const { results } = data.item_results.response;
|
|
50
|
+
if (!Array.isArray(results) || results.length === 0) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
const transformedItems = results
|
|
54
|
+
.map((item) => transformResultItem(item, formatImageUrl))
|
|
55
|
+
.filter((item) => item !== null);
|
|
56
|
+
return transformedItems.length > 0 ? transformedItems : null;
|
|
57
|
+
}
|
|
58
|
+
exports.extractAndTransformItems = extractAndTransformItems;
|
package/lib/cjs/version.js
CHANGED
|
@@ -15,7 +15,7 @@ import PiaModal from '../PiaConversation/PiaModal';
|
|
|
15
15
|
import PiaConversation from '../PiaConversation/PiaConversation';
|
|
16
16
|
// eslint-disable-next-line complexity
|
|
17
17
|
export default function CioPia(props) {
|
|
18
|
-
const { apiKey, itemId, itemName, threadId, variationId, cioClient, displayConfigs, componentOverrides, callbacks, formatters, productCardProps, children, translations, suggestedQuestionsParameters, } = props;
|
|
18
|
+
const { apiKey, itemId, itemName, threadId, variationId, cioClient, displayConfigs, componentOverrides, callbacks, formatters, productCardProps, children, translations, suggestedQuestionsParameters, parameters, } = props;
|
|
19
19
|
const { learnMoreUrl, showFeedback, mode = 'default', type = 'inline', showPreviousItems, disclaimerPosition = 'bottom', } = displayConfigs || {};
|
|
20
20
|
const isConversation = mode === 'conversation' || type === 'modal';
|
|
21
21
|
const { priceCurrency } = productCardProps || {};
|
|
@@ -26,6 +26,7 @@ export default function CioPia(props) {
|
|
|
26
26
|
variationId,
|
|
27
27
|
cioClient,
|
|
28
28
|
suggestedQuestionsParameters,
|
|
29
|
+
parameters,
|
|
29
30
|
formatImageUrl: formatters?.formatImageUrl,
|
|
30
31
|
});
|
|
31
32
|
const tracking = useTracking({
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import React, { useCallback, useEffect, useState } from 'react';
|
|
2
2
|
import { RenderPropsWrapper, } from '@constructor-io/constructorio-ui-components';
|
|
3
3
|
import { translate } from '../../utils/translate';
|
|
4
|
-
|
|
5
|
-
return (React.createElement("svg", { width: '16', height: '16', viewBox: '0 0 16 16', fill: 'none', xmlns: 'http://www.w3.org/2000/svg' },
|
|
6
|
-
React.createElement("g", { id: '.icon-send' },
|
|
7
|
-
React.createElement("path", { id: 'Vector', d: 'M14.8934 7.09533L14.8884 7.09314L1.54219 1.55752C1.42993 1.51053 1.30778 1.49211 1.18666 1.50388C1.06554 1.51566 0.949225 1.55728 0.848125 1.62502C0.741311 1.69501 0.653573 1.79045 0.592805 1.90277C0.532037 2.01509 0.500145 2.14075 0.5 2.26845V5.80877C0.50006 5.98335 0.561019 6.15243 0.67237 6.28689C0.783722 6.42134 0.938491 6.51274 1.11 6.54533L8.38906 7.89127C8.41767 7.89669 8.44348 7.91193 8.46205 7.93435C8.48062 7.95677 8.49078 7.98497 8.49078 8.01408C8.49078 8.04319 8.48062 8.07139 8.46205 8.09381C8.44348 8.11623 8.41767 8.13147 8.38906 8.13689L1.11031 9.48283C0.938851 9.51533 0.784092 9.60661 0.67269 9.74094C0.561288 9.87528 0.500219 10.0443 0.5 10.2188V13.7597C0.499917 13.8816 0.530111 14.0017 0.587871 14.1091C0.645632 14.2165 0.729152 14.3079 0.830938 14.375C0.953375 14.4564 1.09706 14.4998 1.24406 14.5C1.34626 14.4999 1.4474 14.4794 1.54156 14.4397L14.8875 8.93564L14.8934 8.93283C15.0731 8.85563 15.2262 8.72745 15.3337 8.56415C15.4413 8.40086 15.4986 8.20961 15.4986 8.01408C15.4986 7.81855 15.4413 7.6273 15.3337 7.46401C15.2262 7.30071 15.0731 7.17253 14.8934 7.09533Z', fill: 'white' }))));
|
|
8
|
-
}
|
|
4
|
+
import { SendIcon } from '../icons';
|
|
9
5
|
function Input({ value: providedValue, placeholder, disabled = false, onSubmit, onFocus, translations, componentOverride, }) {
|
|
10
6
|
const [value, setValue] = useState(providedValue || '');
|
|
11
7
|
useEffect(() => {
|
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
|
|
3
|
-
return (React.createElement("div", { className: 'cio-pia-suggested-question-icon' },
|
|
4
|
-
React.createElement("svg", { width: '16', height: '16', viewBox: '0 0 16 16', fill: 'none', xmlns: 'http://www.w3.org/2000/svg' },
|
|
5
|
-
React.createElement("g", { id: 'help' },
|
|
6
|
-
React.createElement("path", { id: 'Vector', d: 'M8.00004 1.33331C4.32004 1.33331 1.33337 4.31998 1.33337 7.99998C1.33337 11.68 4.32004 14.6666 8.00004 14.6666C11.68 14.6666 14.6667 11.68 14.6667 7.99998C14.6667 4.31998 11.68 1.33331 8.00004 1.33331ZM8.66671 12.6666H7.33337V11.3333H8.66671V12.6666ZM10.0467 7.49998L9.44671 8.11331C8.96671 8.59998 8.66671 8.99998 8.66671 9.99998H7.33337V9.66665C7.33337 8.93331 7.63337 8.26665 8.11337 7.77998L8.94004 6.93998C9.18671 6.69998 9.33337 6.36665 9.33337 5.99998C9.33337 5.26665 8.73337 4.66665 8.00004 4.66665C7.26671 4.66665 6.66671 5.26665 6.66671 5.99998H5.33337C5.33337 4.52665 6.52671 3.33331 8.00004 3.33331C9.47337 3.33331 10.6667 4.52665 10.6667 5.99998C10.6667 6.58665 10.4267 7.11998 10.0467 7.49998Z', fill: '#0F1324', fillOpacity: '0.6' })))));
|
|
7
|
-
}
|
|
2
|
+
import { QuestionIcon } from '../icons';
|
|
8
3
|
function SuggestedQuestion({ question, onClick }) {
|
|
9
4
|
return (React.createElement("button", { type: 'button', className: 'cio-pia-suggested-question', onClick: onClick },
|
|
10
|
-
React.createElement(
|
|
5
|
+
React.createElement("div", { className: 'cio-pia-suggested-question-icon' },
|
|
6
|
+
React.createElement(QuestionIcon, null)),
|
|
11
7
|
question));
|
|
12
8
|
}
|
|
13
9
|
export default SuggestedQuestion;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export default function QuestionIcon() {
|
|
3
|
+
return (React.createElement("svg", { "aria-hidden": 'true', width: '16', height: '16', viewBox: '0 0 16 16', fill: 'none', xmlns: 'http://www.w3.org/2000/svg' },
|
|
4
|
+
React.createElement("path", { d: 'M8.00004 1.33331C4.32004 1.33331 1.33337 4.31998 1.33337 7.99998C1.33337 11.68 4.32004 14.6666 8.00004 14.6666C11.68 14.6666 14.6667 11.68 14.6667 7.99998C14.6667 4.31998 11.68 1.33331 8.00004 1.33331ZM8.66671 12.6666H7.33337V11.3333H8.66671V12.6666ZM10.0467 7.49998L9.44671 8.11331C8.96671 8.59998 8.66671 8.99998 8.66671 9.99998H7.33337V9.66665C7.33337 8.93331 7.63337 8.26665 8.11337 7.77998L8.94004 6.93998C9.18671 6.69998 9.33337 6.36665 9.33337 5.99998C9.33337 5.26665 8.73337 4.66665 8.00004 4.66665C7.26671 4.66665 6.66671 5.26665 6.66671 5.99998H5.33337C5.33337 4.52665 6.52671 3.33331 8.00004 3.33331C9.47337 3.33331 10.6667 4.52665 10.6667 5.99998C10.6667 6.58665 10.4267 7.11998 10.0467 7.49998Z', fill: '#0F1324', fillOpacity: '0.6' })));
|
|
5
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export default function SendIcon() {
|
|
3
|
+
return (React.createElement("svg", { "aria-hidden": 'true', width: '16', height: '16', viewBox: '0 0 16 16', fill: 'none', xmlns: 'http://www.w3.org/2000/svg' },
|
|
4
|
+
React.createElement("path", { d: 'M14.8934 7.09533L14.8884 7.09314L1.54219 1.55752C1.42993 1.51053 1.30778 1.49211 1.18666 1.50388C1.06554 1.51566 0.949225 1.55728 0.848125 1.62502C0.741311 1.69501 0.653573 1.79045 0.592805 1.90277C0.532037 2.01509 0.500145 2.14075 0.5 2.26845V5.80877C0.50006 5.98335 0.561019 6.15243 0.67237 6.28689C0.783722 6.42134 0.938491 6.51274 1.11 6.54533L8.38906 7.89127C8.41767 7.89669 8.44348 7.91193 8.46205 7.93435C8.48062 7.95677 8.49078 7.98497 8.49078 8.01408C8.49078 8.04319 8.48062 8.07139 8.46205 8.09381C8.44348 8.11623 8.41767 8.13147 8.38906 8.13689L1.11031 9.48283C0.938851 9.51533 0.784092 9.60661 0.67269 9.74094C0.561288 9.87528 0.500219 10.0443 0.5 10.2188V13.7597C0.499917 13.8816 0.530111 14.0017 0.587871 14.1091C0.645632 14.2165 0.729152 14.3079 0.830938 14.375C0.953375 14.4564 1.09706 14.4998 1.24406 14.5C1.34626 14.4999 1.4474 14.4794 1.54156 14.4397L14.8875 8.93564L14.8934 8.93283C15.0731 8.85563 15.2262 8.72745 15.3337 8.56415C15.4413 8.40086 15.4986 8.20961 15.4986 8.01408C15.4986 7.81855 15.4413 7.6273 15.3337 7.46401C15.2262 7.30071 15.0731 7.17253 14.8934 7.09533Z', fill: 'white' })));
|
|
5
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export default function SparklesIcon() {
|
|
3
|
+
return (React.createElement("svg", { "aria-hidden": 'true', width: '16', height: '16', viewBox: '0 0 16 16', fill: 'none', xmlns: 'http://www.w3.org/2000/svg' },
|
|
4
|
+
React.createElement("path", { d: 'M12.6667 6.0013L13.5001 4.16797L15.3334 3.33464L13.5001 2.5013L12.6667 0.667969L11.8334 2.5013L10.0001 3.33464L11.8334 4.16797L12.6667 6.0013Z', fill: '#1C1917' }),
|
|
5
|
+
React.createElement("path", { d: 'M12.6667 10.0013L11.8334 11.8346L10.0001 12.668L11.8334 13.5013L12.6667 15.3346L13.5001 13.5013L15.3334 12.668L13.5001 11.8346L12.6667 10.0013Z', fill: '#1C1917' }),
|
|
6
|
+
React.createElement("path", { d: 'M7.66675 6.33464L6.00008 2.66797L4.33341 6.33464L0.666748 8.0013L4.33341 9.66797L6.00008 13.3346L7.66675 9.66797L11.3334 8.0013L7.66675 6.33464ZM6.66008 8.6613L6.00008 10.1146L5.34008 8.6613L3.88675 8.0013L5.34008 7.3413L6.00008 5.88797L6.66008 7.3413L8.11341 8.0013L6.66008 8.6613Z', fill: '#1C1917' })));
|
|
7
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when a Constructor agent request returns a non-2xx response.
|
|
3
|
+
*
|
|
4
|
+
* `status` is the HTTP status code, so callers can act on the kind of failure instead of
|
|
5
|
+
* parsing the message. A 422 means the request itself was rejected as unsuitable, a 429
|
|
6
|
+
* means rate limiting, and anything 500 or above means the service failed and a retry may
|
|
7
|
+
* succeed. This is the error that arrives in `CioPiaRenderProps.error` when a request fails.
|
|
8
|
+
*/
|
|
9
|
+
export class AgentRequestError extends Error {
|
|
10
|
+
status;
|
|
11
|
+
constructor(status) {
|
|
12
|
+
super(`Request failed with status ${status}`);
|
|
13
|
+
this.name = 'AgentRequestError';
|
|
14
|
+
this.status = status;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AgentRequestError } from '../../errors';
|
|
1
2
|
// Create URL for PIA API
|
|
2
3
|
function createAgentUrl({ itemId, threadId, variationId, question, isStreaming = false, options, parameters = {}, }) {
|
|
3
4
|
const { apiKey, agentServiceUrl, clientId, sessionId, userId, segments, version } = options;
|
|
@@ -54,7 +55,7 @@ class MockAgent {
|
|
|
54
55
|
constructor(options) {
|
|
55
56
|
this.options = options;
|
|
56
57
|
}
|
|
57
|
-
async getSuggestedQuestions({ itemId, variationId, threadId, parameters = {}, }) {
|
|
58
|
+
async getSuggestedQuestions({ itemId, variationId, threadId, parameters = {}, requestParameters = {}, }) {
|
|
58
59
|
if (!itemId)
|
|
59
60
|
throw new Error('Item ID is required');
|
|
60
61
|
if (!this.options.apiKey)
|
|
@@ -64,18 +65,20 @@ class MockAgent {
|
|
|
64
65
|
variationId,
|
|
65
66
|
threadId,
|
|
66
67
|
options: this.options,
|
|
67
|
-
parameters: mapSuggestedQuestionsParams(parameters),
|
|
68
|
+
parameters: { ...requestParameters, ...mapSuggestedQuestionsParams(parameters) },
|
|
68
69
|
});
|
|
69
70
|
try {
|
|
70
71
|
const response = await fetch(url);
|
|
71
72
|
if (!response.ok)
|
|
72
|
-
throw new
|
|
73
|
+
throw new AgentRequestError(response.status);
|
|
73
74
|
const data = await response.json();
|
|
74
75
|
return data;
|
|
75
76
|
}
|
|
76
77
|
catch (error) {
|
|
77
|
-
|
|
78
|
-
|
|
78
|
+
// Rethrow untouched so the status code and the original stack survive.
|
|
79
|
+
if (error instanceof Error)
|
|
80
|
+
throw error;
|
|
81
|
+
throw new Error(String(error));
|
|
79
82
|
}
|
|
80
83
|
}
|
|
81
84
|
async getAnswerResults({ itemId, variationId, threadId, question, parameters = {}, }) {
|
|
@@ -96,13 +99,15 @@ class MockAgent {
|
|
|
96
99
|
try {
|
|
97
100
|
const response = await fetch(url);
|
|
98
101
|
if (!response.ok)
|
|
99
|
-
throw new
|
|
102
|
+
throw new AgentRequestError(response.status);
|
|
100
103
|
const data = await response.json();
|
|
101
104
|
return data;
|
|
102
105
|
}
|
|
103
106
|
catch (error) {
|
|
104
|
-
|
|
105
|
-
|
|
107
|
+
// Rethrow untouched so the status code and the original stack survive.
|
|
108
|
+
if (error instanceof Error)
|
|
109
|
+
throw error;
|
|
110
|
+
throw new Error(String(error));
|
|
106
111
|
}
|
|
107
112
|
}
|
|
108
113
|
async getAnswerResultsStream({ itemId, threadId, variationId, question, parameters, onStart, onMessage, onEnd, }) {
|
|
@@ -1,28 +1,16 @@
|
|
|
1
1
|
import { useCallback, useMemo, useState } from 'react';
|
|
2
|
-
import {
|
|
3
|
-
const
|
|
4
|
-
if (!data?.item_results?.response?.results) {
|
|
5
|
-
return null;
|
|
6
|
-
}
|
|
7
|
-
const { results } = data.item_results.response;
|
|
8
|
-
if (!Array.isArray(results) || results.length === 0) {
|
|
9
|
-
return null;
|
|
10
|
-
}
|
|
11
|
-
const transformedItems = results
|
|
12
|
-
.map((item) => transformResultItem(item, formatImageUrl))
|
|
13
|
-
.filter((item) => item !== null);
|
|
14
|
-
return transformedItems.length > 0 ? transformedItems : null;
|
|
15
|
-
};
|
|
16
|
-
const fetchAnswerResults = async ({ client, itemId, question, variationId, threadId, }) => {
|
|
2
|
+
import { extractAndTransformItems } from '../utils/transformers';
|
|
3
|
+
const fetchAnswerResults = async ({ client, itemId, question, variationId, threadId, parameters, }) => {
|
|
17
4
|
const response = await client.agent.getAnswerResults({
|
|
18
5
|
itemId,
|
|
19
6
|
variationId,
|
|
20
7
|
threadId,
|
|
21
8
|
question,
|
|
9
|
+
parameters,
|
|
22
10
|
});
|
|
23
11
|
return response;
|
|
24
12
|
};
|
|
25
|
-
export default function useAnswerResults({ itemId, variationId, threadId, cioClient, formatImageUrl, }) {
|
|
13
|
+
export default function useAnswerResults({ itemId, variationId, threadId, cioClient, parameters, formatImageUrl, }) {
|
|
26
14
|
const [answerResults, setAnswerResults] = useState(null);
|
|
27
15
|
const [isLoading, setIsLoading] = useState(false);
|
|
28
16
|
const [error, setError] = useState(null);
|
|
@@ -33,7 +21,7 @@ export default function useAnswerResults({ itemId, variationId, threadId, cioCli
|
|
|
33
21
|
setIsLoading(true);
|
|
34
22
|
setError(null);
|
|
35
23
|
setAnswerResults(null);
|
|
36
|
-
fetchAnswerResults({ client: cioClient, itemId, question, variationId, threadId })
|
|
24
|
+
fetchAnswerResults({ client: cioClient, itemId, question, variationId, threadId, parameters })
|
|
37
25
|
.then((fetchedAnswerResults) => {
|
|
38
26
|
setAnswerResults(fetchedAnswerResults);
|
|
39
27
|
setError(null);
|
|
@@ -45,7 +33,7 @@ export default function useAnswerResults({ itemId, variationId, threadId, cioCli
|
|
|
45
33
|
.finally(() => {
|
|
46
34
|
setIsLoading(false);
|
|
47
35
|
});
|
|
48
|
-
}, [cioClient, itemId, variationId, threadId]);
|
|
36
|
+
}, [cioClient, itemId, variationId, threadId, parameters]);
|
|
49
37
|
return {
|
|
50
38
|
data: answerResults,
|
|
51
39
|
items,
|
|
@@ -4,7 +4,7 @@ import useAnswerResults from './useAnswerResults';
|
|
|
4
4
|
import useSuggestedQuestions from './useSuggestedQuestions';
|
|
5
5
|
import version from '../version';
|
|
6
6
|
export default function useCioPia(props) {
|
|
7
|
-
const { apiKey, itemId, variationId, threadId: providedThreadId, cioClient: providedClient, suggestedQuestionsParameters, formatImageUrl, } = props;
|
|
7
|
+
const { apiKey, itemId, variationId, threadId: providedThreadId, cioClient: providedClient, suggestedQuestionsParameters, parameters, formatImageUrl, } = props;
|
|
8
8
|
const [generatedThreadId] = useState(() => crypto.randomUUID());
|
|
9
9
|
const threadId = providedThreadId || generatedThreadId;
|
|
10
10
|
const client = useMemo(() => {
|
|
@@ -22,12 +22,14 @@ export default function useCioPia(props) {
|
|
|
22
22
|
threadId,
|
|
23
23
|
cioClient: client,
|
|
24
24
|
parameters: suggestedQuestionsParameters,
|
|
25
|
+
requestParameters: parameters,
|
|
25
26
|
});
|
|
26
27
|
const answers = useAnswerResults({
|
|
27
28
|
itemId,
|
|
28
29
|
variationId,
|
|
29
30
|
threadId,
|
|
30
31
|
cioClient: client,
|
|
32
|
+
parameters,
|
|
31
33
|
formatImageUrl,
|
|
32
34
|
});
|
|
33
35
|
return {
|