@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.
Files changed (37) hide show
  1. package/dist/constructorio-ui-pia-bundled.js +60 -60
  2. package/lib/cjs/components/CioPia/CioPiaQna.js +2 -1
  3. package/lib/cjs/components/CioPia/PiaCustomCarousel.js +24 -29
  4. package/lib/cjs/hooks/useAnswerResults.js +4 -14
  5. package/lib/cjs/hooks/useCioPia.js +42 -4
  6. package/lib/cjs/hooks/usePiaClient.js +2 -7
  7. package/lib/cjs/hooks/useRecsPod.js +10 -5
  8. package/lib/cjs/hooks/useSuggestedQuestions.js +10 -26
  9. package/lib/cjs/types.js +0 -2
  10. package/lib/cjs/version.js +1 -1
  11. package/lib/mjs/components/CioPia/CioPiaQna.js +2 -1
  12. package/lib/mjs/components/CioPia/PiaCustomCarousel.js +43 -30
  13. package/lib/mjs/hooks/useAnswerResults.js +4 -13
  14. package/lib/mjs/hooks/useCioPia.js +43 -4
  15. package/lib/mjs/hooks/usePiaClient.js +2 -7
  16. package/lib/mjs/hooks/useRecsPod.js +9 -5
  17. package/lib/mjs/hooks/useSuggestedQuestions.js +12 -24
  18. package/lib/mjs/types.js +0 -1
  19. package/lib/mjs/version.js +1 -1
  20. package/lib/types/components/CioPia/types.d.ts +20 -5
  21. package/lib/types/hooks/useAnswerResults.d.ts +4 -4
  22. package/lib/types/hooks/useCioPia.d.ts +20 -5
  23. package/lib/types/hooks/usePiaClient.d.ts +4 -9
  24. package/lib/types/hooks/useRecsPod.d.ts +2 -2
  25. package/lib/types/hooks/useSuggestedQuestions.d.ts +3 -4
  26. package/lib/types/types.d.ts +43 -6
  27. package/lib/types/version.d.ts +1 -1
  28. package/package.json +2 -2
  29. package/lib/cjs/hooks/mocks/MockConstructorIOClient.js +0 -24
  30. package/lib/cjs/hooks/mocks/agent.js +0 -183
  31. package/lib/cjs/hooks/mocks/types.js +0 -2
  32. package/lib/mjs/hooks/mocks/MockConstructorIOClient.js +0 -38
  33. package/lib/mjs/hooks/mocks/agent.js +0 -173
  34. package/lib/mjs/hooks/mocks/types.js +0 -1
  35. package/lib/types/hooks/mocks/MockConstructorIOClient.d.ts +0 -14
  36. package/lib/types/hooks/mocks/agent.d.ts +0 -23
  37. package/lib/types/hooks/mocks/types.d.ts +0 -77
@@ -1,173 +0,0 @@
1
- import { AgentRequestError } from '../../errors';
2
- // Create URL for PIA API
3
- function createAgentUrl({ itemId, threadId, variationId, question, isStreaming = false, options, parameters = {}, }) {
4
- const { apiKey, agentServiceUrl, clientId, sessionId, userId, segments, version } = options;
5
- if (!agentServiceUrl)
6
- throw new Error('Agent service URL is required');
7
- let baseUrl = `${agentServiceUrl}/v1/item_questions`;
8
- if (question) {
9
- baseUrl += `/${encodeURIComponent(question)}/answer`;
10
- }
11
- if (isStreaming) {
12
- baseUrl += '/streaming';
13
- }
14
- const url = new URL(baseUrl);
15
- url.searchParams.append('item_id', itemId);
16
- url.searchParams.append('key', apiKey);
17
- if (threadId) {
18
- url.searchParams.append('thread_id', threadId);
19
- }
20
- if (variationId) {
21
- url.searchParams.append('variation_id', variationId);
22
- }
23
- if (clientId) {
24
- url.searchParams.append('i', clientId);
25
- }
26
- if (sessionId != null) {
27
- url.searchParams.append('s', String(sessionId));
28
- }
29
- if (userId) {
30
- url.searchParams.append('ui', userId);
31
- }
32
- if (version) {
33
- url.searchParams.append('c', version);
34
- }
35
- if (segments?.length) {
36
- segments.forEach((segment) => url.searchParams.append('us', segment));
37
- }
38
- // Any additional parameters
39
- Object.entries(parameters).forEach(([key, value]) => {
40
- if (value !== undefined) {
41
- url.searchParams.append(key, String(value));
42
- }
43
- });
44
- return url.toString();
45
- }
46
- // Map camelCase SuggestedQuestionsParameters to snake_case query params expected by the API
47
- function mapSuggestedQuestionsParams(params) {
48
- const result = {};
49
- if (params.numResults !== undefined)
50
- result.num_results = params.numResults;
51
- return result;
52
- }
53
- class MockAgent {
54
- options;
55
- constructor(options) {
56
- this.options = options;
57
- }
58
- async getSuggestedQuestions({ itemId, variationId, threadId, parameters = {}, requestParameters = {}, }) {
59
- if (!itemId)
60
- throw new Error('Item ID is required');
61
- if (!this.options.apiKey)
62
- throw new Error('API key is required');
63
- const url = createAgentUrl({
64
- itemId,
65
- variationId,
66
- threadId,
67
- options: this.options,
68
- parameters: { ...requestParameters, ...mapSuggestedQuestionsParams(parameters) },
69
- });
70
- try {
71
- const response = await fetch(url);
72
- if (!response.ok)
73
- throw new AgentRequestError(response.status);
74
- const data = await response.json();
75
- return data;
76
- }
77
- catch (error) {
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));
82
- }
83
- }
84
- async getAnswerResults({ itemId, variationId, threadId, question, parameters = {}, }) {
85
- if (!itemId)
86
- throw new Error('Item ID is required');
87
- if (!question)
88
- throw new Error('Question is required');
89
- if (!this.options.apiKey)
90
- throw new Error('API key is required');
91
- const url = createAgentUrl({
92
- itemId,
93
- threadId,
94
- variationId,
95
- question,
96
- options: this.options,
97
- parameters,
98
- });
99
- try {
100
- const response = await fetch(url);
101
- if (!response.ok)
102
- throw new AgentRequestError(response.status);
103
- const data = await response.json();
104
- return data;
105
- }
106
- catch (error) {
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));
111
- }
112
- }
113
- /**
114
- * Fetches one set of recommendations, in the shape the pod renders.
115
- *
116
- * Recommendations are not available yet: there is no endpoint to ask, so nothing is requested and
117
- * an empty result is returned. The pod renders nothing in that state, which leaves whatever the
118
- * retailer already had in that slot showing through. Supply a `cioClient` with your own
119
- * `agent.getRecs` to drive the pod from your own data in the meantime.
120
- *
121
- * `props` is still declared, because it is the contract a caller's own `getRecs` implements and
122
- * `useRecsPod` passes it - there is simply nothing here to send it to yet.
123
- */
124
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
125
- async getRecs(props) {
126
- return { title: '', items: null, refinement: null, status: 'complete' };
127
- }
128
- async getAnswerResultsStream({ itemId, threadId, variationId, question, parameters, onStart, onMessage, onEnd, }) {
129
- if (!itemId)
130
- throw new Error('Item ID is required');
131
- if (!question)
132
- throw new Error('Question is required');
133
- if (!this.options.apiKey)
134
- throw new Error('API key is required');
135
- const url = createAgentUrl({
136
- itemId,
137
- threadId,
138
- variationId,
139
- question,
140
- isStreaming: true,
141
- options: this.options,
142
- parameters,
143
- });
144
- try {
145
- const eventSource = new EventSource(url);
146
- eventSource.addEventListener('open', (event) => {
147
- const data = JSON.parse(event.data);
148
- if (onStart)
149
- onStart(data);
150
- });
151
- eventSource.addEventListener('message', (event) => {
152
- const data = JSON.parse(event.data);
153
- if (onMessage)
154
- onMessage(data);
155
- });
156
- eventSource.addEventListener('end', (event) => {
157
- const data = JSON.parse(event.data);
158
- if (onEnd)
159
- onEnd(data);
160
- eventSource.close();
161
- });
162
- eventSource.onerror = () => {
163
- eventSource.close();
164
- throw new Error('Unexpected error occurred. Please try again.');
165
- };
166
- }
167
- catch (error) {
168
- const errorMessage = error instanceof Error ? error.message : String(error);
169
- throw new Error(errorMessage);
170
- }
171
- }
172
- }
173
- export default MockAgent;
@@ -1 +0,0 @@
1
- export {};
@@ -1,14 +0,0 @@
1
- import { ConstructorClientOptions } from '@constructor-io/constructorio-client-javascript';
2
- import { Recommendations, Browse, Search, Tracker, Quizzes } from '@constructor-io/constructorio-client-javascript/lib/types/constructorio';
3
- import MockAgent from './agent';
4
- declare class MockConstructorIOClient {
5
- options: ConstructorClientOptions;
6
- search: Search;
7
- browse: Browse;
8
- recommendations: Recommendations;
9
- tracker: Tracker;
10
- quizzes: Quizzes;
11
- agent: MockAgent;
12
- constructor(options: ConstructorClientOptions);
13
- }
14
- export default MockConstructorIOClient;
@@ -1,23 +0,0 @@
1
- import { ConstructorClientOptions } from '@constructor-io/constructorio-client-javascript';
2
- import { QuestionResponse, GetSuggestedQuestionsProps, GetAnswerResultsStreamProps, GetAnswerResultsProps, GetAnswerResultsResponse } from './types';
3
- import { GetRecsProps, RecsResult } from '../../types';
4
- declare class MockAgent {
5
- options: ConstructorClientOptions;
6
- constructor(options: ConstructorClientOptions);
7
- getSuggestedQuestions({ itemId, variationId, threadId, parameters, requestParameters, }: GetSuggestedQuestionsProps): Promise<QuestionResponse>;
8
- getAnswerResults({ itemId, variationId, threadId, question, parameters, }: GetAnswerResultsProps): Promise<GetAnswerResultsResponse>;
9
- /**
10
- * Fetches one set of recommendations, in the shape the pod renders.
11
- *
12
- * Recommendations are not available yet: there is no endpoint to ask, so nothing is requested and
13
- * an empty result is returned. The pod renders nothing in that state, which leaves whatever the
14
- * retailer already had in that slot showing through. Supply a `cioClient` with your own
15
- * `agent.getRecs` to drive the pod from your own data in the meantime.
16
- *
17
- * `props` is still declared, because it is the contract a caller's own `getRecs` implements and
18
- * `useRecsPod` passes it - there is simply nothing here to send it to yet.
19
- */
20
- getRecs(props: GetRecsProps): Promise<RecsResult>;
21
- getAnswerResultsStream({ itemId, threadId, variationId, question, parameters, onStart, onMessage, onEnd, }: GetAnswerResultsStreamProps): Promise<void>;
22
- }
23
- export default MockAgent;
@@ -1,77 +0,0 @@
1
- import { ConstructorClientOptions, ItemData } from '@constructor-io/constructorio-client-javascript';
2
- export type SuggestedQuestionsParameters = {
3
- numResults?: number;
4
- };
5
- export type AgentUrlProps = {
6
- itemId: string;
7
- variationId?: string;
8
- /** Thread ID for conversation context. Must be a valid UUID (e.g., "550e8400-e29b-41d4-a716-446655440000") */
9
- threadId?: string;
10
- question?: string;
11
- isStreaming?: boolean;
12
- options: ConstructorClientOptions;
13
- parameters?: Record<string, string | number | boolean>;
14
- };
15
- export type GetSuggestedQuestionsProps = {
16
- itemId: string;
17
- variationId?: string;
18
- /** Thread ID for conversation context. Must be a valid UUID (e.g., "550e8400-e29b-41d4-a716-446655440000") */
19
- threadId?: string;
20
- parameters?: SuggestedQuestionsParameters;
21
- /** Extra query parameters appended verbatim to the request (e.g. `ef-*` test cell params). */
22
- requestParameters?: Record<string, string | number | boolean>;
23
- };
24
- export interface Question {
25
- value: string;
26
- }
27
- export interface QuestionResponse {
28
- questions: Array<Question>;
29
- }
30
- export type GetAnswerResultsProps = {
31
- itemId: string;
32
- variationId?: string;
33
- /** Thread ID for conversation context. Must be a valid UUID (e.g., "550e8400-e29b-41d4-a716-446655440000") */
34
- threadId?: string;
35
- question: string;
36
- parameters?: Record<string, any>;
37
- };
38
- export type GetAnswerResultsStreamProps = GetAnswerResultsProps & {
39
- onStart?: (event: StreamStartEvent) => void;
40
- onMessage?: (event: StreamMessageEvent) => void;
41
- onEnd?: (event: StreamEndEvent) => void;
42
- };
43
- /** Defines loose types for response-related types to account for future changes to API schema */
44
- export interface ApiItemVariation extends Record<string, any> {
45
- value: string;
46
- data?: ItemData;
47
- }
48
- export interface ApiItem extends Record<string, any> {
49
- value: string;
50
- matched_terms: Array<string>;
51
- data: ItemData;
52
- variations?: Array<ApiItemVariation> | null;
53
- variations_map?: Record<string, any> | Array<Record<string, any>> | null;
54
- }
55
- export interface AnswerItemResults {
56
- request?: Record<string, any>;
57
- response: {
58
- results: Array<ApiItem>;
59
- };
60
- }
61
- export interface GetAnswerResultsResponse {
62
- qna_result_id: string;
63
- value: string;
64
- item_results?: AnswerItemResults;
65
- follow_up_questions?: Array<Question>;
66
- thread_id?: string;
67
- }
68
- export interface StreamStartEvent {
69
- qna_result_id: string;
70
- }
71
- export interface StreamMessageEvent {
72
- qna_result_id: string;
73
- text: string;
74
- }
75
- export interface StreamEndEvent {
76
- qna_result_id: string;
77
- }