From cdf00b023292043d683a43f5bf2cb63388e29e71 Mon Sep 17 00:00:00 2001 From: Evan Yan Date: Tue, 15 Jul 2025 14:17:17 -0700 Subject: [PATCH 01/14] Rename assistant module to agent --- src/constructorio.js | 8 +- src/modules/agent.js | 187 ++++++++++++++++++++++++++++++ src/modules/assistant.js | 175 ++-------------------------- src/modules/tracker.js | 238 +++++++++++++++++++++++++++++++++------ src/types/agent.d.ts | 22 ++++ 5 files changed, 427 insertions(+), 203 deletions(-) create mode 100644 src/modules/agent.js create mode 100644 src/types/agent.d.ts diff --git a/src/constructorio.js b/src/constructorio.js index fbf820a7..f2777857 100644 --- a/src/constructorio.js +++ b/src/constructorio.js @@ -11,6 +11,7 @@ const EventDispatcher = require('./utils/event-dispatcher'); const helpers = require('./utils/helpers'); const { default: packageVersion } = require('./version'); const Quizzes = require('./modules/quizzes'); +const Agent = require('./modules/agent'); const Assistant = require('./modules/assistant'); // Compute package version string @@ -38,7 +39,8 @@ class ConstructorIO { * @param {string} parameters.apiKey - Constructor.io API key * @param {string} [parameters.serviceUrl='https://ac.cnstrc.com'] - API URL endpoint * @param {string} [parameters.quizzesServiceUrl='https://quizzes.cnstrc.com'] - Quizzes API URL endpoint - * @param {string} [parameters.assistantServiceUrl='https://assistant.cnstrc.com'] - AI Assistant API URL endpoint + * @param {string} [parameters.agentServiceUrl='https://agent.cnstrc.com'] - AI Shopping Agent API URL endpoint + * @param {string} [parameters.assistantServiceUrl='https://assistant.cnstrc.com'] - AI Shopping Assistant API URL endpoint * @param {array} [parameters.segments] - User segments * @param {object} [parameters.testCells] - User test cells * @param {string} [parameters.clientId] - Client ID, defaults to value supplied by 'constructorio-id' module @@ -60,6 +62,7 @@ class ConstructorIO { * @property {object} recommendations - Interface to {@link module:recommendations} * @property {object} tracker - Interface to {@link module:tracker} * @property {object} quizzes - Interface to {@link module:quizzes} + * @property {object} agent - Interface to {@link module:agent} * @property {object} assistant - Interface to {@link module:assistant} * @returns {class} */ @@ -69,6 +72,7 @@ class ConstructorIO { version: versionFromOptions, serviceUrl, quizzesServiceUrl, + agentServiceUrl, assistantServiceUrl, segments, testCells, @@ -115,6 +119,7 @@ class ConstructorIO { version: versionFromOptions || versionFromGlobal || computePackageVersion(), serviceUrl: helpers.addHTTPSToString(normalizedServiceUrl) || 'https://ac.cnstrc.com', quizzesServiceUrl: (quizzesServiceUrl && quizzesServiceUrl.replace(/\/$/, '')) || 'https://quizzes.cnstrc.com', + agentServiceUrl: (agentServiceUrl && agentServiceUrl.replace(/\/$/, '')) || 'https://agent.cnstrc.com', assistantServiceUrl: (assistantServiceUrl && assistantServiceUrl.replace(/\/$/, '')) || 'https://assistant.cnstrc.com', sessionId: sessionId || session_id, clientId: clientId || client_id, @@ -137,6 +142,7 @@ class ConstructorIO { this.recommendations = new Recommendations(this.options); this.tracker = new Tracker(this.options); this.quizzes = new Quizzes(this.options); + this.agent = new Agent(this.options); this.assistant = new Assistant(this.options); // Dispatch initialization event diff --git a/src/modules/agent.js b/src/modules/agent.js new file mode 100644 index 00000000..3358938c --- /dev/null +++ b/src/modules/agent.js @@ -0,0 +1,187 @@ +const { cleanParams, trimNonBreakingSpaces, encodeURIComponentRFC3986, stringify } = require('../utils/helpers'); + +// Create URL from supplied intent (term) and parameters +function createAgentUrl(intent, parameters, options) { + const { + apiKey, + version, + sessionId, + clientId, + userId, + segments, + testCells, + agentServiceUrl, + assistantServiceUrl, + } = options; + let queryParams = { c: version }; + queryParams.key = apiKey; + queryParams.i = clientId; + queryParams.s = sessionId; + + const serviceUrl = agentServiceUrl || assistantServiceUrl; + + // Validate intent is provided + if (!intent || typeof intent !== 'string') { + throw new Error('intent is a required parameter of type string'); + } + + // Validate domain is provided + if (!parameters.domain || typeof parameters.domain !== 'string') { + throw new Error('parameters.domain is a required parameter of type string'); + } + + // Pull test cells from options + if (testCells) { + Object.keys(testCells).forEach((testCellKey) => { + queryParams[`ef-${testCellKey}`] = testCells[testCellKey]; + }); + } + + // Pull user segments from options + if (segments && segments.length) { + queryParams.us = segments; + } + + // Pull user id from options and ensure string + if (userId) { + queryParams.ui = String(userId); + } + + if (parameters) { + const { domain, numResultsPerPage } = parameters; + + // Pull domain from parameters + if (domain) { + queryParams.domain = domain; + } + + // Pull results number from parameters + if (numResultsPerPage) { + queryParams.num_results_per_page = numResultsPerPage; + } + } + + // eslint-disable-next-line no-underscore-dangle + queryParams._dt = Date.now(); + queryParams = cleanParams(queryParams); + + const queryString = stringify(queryParams); + const cleanedQuery = intent.replace(/^\//, '|'); // For compatibility with backend API + + return `${serviceUrl}/v1/intent/${encodeURIComponentRFC3986(trimNonBreakingSpaces(cleanedQuery))}?${queryString}`; +} + +// Add event listeners to custom SSE that pushes data to the stream +function setupEventListeners(eventSource, controller, eventTypes) { + const addListener = (type) => { + eventSource.addEventListener(type, (event) => { + const data = JSON.parse(event.data); + + controller.enqueue({ type, data }); // Enqueue data into the stream + }); + }; + + // Set up listeners for all event types except END + Object.values(eventTypes).forEach((type) => { + if (type !== eventTypes.END) { + addListener(type); + } + }); + + // Handle the END event separately to close the stream + eventSource.addEventListener(eventTypes.END, () => { + controller.close(); // Close the stream + eventSource.close(); // Close the EventSource connection + }); + + // Handle errors from the EventSource + // eslint-disable-next-line no-param-reassign + eventSource.onerror = (error) => { + controller.error(error); // Pass the error to the stream + eventSource.close(); // Close the EventSource connection + }; +} + +/** + * Interface to agent SSE. + * Replaces the previous Assistant module. + * + * @module agent + * @inner + * @returns {object} + */ +class Agent { + constructor(options) { + this.options = options || {}; + } + + static EventTypes = { + START: 'start', // Denotes the start of the stream + GROUP: 'group', // Represents a semantic grouping of search results, optionally having textual explanation + SEARCH_RESULT: 'search_result', // Represents a set of results with metadata (used to show results with search refinements) + ARTICLE_REFERENCE: 'article_reference', // Represents a set of content with metadata + RECIPE_INFO: 'recipe_info', // Represents recipes' auxiliary information like cooking times & serving sizes + RECIPE_INSTRUCTIONS: 'recipe_instructions', // Represents recipe instructions + SERVER_ERROR: 'server_error', // Server Error event + IMAGE_META: 'image_meta', // This event type is used for enhancing recommendations with media content such as images + END: 'end', // Represents the end of data stream + }; + + /** + * Retrieve agent results from EventStream + * + * @function getAgentResultsStream + * @description Retrieve a stream of agent results from Constructor.io API + * @param {string} intent - Intent to use to perform an intent based recommendations + * @param {object} [parameters] - Additional parameters to refine result set + * @param {string} [parameters.domain] - domain name e.g. swimming sports gear, groceries + * @param {number} [parameters.numResultsPerPage] - The total number of results to return + * @returns {ReadableStream} Returns a ReadableStream. + * @example + * const readableStream = constructorio.agent.getAgentResultsStream('I want to get shoes', { + * domain: "nike_sportswear", + * }); + * const reader = readableStream.getReader(); + * const { value, done } = await reader.read(); + */ + getAgentResultsStream(query, parameters) { + let eventSource; + let readableStream; + + try { + const requestUrl = createAgentUrl(query, parameters, this.options); + + // Create an EventSource that connects to the Server Sent Events API + eventSource = new EventSource(requestUrl); + + // Create a readable stream that data will be pushed into + readableStream = new ReadableStream({ + // To be called on stream start + start(controller) { + // Listen to events emitted from ASA Server Sent Events and push data to the ReadableStream + setupEventListeners(eventSource, controller, Agent.EventTypes); + }, + // To be called on stream cancelling + cancel() { + // Close the EventSource connection when the stream is prematurely canceled + eventSource.close(); + }, + }); + } catch (e) { + if (readableStream) { + readableStream?.cancel(); + } else { + // If the stream was not successfully created, close the EventSource directly + eventSource?.close(); + } + + throw new Error(e.message); + } + + return readableStream; + } +} + +module.exports = Agent; +module.exports.createAgentUrl = createAgentUrl; +module.exports.setupEventListeners = setupEventListeners; diff --git a/src/modules/assistant.js b/src/modules/assistant.js index f67e05ee..3661e62b 100644 --- a/src/modules/assistant.js +++ b/src/modules/assistant.js @@ -1,184 +1,25 @@ -const { cleanParams, trimNonBreakingSpaces, encodeURIComponentRFC3986, stringify } = require('../utils/helpers'); - -// Create URL from supplied intent (term) and parameters -function createAssistantUrl(intent, parameters, options) { - const { - apiKey, - version, - sessionId, - clientId, - userId, - segments, - testCells, - assistantServiceUrl, - } = options; - let queryParams = { c: version }; - - queryParams.key = apiKey; - queryParams.i = clientId; - queryParams.s = sessionId; - - // Validate intent is provided - if (!intent || typeof intent !== 'string') { - throw new Error('intent is a required parameter of type string'); - } - - // Validate domain is provided - if (!parameters.domain || typeof parameters.domain !== 'string') { - throw new Error('parameters.domain is a required parameter of type string'); - } - - // Pull test cells from options - if (testCells) { - Object.keys(testCells).forEach((testCellKey) => { - queryParams[`ef-${testCellKey}`] = testCells[testCellKey]; - }); - } - - // Pull user segments from options - if (segments && segments.length) { - queryParams.us = segments; - } - - // Pull user id from options and ensure string - if (userId) { - queryParams.ui = String(userId); - } - - if (parameters) { - const { domain, numResultsPerPage } = parameters; - - // Pull domain from parameters - if (domain) { - queryParams.domain = domain; - } - - // Pull results number from parameters - if (numResultsPerPage) { - queryParams.num_results_per_page = numResultsPerPage; - } - } - - // eslint-disable-next-line no-underscore-dangle - queryParams._dt = Date.now(); - queryParams = cleanParams(queryParams); - - const queryString = stringify(queryParams); - const cleanedQuery = intent.replace(/^\//, '|'); // For compatibility with backend API - - return `${assistantServiceUrl}/v1/intent/${encodeURIComponentRFC3986(trimNonBreakingSpaces(cleanedQuery))}?${queryString}`; -} - -// Add event listeners to custom SSE that pushes data to the stream -function setupEventListeners(eventSource, controller, eventTypes) { - const addListener = (type) => { - eventSource.addEventListener(type, (event) => { - const data = JSON.parse(event.data); - - controller.enqueue({ type, data }); // Enqueue data into the stream - }); - }; - - // Set up listeners for all event types except END - Object.values(eventTypes).forEach((type) => { - if (type !== eventTypes.END) { - addListener(type); - } - }); - - // Handle the END event separately to close the stream - eventSource.addEventListener(eventTypes.END, () => { - controller.close(); // Close the stream - eventSource.close(); // Close the EventSource connection - }); - - // Handle errors from the EventSource - // eslint-disable-next-line no-param-reassign - eventSource.onerror = (error) => { - controller.error(error); // Pass the error to the stream - eventSource.close(); // Close the EventSource connection - }; -} +const Agent = require('./agent'); +const { createAgentUrl, setupEventListeners } = require('./agent'); /** + * @deprecated This module is deprecated and will be removed in a future version. Use the Agent module instead. * Interface to assistant SSE. * * @module assistant * @inner * @returns {object} */ -class Assistant { - constructor(options) { - this.options = options || {}; - } - - static EventTypes = { - START: 'start', // Denotes the start of the stream - GROUP: 'group', // Represents a semantic grouping of search results, optionally having textual explanation - SEARCH_RESULT: 'search_result', // Represents a set of results with metadata (used to show results with search refinements) - ARTICLE_REFERENCE: 'article_reference', // Represents a set of content with metadata - RECIPE_INFO: 'recipe_info', // Represents recipes' auxiliary information like cooking times & serving sizes - RECIPE_INSTRUCTIONS: 'recipe_instructions', // Represents recipe instructions - SERVER_ERROR: 'server_error', // Server Error event - IMAGE_META: 'image_meta', // This event type is used for enhancing recommendations with media content such as images - END: 'end', // Represents the end of data stream - }; +class Assistant extends Agent { + EventTypes = Agent.EventTypes; /** - * Retrieve assistant results from EventStream - * - * @function getAssistantResultsStream - * @description Retrieve a stream of assistant results from Constructor.io API - * @param {string} intent - Intent to use to perform an intent based recommendations - * @param {object} [parameters] - Additional parameters to refine result set - * @param {string} [parameters.domain] - domain name e.g. swimming sports gear, groceries - * @param {number} [parameters.numResultsPerPage] - The total number of results to return - * @returns {ReadableStream} Returns a ReadableStream. - * @example - * const readableStream = constructorio.assistant.getAssistantResultsStream('I want to get shoes', { - * domain: "nike_sportswear", - * }); - * const reader = readableStream.getReader(); - * const { value, done } = await reader.read(); + * @deprecated Use getAgentResultsStream from the Agent module instead. */ getAssistantResultsStream(query, parameters) { - let eventSource; - let readableStream; - - try { - const requestUrl = createAssistantUrl(query, parameters, this.options); - - // Create an EventSource that connects to the Server Sent Events API - eventSource = new EventSource(requestUrl); - - // Create a readable stream that data will be pushed into - readableStream = new ReadableStream({ - // To be called on stream start - start(controller) { - // Listen to events emitted from ASA Server Sent Events and push data to the ReadableStream - setupEventListeners(eventSource, controller, Assistant.EventTypes); - }, - // To be called on stream cancelling - cancel() { - // Close the EventSource connection when the stream is prematurely canceled - eventSource.close(); - }, - }); - } catch (e) { - if (readableStream) { - readableStream?.cancel(); - } else { - // If the stream was not successfully created, close the EventSource directly - eventSource?.close(); - } - - throw new Error(e.message); - } - - return readableStream; + return this.getAgentResultsStream(query, parameters); } } module.exports = Assistant; -module.exports.createAssistantUrl = createAssistantUrl; +module.exports.createAssistantUrl = createAgentUrl; module.exports.setupEventListeners = setupEventListeners; diff --git a/src/modules/tracker.js b/src/modules/tracker.js index 2a2b9922..fb82d362 100644 --- a/src/modules/tracker.js +++ b/src/modules/tracker.js @@ -2403,26 +2403,26 @@ class Tracker { /** * Send ASA request submitted event * - * @function trackAssistantSubmit + * @function trackAgentSubmit * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - Intent of user request * @param {string} [parameters.section] - The section name for the item Ex. "Products" * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} - * @description User submitted an assistant search - * (pressing enter within assistant input element, or clicking assistant submit element) + * @description User submitted an agent search + * (pressing enter within agent input element, or clicking agent submit element) * @example - * constructorio.tracker.trackAssistantSubmit( + * constructorio.tracker.trackAgentSubmit( * { * intent: 'show me a recipe for a cookie', * }, * ); */ - trackAssistantSubmit(parameters, networkParameters = {}) { + trackAgentSubmit(parameters, networkParameters = {}) { if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { // Ensure parameters are provided (required) - const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/assistant_submit?`; + const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/agent_submit?`; const { section, intent, @@ -2454,9 +2454,9 @@ class Tracker { } /** - * Send assistant results page load started + * Send agent results page load started * - * @function trackAssistantResultLoadStarted + * @function trackAgentResultLoadStarted * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - Intent of user request * @param {string} [parameters.section] - The section name for the item Ex. "Products" @@ -2464,19 +2464,19 @@ class Tracker { * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} - * @description Assistant results page load begun (but has not necessarily loaded completely) + * @description Agent results page load begun (but has not necessarily loaded completely) * @example - * constructorio.tracker.trackAssistantResultLoadStarted( + * constructorio.tracker.trackAgentResultLoadStarted( * { * intent: 'show me a recipe for a cookie', * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', * }, * ); */ - trackAssistantResultLoadStarted(parameters, networkParameters = {}) { + trackAgentResultLoadStarted(parameters, networkParameters = {}) { if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { // Ensure parameters are provided (required) - const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/assistant_result_load_start?`; + const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/agent_result_load_start?`; const { section, intentResultId, @@ -2510,9 +2510,9 @@ class Tracker { } /** - * Send assistant results page load finished + * Send agent results page load finished * - * @function trackAssistantResultLoadFinished + * @function trackAgentResultLoadFinished * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - Intent of user request * @param {number} parameters.searchResultCount - Number of search results loaded @@ -2521,9 +2521,9 @@ class Tracker { * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} - * @description Assistant results page load finished + * @description Agent results page load finished * @example - * constructorio.tracker.trackAssistantResultLoadFinished( + * constructorio.tracker.trackAgentResultLoadFinished( * { * intent: 'show me a recipe for a cookie', * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', @@ -2531,10 +2531,10 @@ class Tracker { * }, * ); */ - trackAssistantResultLoadFinished(parameters, networkParameters = {}) { + trackAgentResultLoadFinished(parameters, networkParameters = {}) { // Ensure parameters are provided (required) if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { - const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/assistant_result_load_finish?`; + const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/agent_result_load_finish?`; const { section, searchResultCount, @@ -2570,9 +2570,9 @@ class Tracker { } /** - * Send assistant result click event to API + * Send agent result click event to API * - * @function trackAssistantResultClick + * @function trackAgentResultClick * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - intent of the user * @param {string} parameters.searchResultId - result_id of the specific search result the clicked item belongs to @@ -2584,9 +2584,9 @@ class Tracker { * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} - * @description User clicked a result that appeared within an assistant search result + * @description User clicked a result that appeared within an agent search result * @example - * constructorio.tracker.trackAssistantResultClick( + * constructorio.tracker.trackAgentResultClick( * { * variationId: 'KMH879-7632', * searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2', @@ -2596,10 +2596,10 @@ class Tracker { * }, * ); */ - trackAssistantResultClick(parameters, networkParameters = {}) { + trackAgentResultClick(parameters, networkParameters = {}) { // Ensure parameters are provided (required) if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { - const requestPath = `${this.options.serviceUrl}/v2/behavioral_action/assistant_search_result_click?`; + const requestPath = `${this.options.serviceUrl}/v2/behavioral_action/agent_search_result_click?`; const { section = 'Products', variationId, @@ -2641,9 +2641,9 @@ class Tracker { } /** - * Send assistant search result view event to API + * Send agent search result view event to API * - * @function trackAssistantResultView + * @function trackAgentResultView * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - intent of the user * @param {string} parameters.searchResultId - result_id of the specific search result the clicked item belongs to @@ -2654,9 +2654,9 @@ class Tracker { * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} - * @description User viewed a search result within an assistant result + * @description User viewed a search result within an agent result * @example - * constructorio.tracker.trackAssistantResultView( + * constructorio.tracker.trackAgentResultView( * { * searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2', * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', @@ -2666,10 +2666,10 @@ class Tracker { * }, * ); */ - trackAssistantResultView(parameters, networkParameters = {}) { + trackAgentResultView(parameters, networkParameters = {}) { // Ensure parameters are provided (required) if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { - const requestPath = `${this.options.serviceUrl}/v2/behavioral_action/assistant_search_result_view?`; + const requestPath = `${this.options.serviceUrl}/v2/behavioral_action/agent_search_result_view?`; const { section = 'Products', items, @@ -2711,7 +2711,7 @@ class Tracker { /** * Send ASA search submitted event * - * @function trackAssistantSearchSubmit + * @function trackAgentSearchSubmit * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - Intent of user request * @param {string} parameters.searchTerm - Term of submitted assistant search event @@ -2721,9 +2721,9 @@ class Tracker { * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} - * @description User submitted an alternative assistant search result search term + * @description User submitted an alternative agent search result search term * @example - * constructorio.tracker.trackAssistantSearchSubmit({ + * constructorio.tracker.trackAgentSearchSubmit({ * { * searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2', * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', @@ -2732,11 +2732,11 @@ class Tracker { * }, * ); */ - trackAssistantSearchSubmit(parameters, networkParameters = {}) { + trackAgentSearchSubmit(parameters, networkParameters = {}) { // Ensure parameters are provided (required) if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { // Ensure parameters are provided (required) - const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/assistant_search_submit?`; + const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/agent_search_submit?`; const { section, intent, @@ -2774,6 +2774,174 @@ class Tracker { return new Error('parameters is a required parameter of type object'); } + /** + * Send ASA request submitted event + * + * @deprecated This method will be removed in a future version. Use trackAgentSubmit instead. + * @function trackAssistantSubmit + * @param {object} parameters - Additional parameters to be sent with request + * @param {string} parameters.intent - Intent of user request + * @param {string} [parameters.section] - The section name for the item Ex. "Products" + * @param {object} [networkParameters] - Parameters relevant to the network request + * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) + * @returns {(true|Error)} + * @description User submitted an assistant search + * (pressing enter within assistant input element, or clicking assistant submit element) + * @example + * constructorio.tracker.trackAssistantSubmit( + * { + * intent: 'show me a recipe for a cookie', + * }, + * ); + */ + trackAssistantSubmit(parameters, networkParameters = {}) { + return this.trackAgentSubmit(parameters, networkParameters); + } + + /** + * Send assistant results page load started + * + * @deprecated This method will be removed in a future version. Use trackAgentResultLoadStarted instead. + * @function trackAssistantResultLoadStarted + * @param {object} parameters - Additional parameters to be sent with request + * @param {string} parameters.intent - Intent of user request + * @param {string} [parameters.section] - The section name for the item Ex. "Products" + * @param {string} [parameters.intentResultId] - The intent result id from the ASA response + * @param {object} [networkParameters] - Parameters relevant to the network request + * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) + * @returns {(true|Error)} + * @description Assistant results page load begun (but has not necessarily loaded completely) + * @example + * constructorio.tracker.trackAssistantResultLoadStarted( + * { + * intent: 'show me a recipe for a cookie', + * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', + * }, + * ); + */ + trackAssistantResultLoadStarted(parameters, networkParameters = {}) { + return this.trackAgentResultLoadStarted(parameters, networkParameters); + } + + /** + * Send assistant results page load finished + * + * @deprecated This method will be removed in a future version. Use trackAgentResultLoadFinished instead. + * @function trackAssistantResultLoadFinished + * @param {object} parameters - Additional parameters to be sent with request + * @param {string} parameters.intent - Intent of user request + * @param {number} parameters.searchResultCount - Number of search results loaded + * @param {string} [parameters.section] - The section name for the item Ex. "Products" + * @param {string} [parameters.intentResultId] - The intent result id from the ASA response + * @param {object} [networkParameters] - Parameters relevant to the network request + * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) + * @returns {(true|Error)} + * @description Assistant results page load finished + * @example + * constructorio.tracker.trackAssistantResultLoadFinished( + * { + * intent: 'show me a recipe for a cookie', + * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', + * searchResultCount: 5, + * }, + * ); + */ + trackAssistantResultLoadFinished(parameters, networkParameters = {}) { + return this.trackAgentResultLoadFinished(parameters, networkParameters); + } + + /** + * Send assistant result click event to API + * + * @deprecated This method will be removed in a future version. Use trackAgentResultClick instead. + * @function trackAssistantResultClick + * @param {object} parameters - Additional parameters to be sent with request + * @param {string} parameters.intent - intent of the user + * @param {string} parameters.searchResultId - result_id of the specific search result the clicked item belongs to + * @param {string} parameters.itemId - Product item unique identifier + * @param {string} parameters.itemName - Product item name + * @param {string} [parameters.section] - The section name for the item Ex. "Products" + * @param {string} [parameters.variationId] - Product item variation unique identifier + * @param {string} [parameters.intentResultId] - Browse result identifier (returned in response from Constructor) + * @param {object} [networkParameters] - Parameters relevant to the network request + * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) + * @returns {(true|Error)} + * @description User clicked a result that appeared within an assistant search result + * @example + * constructorio.tracker.trackAssistantResultClick( + * { + * variationId: 'KMH879-7632', + * searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2', + * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', + * intent: 'show me a recipe for a cookie', + * itemId: 'KMH876', + * }, + * ); + */ + trackAssistantResultClick(parameters, networkParameters = {}) { + return this.trackAgentResultClick(parameters, networkParameters); + } + + /** + * Send assistant search result view event to API + * + * @deprecated This method will be removed in a future version. Use trackAgentResultView instead. + * @function trackAssistantResultView + * @param {object} parameters - Additional parameters to be sent with request + * @param {string} parameters.intent - intent of the user + * @param {string} parameters.searchResultId - result_id of the specific search result the clicked item belongs to + * @param {number} parameters.numResultsViewed - Number of items viewed in this search result + * @param {object[]} [parameters.items] - List of product item objects viewed + * @param {string} [parameters.section] - The section name for the item Ex. "Products" + * @param {string} [parameters.intentResultId] - Browse result identifier (returned in response from Constructor) + * @param {object} [networkParameters] - Parameters relevant to the network request + * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) + * @returns {(true|Error)} + * @description User viewed a search result within an assistant result + * @example + * constructorio.tracker.trackAssistantResultView( + * { + * searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2', + * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', + * intent: 'show me a recipe for a cookie', + * numResultsViewed: 5, + * items: [{itemId: 'KMH876'}, {itemId: 'KMH140'}, {itemId: 'KMH437'}], + * }, + * ); + */ + trackAssistantResultView(parameters, networkParameters = {}) { + this.trackAgentResultView(parameters, networkParameters); + } + + /** + * Send ASA search submitted event + * + * @deprecated This method will be removed in a future version. Use trackAgentSearchSubmit instead. + * @function trackAssistantSearchSubmit + * @param {object} parameters - Additional parameters to be sent with request + * @param {string} parameters.intent - Intent of user request + * @param {string} parameters.searchTerm - Term of submitted assistant search event + * @param {string} parameters.searchResultId - resultId of search result the clicked item belongs to + * @param {string} [parameters.section] - The section name for the item Ex. "Products" + * @param {string} [parameters.intentResultId] - intentResultId from the ASA response + * @param {object} [networkParameters] - Parameters relevant to the network request + * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) + * @returns {(true|Error)} + * @description User submitted an alternative assistant search result search term + * @example + * constructorio.tracker.trackAssistantSearchSubmit({ + * { + * searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2', + * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', + * intent: 'show me a recipe for a cookie', + * searchTerm: 'flour', + * }, + * ); + */ + trackAssistantSearchSubmit(parameters, networkParameters = {}) { + return this.trackAgentSearchSubmit(parameters, networkParameters); + } + /** * Subscribe to success or error messages emitted by tracking requests * diff --git a/src/types/agent.d.ts b/src/types/agent.d.ts new file mode 100644 index 00000000..eeb63f25 --- /dev/null +++ b/src/types/agent.d.ts @@ -0,0 +1,22 @@ +import { + ConstructorClientOptions, +} from '.'; + +export default Agent; + +export interface IAgentParameters { + domain: string; + numResultsPerPage?: number; + filters?: Record; +} + +declare class Agent { + constructor(options: ConstructorClientOptions); + + options: ConstructorClientOptions; + + getAgentResultsStream( + intent: string, + parameters?: IAgentParameters, + ): ReadableStream; +} From 7da21fb62b5f4e6958e2155be34935c3dbce681d Mon Sep 17 00:00:00 2001 From: Evan Yan Date: Tue, 15 Jul 2025 15:00:16 -0700 Subject: [PATCH 02/14] Add test cases for agent module --- spec/src/modules/agent.js | 268 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 268 insertions(+) create mode 100644 spec/src/modules/agent.js diff --git a/spec/src/modules/agent.js b/spec/src/modules/agent.js new file mode 100644 index 00000000..7810d0a3 --- /dev/null +++ b/spec/src/modules/agent.js @@ -0,0 +1,268 @@ +/* eslint-disable no-unused-expressions, import/no-unresolved */ +const dotenv = require('dotenv'); +const chai = require('chai'); +const sinon = require('sinon'); +const sinonChai = require('sinon-chai'); +const EventSource = require('eventsource'); +const { ReadableStream } = require('web-streams-polyfill'); +const qs = require('qs'); +const { createAgentUrl, setupEventListeners } = require('../../../src/modules/agent'); +const Agent = require('../../../src/modules/agent'); +let ConstructorIO = require('../../../test/constructorio'); // eslint-disable-line import/extensions +const { encodeURIComponentRFC3986 } = require('../../../src/utils/helpers'); +const jsdom = require('../utils/jsdom-global'); + +const bundled = process.env.BUNDLED === 'true'; +const bundledDescriptionSuffix = bundled ? ' - Bundled' : ''; + +chai.use(sinonChai); +dotenv.config(); + +const testApiKey = process.env.TEST_REQUEST_API_KEY; +const clientVersion = 'cio-mocha'; + +const defaultOptions = { + apiKey: testApiKey, + version: clientVersion, + agentServiceUrl: 'https://agent.cnstrc.com', + clientId: '123', + sessionId: 123, +}; + +const defaultParameters = { + domain: 'agent', +}; + +describe(`ConstructorIO - Agent${bundledDescriptionSuffix}`, () => { + const jsdomOptions = { url: 'http://localhost' }; + let cleanup; + + beforeEach(() => { + cleanup = jsdom(jsdomOptions); + global.CLIENT_VERSION = clientVersion; + window.CLIENT_VERSION = clientVersion; + + if (bundled) { + ConstructorIO = window.ConstructorioClient; + } + }); + + afterEach(() => { + delete global.CLIENT_VERSION; + delete window.CLIENT_VERSION; + cleanup(); + }); + + // createAgentUrl util Tests + describe('Test createAgentUrl', () => { + + it('should throw an error if intent is not provided', () => { + expect(() => createAgentUrl('', defaultParameters, defaultOptions)).throw('intent is a required parameter of type string'); + }); + + it('should throw an error if domain is not provided in parameters', () => { + expect(() => createAgentUrl('testIntent', {}, defaultOptions)).throw('parameters.domain is a required parameter of type string'); + }); + + it('should correctly construct a URL with minimal valid inputs', () => { + const intent = 'testIntent'; + const url = createAgentUrl(intent, defaultParameters, defaultOptions); + + expect(url).contain('https://agent.cnstrc.com/v1/intent/'); + expect(url).contain(`intent/${encodeURIComponentRFC3986(intent)}`); + + const requestedUrlParams = qs.parse(url.split('?')?.[1]); + + expect(requestedUrlParams).to.have.property('key'); + expect(requestedUrlParams).to.have.property('i'); + expect(requestedUrlParams).to.have.property('s'); + expect(requestedUrlParams).to.have.property('c').to.equal(clientVersion); + expect(requestedUrlParams).to.have.property('_dt'); + + }); + + it('should clean and encode parameters correctly', () => { + const intentWithSpaces = 'test/ [Intent)'; + const url = createAgentUrl( + intentWithSpaces, + { ...defaultParameters }, + defaultOptions, + ); + + // Ensure spaces are encoded and parameters are cleaned + expect(url).not.contain(' '); + expect(url).contain(encodeURIComponentRFC3986(intentWithSpaces)); + }); + }); + + // setupEventListeners util Tests + describe('Test setupEventListeners', () => { + let mockEventSource; + let mockStreamController; + + beforeEach(() => { + mockEventSource = { + addEventListener: sinon.stub(), + close: sinon.stub(), + }; + + mockStreamController = { + enqueue: sinon.stub(), + close: sinon.stub(), + error: sinon.stub(), + }; + }); + + afterEach(() => { + sinon.restore(); // Restore all mocks + }); + + it('should set up event listeners for all event types', () => { + const eventTypes = Agent.EventTypes; + + setupEventListeners(mockEventSource, mockStreamController, eventTypes); + Object.values(Agent.EventTypes).forEach((event) => { + expect(mockEventSource.addEventListener.calledWith(event)).to.be.true; + }); + }); + + it('should enqueue event data into the stream', (done) => { + const eventTypes = Agent.EventTypes; + const eventType = Agent.EventTypes.SEARCH_RESULT; + const eventData = { data: 'Hello, world!' }; + + setupEventListeners(mockEventSource, mockStreamController, eventTypes); + + // Simulate an event being emitted + const allEventsCallbacks = mockEventSource.addEventListener.getCalls(); + const searchResultsCallback = allEventsCallbacks.find((call) => call.args[0] === eventType).args[1]; + + searchResultsCallback({ data: JSON.stringify(eventData) }); + + setImmediate(() => { // Ensure stream processing completes + expect(mockStreamController.enqueue.calledWith({ type: eventType, data: eventData })).to.be.true; + done(); + }); + }); + + it('should close the EventSource and the stream when END event is received', () => { + const eventTypes = Agent.EventTypes; + const eventType = Agent.EventTypes.END; + + setupEventListeners(mockEventSource, mockStreamController, eventTypes); + + // Simulate the END event being emitted + const endEventCallback = mockEventSource.addEventListener.getCalls() + .find((call) => call.args[0] === eventType).args[1]; + + endEventCallback(); + + expect(mockEventSource.close.called).to.be.true; + expect(mockStreamController.close.called).to.be.true; + }); + + it('should handle errors from the EventSource', () => { + const eventTypes = { START: 'start', END: 'end' }; + const mockError = new Error('Test Error'); + + setupEventListeners(mockEventSource, mockStreamController, eventTypes); + + // Directly trigger the onerror handler + mockEventSource.onerror(mockError); + + // Assert that controller.error was called with the mock error + sinon.assert.calledWith(mockStreamController.error, mockError); + + // Assert that the event source was closed + sinon.assert.calledOnce(mockEventSource.close); + }); + + it('should correctly handle and enqueue events with specific data structures', (done) => { + const eventType = Agent.EventTypes.SEARCH_RESULT; + const complexData = { intent_result_id: 123, response: { results: [{ name: 'Item 1' }, { name: 'Item 2' }] } }; + const eventTypes = Agent.EventTypes; + + setupEventListeners(mockEventSource, mockStreamController, eventTypes); + + // Simulate an event with a complex data structure being emitted + const dataStructureEventCallback = mockEventSource.addEventListener.getCalls() + .find((call) => call.args[0] === eventType).args[1]; + + dataStructureEventCallback({ data: JSON.stringify(complexData) }); + + // Verify that the complex data structure was correctly enqueued + setImmediate(() => { + expect(mockStreamController.enqueue.calledWith({ type: eventType, data: complexData })).to.be.true; + done(); + }); + }); + }); + + describe('getAgentResultsStream', () => { + beforeEach(() => { + global.EventSource = EventSource; + global.ReadableStream = ReadableStream; + window.EventSource = EventSource; + window.ReadableStream = ReadableStream; + }); + + afterEach(() => { + delete global.EventSource; + delete global.ReadableStream; + delete window.EventSource; + delete window.ReadableStream; + }); + + it('should create a readable stream', () => { + const { agent } = new ConstructorIO(defaultOptions); + const stream = agent.getAgentResultsStream('I want shoes', { domain: 'assistant' }); + + // Assert it return a stream object + expect(stream).to.have.property('getReader'); + }); + + it('should throw an error if missing domain parameter', () => { + const { agent } = new ConstructorIO(defaultOptions); + + expect(() => agent.getAgentResultsStream('I want shoes', {})).throw('parameters.domain is a required parameter of type string'); + }); + + it('should throw an error if missing intent', () => { + const { agent } = new ConstructorIO(defaultOptions); + + expect(() => agent.getAgentResultsStream('', {})).throw('intent is a required parameter of type string'); + }); + + it('should push expected data to the stream', async () => { + const { agent } = new ConstructorIO(defaultOptions); + const stream = await agent.getAgentResultsStream('query', { domain: 'assistant' }); + const reader = stream.getReader(); + const { value, done } = await reader.read(); + + // Assert that the stream is not empty and the first chunk contains expected data + expect(done).to.be.false; + expect(value.type).to.equal('start'); + reader.releaseLock(); + }); + + it('should handle cancel to the stream gracefully', async () => { + const { agent } = new ConstructorIO(defaultOptions); + const stream = await agent.getAgentResultsStream('query', { domain: 'assistant' }); + const reader = stream.getReader(); + const { value, done } = await reader.read(); + + // Assert that the stream is not empty and the first chunk contains expected data + expect(done).to.be.false; + expect(value.type).to.equal('start'); + reader.cancel(); + }); + + it('should handle pre maturely cancel before reading any data', async () => { + const { agent } = new ConstructorIO(defaultOptions); + const stream = await agent.getAgentResultsStream('query', { domain: 'assistant' }); + const reader = stream.getReader(); + + reader.cancel(); + }); + }); +}); From a21ea2fb9d0cc4a5301302c9e369c4a334ece64e Mon Sep 17 00:00:00 2001 From: Evan Yan Date: Tue, 15 Jul 2025 15:46:17 -0700 Subject: [PATCH 03/14] Add types for agent module --- src/modules/tracker.js | 82 +++++++++++++++++++++--------------------- src/types/index.d.ts | 3 +- src/types/tracker.d.ts | 65 +++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 42 deletions(-) diff --git a/src/modules/tracker.js b/src/modules/tracker.js index e01e083b..11d8e0da 100644 --- a/src/modules/tracker.js +++ b/src/modules/tracker.js @@ -2941,47 +2941,47 @@ class Tracker { trackAssistantSearchSubmit(parameters, networkParameters = {}) { return this.trackAgentSearchSubmit(parameters, networkParameters); } - - /** - * Send product insights agent view events - * - * @function trackProductInsightsAgentViews - * @param {object} parameters - Additional parameters to be sent with request - * @param {array.<{question: string}>} parameters.questions - List of pre-defined questions shown to the user - * @param {string} parameters.itemId - Product id whose page we are on - * @param {string} parameters.itemName - Product name whose page we are on - * @param {array.<{start: string | undefined, - * end: string | undefined}>} parameters.viewTimespans - List of timestamp pairs in ISO_8601 format - * @param {string} [parameters.variationId] - Variation id whose page we are on - * @param {string} [parameters.section] - The section name for the item Ex. "Products" - * @param {object} [networkParameters] - Parameters relevant to the network request - * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) - * @returns {(true|Error)} - * @description The product insights agent element appeared in the visible part of the page - * @example - * constructorio.tracker.trackProductInsightsAgentViews({ - * { - * 'itemId': '1', - * 'itemName': 'item1', - * 'variationId': '2', - * 'questions': [ - * { question: 'Why choose this?' }, - * { question: 'How is this product made?' }, - * { question: 'What are the dimensions of this product?' } - * ], - * 'viewTimespans': [ - * { - * 'start': '2025-05-19T14:30:00+02:00', - * 'end': '2025-05-19T14:30:05+02:00' - * }, - * { - * 'start': '2025-05-19T14:30:10+02:00', - * 'end': '2025-05-19T14:30:15+02:00' - * } - * ] - * }, - * ); - */ + + /** + * Send product insights agent view events + * + * @function trackProductInsightsAgentViews + * @param {object} parameters - Additional parameters to be sent with request + * @param {array.<{question: string}>} parameters.questions - List of pre-defined questions shown to the user + * @param {string} parameters.itemId - Product id whose page we are on + * @param {string} parameters.itemName - Product name whose page we are on + * @param {array.<{start: string | undefined, + * end: string | undefined}>} parameters.viewTimespans - List of timestamp pairs in ISO_8601 format + * @param {string} [parameters.variationId] - Variation id whose page we are on + * @param {string} [parameters.section] - The section name for the item Ex. "Products" + * @param {object} [networkParameters] - Parameters relevant to the network request + * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) + * @returns {(true|Error)} + * @description The product insights agent element appeared in the visible part of the page + * @example + * constructorio.tracker.trackProductInsightsAgentViews({ + * { + * 'itemId': '1', + * 'itemName': 'item1', + * 'variationId': '2', + * 'questions': [ + * { question: 'Why choose this?' }, + * { question: 'How is this product made?' }, + * { question: 'What are the dimensions of this product?' } + * ], + * 'viewTimespans': [ + * { + * 'start': '2025-05-19T14:30:00+02:00', + * 'end': '2025-05-19T14:30:05+02:00' + * }, + * { + * 'start': '2025-05-19T14:30:10+02:00', + * 'end': '2025-05-19T14:30:15+02:00' + * } + * ] + * }, + * ); + */ trackProductInsightsAgentViews(parameters, networkParameters = {}) { // Ensure parameters are provided (required) if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { diff --git a/src/types/index.d.ts b/src/types/index.d.ts index c0eab5ea..1d5f0a52 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -47,7 +47,8 @@ export interface ConstructorClientOptions { version?: string; serviceUrl?: string; quizzesServiceUrl?: string; - assistantServiceUrl?: string, + agentServiceUrl?: string; + assistantServiceUrl?: string; sessionId?: number; clientId?: string; userId?: string; diff --git a/src/types/tracker.d.ts b/src/types/tracker.d.ts index d508298a..35a02d73 100644 --- a/src/types/tracker.d.ts +++ b/src/types/tracker.d.ts @@ -266,6 +266,71 @@ declare class Tracker { networkParameters?: NetworkParameters ): true | Error; + trackAgentSubmit( + parameters: { + intent: string; + section?: string; + }, + networkParameters?: NetworkParameters + ): true | Error; + + trackAgentResultLoadStarted( + parameters: { + intent: string; + section?: string; + intentResultId?: string; + }, + networkParameters?: NetworkParameters + ): true | Error; + + trackAgentResultLoadFinished( + parameters: { + intent: string; + searchResultCount: number; + section?: string; + intentResultId?: string; + }, + networkParameters?: NetworkParameters + ): true | Error; + + trackAgentResultClick( + parameters: { + intent: string; + searchResultId: string; + itemId?: string; + itemName?: string; + variationId?: string; + section?: string; + intentResultId?: string; + }, + networkParameters?: NetworkParameters + ): true | Error; + + trackAgentResultView( + parameters: { + intent: string; + searchResultId: string; + numResultsViewed: number; + items?: ItemTracked[]; + intentResultId?: string; + section?: string; + }, + networkParameters?: NetworkParameters + ): true | Error; + + trackAgentSearchSubmit( + parameters: { + intent: string; + searchTerm: string; + userInput: string; + searchResultId: string; + groupId?: string; + section?: string; + intentResultId?: string; + }, + networkParameters?: NetworkParameters + ): true | Error; + trackAssistantSubmit( parameters: { intent: string; From 47c2bd18dce78d500c0388c04825d75d1fd8ecf9 Mon Sep 17 00:00:00 2001 From: Evan Yan Date: Tue, 15 Jul 2025 16:00:41 -0700 Subject: [PATCH 04/14] Update tests for tracker --- spec/src/modules/tracker.js | 1787 +++++++++++++++++++++++++++++++++++ 1 file changed, 1787 insertions(+) diff --git a/spec/src/modules/tracker.js b/spec/src/modules/tracker.js index 5f14fc9b..cf6b1142 100644 --- a/spec/src/modules/tracker.js +++ b/spec/src/modules/tracker.js @@ -8303,6 +8303,1793 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => { }); }); + describe('trackAgentSubmit', () => { + const requiredParameters = { intent: 'Show me cookie recipes' }; + const optionalParameters = { + section: 'Products', + }; + + it('Should respond with a valid response when intent and required parameters are provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('key'); + expect(requestParams).to.have.property('i'); + expect(requestParams).to.have.property('s'); + expect(requestParams).to.have.property('c').to.equal(clientVersion); + expect(requestParams).to.have.property('_dt'); + expect(requestParams).to.have.property('intent').to.equal(requiredParameters.intent); + validateOriginReferrer(requestParams); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentSubmit(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when term, required parameters and segments are provided', (done) => { + const segments = ['foo', 'bar']; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + segments, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('us').to.deep.equal(segments); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentSubmit(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required parameters and userId are provided', (done) => { + const userId = 'user-id'; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userId); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentSubmit(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required parameters and testCells are provided', (done) => { + const testCells = { foo: 'bar' }; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + testCells, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property(`ef-${Object.keys(testCells)[0]}`).to.equal(Object.values(testCells)[0]); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentSubmit(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required and optional parameters are provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('section').to.equal('Products'); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentSubmit(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + }); + + it('Should throw an error when no parameters are provided', () => { + const { tracker } = new ConstructorIO({ apiKey: testApiKey }); + + expect(tracker.trackAgentSubmit()).to.be.an('error'); + }); + + it('Should throw an error when invalid parameters are provided', () => { + const { tracker } = new ConstructorIO({ apiKey: testApiKey }); + + expect(tracker.trackAgentSubmit()).to.be.an('error'); + }); + + it('Should send along origin_referrer query param if sendReferrerWithTrackingEvents is true', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + sendReferrerWithTrackingEvents: true, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + validateOriginReferrer(requestParams); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentSubmit(requiredParameters)).to.equal(true); + }); + + it('Should not send along origin_referrer query param if sendReferrerWithTrackingEvents is false', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + sendReferrerWithTrackingEvents: false, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.not.have.property('origin_referrer'); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentSubmit(requiredParameters)).to.equal(true); + }); + + if (!skipNetworkTimeoutTests) { + it('Should be rejected when network request timeout is provided and reached', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + ...requestQueueOptions, + }); + + tracker.on('error', ({ message }) => { + expect(message).to.equal(timeoutRejectionMessage); + done(); + }); + + expect(tracker.trackAgentSubmit(requiredParameters, { timeout: 10 })).to.equal(true); + }); + + it('Should be rejected when global network request timeout is provided and reached', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + networkParameters: { + timeout: 20, + }, + ...requestQueueOptions, + }); + + tracker.on('error', ({ message }) => { + expect(message).to.equal(timeoutRejectionMessage); + done(); + }); + + expect(tracker.trackAgentSubmit(requiredParameters)).to.equal(true); + }); + } + + it('Should properly encode query parameters', (done) => { + const specialCharacters = '+[]&'; + const userId = `user-id ${specialCharacters}`; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userId); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentSubmit(requiredParameters)).to.equal(true); + }); + + it('Should properly transform non-breaking spaces in parameters', (done) => { + const breakingSpaces = '   '; + const userId = `user-id ${breakingSpaces} user-id`; + const userIdExpected = 'user-id user-id'; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userIdExpected); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentSubmit(requiredParameters)).to.equal(true); + }); + }); + + describe('trackAgentResultLoadStarted', () => { + const requiredParameters = { intent: 'Show me cookie recipes' }; + const optionalParameters = { + section: 'Products', + intentResultId: '123451', + }; + + it('Should respond with a valid response when term and required parameters are provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('key'); + expect(requestParams).to.have.property('i'); + expect(requestParams).to.have.property('s'); + expect(requestParams).to.have.property('c').to.equal(clientVersion); + expect(requestParams).to.have.property('_dt'); + expect(requestParams).to.have.property('intent').to.equal(requiredParameters.intent); + validateOriginReferrer(requestParams); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultLoadStarted(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when term, required parameters and segments are provided', (done) => { + const segments = ['foo', 'bar']; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + segments, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('us').to.deep.equal(segments); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultLoadStarted(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required parameters and userId are provided', (done) => { + const userId = 'user-id'; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userId); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultLoadStarted(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required parameters and testCells are provided', (done) => { + const testCells = { foo: 'bar' }; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + testCells, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property(`ef-${Object.keys(testCells)[0]}`).to.equal(Object.values(testCells)[0]); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultLoadStarted(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required and optional parameters are provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('section').to.equal(optionalParameters.section); + expect(requestParams).to.have.property('intent_result_id').to.equal(optionalParameters.intentResultId); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + // eslint-disable-next-line max-len + expect(tracker.trackAgentResultLoadStarted(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + }); + + it('Should throw an error when no parameters are provided', () => { + const { tracker } = new ConstructorIO({ apiKey: testApiKey }); + + expect(tracker.trackAgentResultLoadStarted()).to.be.an('error'); + }); + + it('Should throw an error when invalid parameters are provided', () => { + const { tracker } = new ConstructorIO({ apiKey: testApiKey }); + + expect(tracker.trackAgentResultLoadStarted()).to.be.an('error'); + }); + + it('Should send along origin_referrer query param if sendReferrerWithTrackingEvents is true', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + sendReferrerWithTrackingEvents: true, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + validateOriginReferrer(requestParams); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultLoadStarted(requiredParameters)).to.equal(true); + }); + + it('Should not send along origin_referrer query param if sendReferrerWithTrackingEvents is false', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + sendReferrerWithTrackingEvents: false, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.not.have.property('origin_referrer'); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultLoadStarted(requiredParameters)).to.equal(true); + }); + + if (!skipNetworkTimeoutTests) { + it('Should be rejected when network request timeout is provided and reached', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + ...requestQueueOptions, + }); + + tracker.on('error', ({ message }) => { + expect(message).to.equal(timeoutRejectionMessage); + done(); + }); + + expect(tracker.trackAgentResultLoadStarted(requiredParameters, { timeout: 10 })).to.equal(true); + }); + + it('Should be rejected when global network request timeout is provided and reached', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + networkParameters: { + timeout: 20, + }, + ...requestQueueOptions, + }); + + tracker.on('error', ({ message }) => { + expect(message).to.equal(timeoutRejectionMessage); + done(); + }); + + expect(tracker.trackAgentResultLoadStarted(requiredParameters)).to.equal(true); + }); + } + + it('Should properly encode query parameters', (done) => { + const specialCharacters = '+[]&'; + const userId = `user-id ${specialCharacters}`; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userId); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultLoadStarted(requiredParameters)).to.equal(true); + }); + + it('Should properly transform non-breaking spaces in parameters', (done) => { + const breakingSpaces = '   '; + const userId = `user-id ${breakingSpaces} user-id`; + const userIdExpected = 'user-id user-id'; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userIdExpected); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultLoadStarted(requiredParameters)).to.equal(true); + }); + }); + + describe('trackAgentResultLoadFinished', () => { + const requiredParameters = { intent: 'Show me cookie recipes', searchResultCount: 15 }; + const optionalParameters = { + section: 'Products', + intentResultId: '123451', + }; + + it('Should respond with a valid response when term and required parameters are provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('key'); + expect(requestParams).to.have.property('i'); + expect(requestParams).to.have.property('s'); + expect(requestParams).to.have.property('c').to.equal(clientVersion); + expect(requestParams).to.have.property('_dt'); + expect(requestParams).to.have.property('intent').to.equal(requiredParameters.intent); + expect(requestParams).to.have.property('search_result_count').to.equal(requiredParameters.searchResultCount); + validateOriginReferrer(requestParams); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultLoadFinished(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when term, required parameters and segments are provided', (done) => { + const segments = ['foo', 'bar']; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + segments, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('us').to.deep.equal(segments); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultLoadFinished(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required parameters and userId are provided', (done) => { + const userId = 'user-id'; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userId); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultLoadFinished(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required parameters and testCells are provided', (done) => { + const testCells = { foo: 'bar' }; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + testCells, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property(`ef-${Object.keys(testCells)[0]}`).to.equal(Object.values(testCells)[0]); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultLoadFinished(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required and optional parameters are provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('section').to.equal(optionalParameters.section); + expect(requestParams).to.have.property('intent_result_id').to.equal(optionalParameters.intentResultId); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + // eslint-disable-next-line max-len + expect(tracker.trackAgentResultLoadFinished(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + }); + + it('Should throw an error when no parameters are provided', () => { + const { tracker } = new ConstructorIO({ apiKey: testApiKey }); + + expect(tracker.trackAgentResultLoadFinished()).to.be.an('error'); + }); + + it('Should throw an error when invalid parameters are provided', () => { + const { tracker } = new ConstructorIO({ apiKey: testApiKey }); + + expect(tracker.trackAgentResultLoadFinished()).to.be.an('error'); + }); + + it('Should send along origin_referrer query param if sendReferrerWithTrackingEvents is true', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + sendReferrerWithTrackingEvents: true, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + validateOriginReferrer(requestParams); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultLoadFinished(requiredParameters)).to.equal(true); + }); + + it('Should not send along origin_referrer query param if sendReferrerWithTrackingEvents is false', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + sendReferrerWithTrackingEvents: false, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.not.have.property('origin_referrer'); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultLoadFinished(requiredParameters)).to.equal(true); + }); + + if (!skipNetworkTimeoutTests) { + it('Should be rejected when network request timeout is provided and reached', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + ...requestQueueOptions, + }); + + tracker.on('error', ({ message }) => { + expect(message).to.equal(timeoutRejectionMessage); + done(); + }); + + expect(tracker.trackAgentResultLoadFinished(requiredParameters, { timeout: 10 })).to.equal(true); + }); + + it('Should be rejected when global network request timeout is provided and reached', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + networkParameters: { + timeout: 20, + }, + ...requestQueueOptions, + }); + + tracker.on('error', ({ message }) => { + expect(message).to.equal(timeoutRejectionMessage); + done(); + }); + + expect(tracker.trackAgentResultLoadFinished(requiredParameters)).to.equal(true); + }); + } + + it('Should properly encode query parameters', (done) => { + const specialCharacters = '+[]&'; + const userId = `user-id ${specialCharacters}`; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userId); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultLoadFinished(requiredParameters)).to.equal(true); + }); + + it('Should properly transform non-breaking spaces in parameters', (done) => { + const breakingSpaces = '   '; + const userId = `user-id ${breakingSpaces} user-id`; + const userIdExpected = 'user-id user-id'; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userIdExpected); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultLoadFinished(requiredParameters)).to.equal(true); + }); + }); + + describe('trackAgentResultClick', () => { + const requiredParameters = { + intent: 'Show me cookie recipes', + searchResultId: '12341cd', + itemName: 'espresso', + itemId: '1123', + }; + const optionalParameters = { + section: 'Products', + intentResultId: '12312', + variationId: '123123', + }; + + it('Should respond with a valid response when required parameters are provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('key'); + expect(requestParams).to.have.property('i'); + expect(requestParams).to.have.property('s'); + expect(requestParams).to.have.property('c').to.equal(clientVersion); + expect(requestParams).to.have.property('_dt'); + validateOriginReferrer(requestParams); + expect(requestParams).to.have.property('intent').to.equal(requiredParameters.intent); + expect(requestParams).to.have.property('search_result_id').to.equal(requiredParameters.searchResultId); + expect(requestParams).to.have.property('item_name').to.equal(requiredParameters.itemName); + expect(requestParams).to.have.property('item_id').to.equal(requiredParameters.itemId); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultClick(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required parameters and segments are provided', (done) => { + const segments = ['foo', 'bar']; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + segments, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractUrlParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('us').to.deep.equal(segments); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultClick(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required parameters and userId are provided', (done) => { + const userId = 'user-id'; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractUrlParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userId); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultClick(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required parameters and testCells are provided', (done) => { + const testCells = { foo: 'bar' }; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + testCells, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractUrlParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property(`ef-${Object.keys(testCells)[0]}`).to.equal(Object.values(testCells)[0]); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultClick(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required and optional parameters are provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('intent_result_id').to.equal(optionalParameters.intentResultId); + expect(requestParams).to.have.property('section').to.equal(optionalParameters.section); + expect(requestParams).to.have.property('variation_id').to.equal(optionalParameters.variationId); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultClick(Object.assign(requiredParameters, optionalParameters))) + .to.equal(true); + }); + + it('Should throw an error when invalid parameters are provided', () => { + const { tracker } = new ConstructorIO({ apiKey: testApiKey }); + + expect(tracker.trackAgentResultClick([])).to.be.an('error'); + }); + + it('Should throw an error when no parameters are provided', () => { + const { tracker } = new ConstructorIO({ apiKey: testApiKey }); + + expect(tracker.trackAgentResultClick()).to.be.an('error'); + }); + + it('Should send along origin_referrer query param if sendReferrerWithTrackingEvents is true', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + sendReferrerWithTrackingEvents: true, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractUrlParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + validateOriginReferrer(requestParams); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultClick(requiredParameters)).to.equal(true); + }); + + it('Should not send along origin_referrer query param if sendReferrerWithTrackingEvents is false', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + sendReferrerWithTrackingEvents: false, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractUrlParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.not.have.property('origin_referrer'); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultClick(requiredParameters)).to.equal(true); + }); + + if (!skipNetworkTimeoutTests) { + it('Should be rejected when network request timeout is provided and reached', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + ...requestQueueOptions, + }); + + tracker.on('error', ({ message }) => { + expect(message).to.equal(timeoutRejectionMessage); + done(); + }); + + expect(tracker.trackAgentResultClick(requiredParameters, { timeout: 10 })).to.equal(true); + }); + + it('Should be rejected when global network request timeout is provided and reached', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + networkParameters: { + timeout: 20, + }, + ...requestQueueOptions, + }); + + tracker.on('error', ({ message }) => { + expect(message).to.equal(timeoutRejectionMessage); + done(); + }); + + expect(tracker.trackAgentResultClick(requiredParameters)).to.equal(true); + }); + } + + it('Should properly encode query parameters', (done) => { + const specialCharacters = '+[]&'; + const userId = `user-id ${specialCharacters}`; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractUrlParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userId); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultClick(requiredParameters)).to.equal(true); + }); + + it('Should properly transform non-breaking spaces in parameters', (done) => { + const breakingSpaces = '   '; + const userId = `user-id ${breakingSpaces} user-id`; + const userIdExpected = 'user-id user-id'; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractUrlParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userIdExpected); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultClick(requiredParameters)).to.equal(true); + }); + }); + + describe('trackAgentResultView', () => { + const requiredParameters = { + intent: 'Show me cookie recipes', + numResultsViewed: 5, + searchResultId: '123123123', + }; + const optionalParameters = { + intentResultId: 'result-id', + section: 'Products', + items: [ + { + itemId: '123', + variationId: '456', + }, + { + itemName: 'product test', + itemId: '789', + }, + ], + }; + const snakeCaseItems = [ + { + item_id: '123', + variation_id: '456', + }, + { + item_name: 'product test', + item_id: '789', + }, + ]; + + it('Should respond with a valid response when required parameters are provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('key'); + expect(requestParams).to.have.property('i'); + expect(requestParams).to.have.property('s'); + expect(requestParams).to.have.property('c').to.equal(clientVersion); + expect(requestParams).to.have.property('_dt'); + expect(requestParams).to.have.property('intent').to.equal(requiredParameters.intent); + expect(requestParams).to.have.property('search_result_id').to.equal(requiredParameters.searchResultId); + expect(requestParams).to.have.property('num_results_viewed').to.equal(requiredParameters.numResultsViewed); + validateOriginReferrer(requestParams); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackAgentResultView(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required parameters and segments are provided', (done) => { + const segments = ['foo', 'bar']; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + segments, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('us').to.deep.equal(segments); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackAgentResultView(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required parameters and testCells are provided', (done) => { + const testCells = { foo: 'bar' }; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + testCells, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property(`ef-${Object.keys(testCells)[0]}`).to.equal(Object.values(testCells)[0]); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackAgentResultView(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when parameters and userId are provided', (done) => { + const userId = 'user-id'; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userId); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackAgentResultView(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required and optional parameters are provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('intent_result_id').to.equal(optionalParameters.intentResultId); + expect(requestParams).to.have.property('items').to.deep.equal(snakeCaseItems); + expect(requestParams).to.have.property('section').to.equal(optionalParameters.section); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message'); + + done(); + }); + + expect(tracker.trackAgentResultView(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + }); + + it('Should throw an error when invalid parameters are provided', () => { + const { tracker } = new ConstructorIO({ apiKey: testApiKey }); + + expect(tracker.trackAgentResultView([])).to.be.an('error'); + }); + + it('Should throw an error when no parameters are provided', () => { + const { tracker } = new ConstructorIO({ apiKey: testApiKey }); + + expect(tracker.trackAgentResultView()).to.be.an('error'); + }); + + it('Should send along origin_referrer query param if sendReferrerWithTrackingEvents is true', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + sendReferrerWithTrackingEvents: true, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractUrlParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + validateOriginReferrer(requestParams); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultView(requiredParameters)).to.equal(true); + }); + + it('Should not send along origin_referrer query param if sendReferrerWithTrackingEvents is false', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + sendReferrerWithTrackingEvents: false, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractUrlParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.not.have.property('origin_referrer'); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultView(requiredParameters)).to.equal(true); + }); + + if (!skipNetworkTimeoutTests) { + it('Should be rejected when network request timeout is provided and reached', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + ...requestQueueOptions, + }); + + tracker.on('error', ({ message }) => { + expect(message).to.equal(timeoutRejectionMessage); + done(); + }); + + expect(tracker.trackAgentResultView(requiredParameters, { timeout: 10 })).to.equal(true); + }); + + it('Should be rejected when global network request timeout is provided and reached', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + networkParameters: { + timeout: 20, + }, + ...requestQueueOptions, + }); + + tracker.on('error', ({ message }) => { + expect(message).to.equal(timeoutRejectionMessage); + done(); + }); + + expect(tracker.trackAgentResultView(requiredParameters)).to.equal(true); + }); + } + + it('Should not encode body parameters', (done) => { + const specialCharacters = '+[]&'; + const userId = `user-id ${specialCharacters}`; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userId); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultView(requiredParameters)).to.equal(true); + }); + + it('Should properly transform non-breaking spaces in parameters', (done) => { + const breakingSpaces = '   '; + const userId = `user-id ${breakingSpaces} user-id`; + const userIdExpected = 'user-id user-id'; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userIdExpected); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentResultView(requiredParameters)).to.equal(true); + }); + }); + + describe('trackAgentSearchSubmit', () => { + const requiredParameters = { intent: 'Show me cookie recipes', searchTerm: 'Flour', searchResultId: '123' }; + const optionalParameters = { + section: 'Products', + intentResultId: '1234', + }; + + it('Should respond with a valid response when term and required parameters are provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('key'); + expect(requestParams).to.have.property('i'); + expect(requestParams).to.have.property('s'); + expect(requestParams).to.have.property('c').to.equal(clientVersion); + expect(requestParams).to.have.property('_dt'); + expect(requestParams).to.have.property('intent').to.equal(requiredParameters.intent); + expect(requestParams).to.have.property('search_term').to.equal(requiredParameters.searchTerm); + expect(requestParams).to.have.property('search_result_id').to.equal(requiredParameters.searchResultId); + validateOriginReferrer(requestParams); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentSearchSubmit(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when term, required parameters and segments are provided', (done) => { + const segments = ['foo', 'bar']; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + segments, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('us').to.deep.equal(segments); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentSearchSubmit(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required parameters and userId are provided', (done) => { + const userId = 'user-id'; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userId); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentSearchSubmit(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required parameters and testCells are provided', (done) => { + const testCells = { foo: 'bar' }; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + testCells, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property(`ef-${Object.keys(testCells)[0]}`).to.equal(Object.values(testCells)[0]); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentSearchSubmit(requiredParameters)).to.equal(true); + }); + + it('Should respond with a valid response when required and optional parameters are provided', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('section').to.equal(optionalParameters.section); + expect(requestParams).to.have.property('intent_result_id').to.equal(optionalParameters.intentResultId); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentSearchSubmit(Object.assign(requiredParameters, optionalParameters))).to.equal(true); + }); + + it('Should throw an error when no parameters are provided', () => { + const { tracker } = new ConstructorIO({ apiKey: testApiKey }); + + expect(tracker.trackAgentSearchSubmit()).to.be.an('error'); + }); + + it('Should throw an error when invalid parameters are provided', () => { + const { tracker } = new ConstructorIO({ apiKey: testApiKey }); + + expect(tracker.trackAgentSearchSubmit()).to.be.an('error'); + }); + + it('Should send along origin_referrer query param if sendReferrerWithTrackingEvents is true', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + sendReferrerWithTrackingEvents: true, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + validateOriginReferrer(requestParams); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentSearchSubmit(requiredParameters)).to.equal(true); + }); + + it('Should not send along origin_referrer query param if sendReferrerWithTrackingEvents is false', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + sendReferrerWithTrackingEvents: false, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.not.have.property('origin_referrer'); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentSearchSubmit(requiredParameters)).to.equal(true); + }); + + if (!skipNetworkTimeoutTests) { + it('Should be rejected when network request timeout is provided and reached', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + ...requestQueueOptions, + }); + + tracker.on('error', ({ message }) => { + expect(message).to.equal(timeoutRejectionMessage); + done(); + }); + + expect(tracker.trackAgentSearchSubmit(requiredParameters, { timeout: 10 })).to.equal(true); + }); + + it('Should be rejected when global network request timeout is provided and reached', (done) => { + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + networkParameters: { + timeout: 20, + }, + ...requestQueueOptions, + }); + + tracker.on('error', ({ message }) => { + expect(message).to.equal(timeoutRejectionMessage); + done(); + }); + + expect(tracker.trackAgentSearchSubmit(requiredParameters)).to.equal(true); + }); + } + + it('Should properly encode query parameters', (done) => { + const specialCharacters = '+[]&'; + const userId = `user-id ${specialCharacters}`; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userId); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentSearchSubmit(requiredParameters)).to.equal(true); + }); + + it('Should properly transform non-breaking spaces in parameters', (done) => { + const breakingSpaces = '   '; + const userId = `user-id ${breakingSpaces} user-id`; + const userIdExpected = 'user-id user-id'; + const { tracker } = new ConstructorIO({ + apiKey: testApiKey, + userId, + fetch: fetchSpy, + ...requestQueueOptions, + }); + + tracker.on('success', (responseParams) => { + const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy); + + // Request + expect(fetchSpy).to.have.been.called; + expect(requestParams).to.have.property('ui').to.equal(userIdExpected); + + // Response + expect(responseParams).to.have.property('method').to.equal('POST'); + expect(responseParams).to.have.property('message').to.equal('ok'); + + done(); + }); + + expect(tracker.trackAgentSearchSubmit(requiredParameters)).to.equal(true); + }); + }); + describe('trackAssistantSubmit', () => { const requiredParameters = { intent: 'Show me cookie recipes' }; const optionalParameters = { From 660a802e2bb01ef6c597ab499ba528f837d93c84 Mon Sep 17 00:00:00 2001 From: Evan Yan Date: Tue, 15 Jul 2025 16:09:03 -0700 Subject: [PATCH 05/14] Update docs --- docs/ConstructorIO.html | 74 +- docs/constructorio.js.html | 12 +- docs/index.html | 4 +- docs/module-agent.html | 474 +++++ docs/module-assistant.html | 325 +-- docs/module-autocomplete.html | 4 +- docs/module-browse.html | 4 +- docs/module-quizzes.html | 4 +- docs/module-recommendations.html | 4 +- docs/module-search.html | 4 +- docs/module-tracker.html | 2751 +++++++++++++++++++++++++- docs/modules_agent.js.html | 263 +++ docs/modules_assistant.js.html | 179 +- docs/modules_autocomplete.js.html | 4 +- docs/modules_browse.js.html | 4 +- docs/modules_quizzes.js.html | 4 +- docs/modules_recommendations.js.html | 4 +- docs/modules_search.js.html | 4 +- docs/modules_tracker.js.html | 242 ++- src/modules/tracker.js | 78 +- 20 files changed, 3831 insertions(+), 611 deletions(-) create mode 100644 docs/module-agent.html create mode 100644 docs/modules_agent.js.html diff --git a/docs/ConstructorIO.html b/docs/ConstructorIO.html index dde9cab5..541f75d8 100644 --- a/docs/ConstructorIO.html +++ b/docs/ConstructorIO.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -83,7 +83,7 @@

new Cons
Source:
@@ -288,6 +288,30 @@

Properties:
+ + + agent + + + + + +object + + + + + + + + + + + Interface to module:agent + + + + assistant @@ -513,6 +537,46 @@
Properties
+ + + agentServiceUrl + + + + + +string + + + + + + + + + + <optional>
+ + + + + + + + + + + + 'https://agent.cnstrc.com' + + + + + AI Shopping Agent API URL endpoint + + + + assistantServiceUrl @@ -548,7 +612,7 @@
Properties
- AI Assistant API URL endpoint + AI Shopping Assistant API URL endpoint @@ -1275,7 +1339,7 @@

setCl
Source:
@@ -1607,7 +1671,7 @@

Properties

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/constructorio.js.html b/docs/constructorio.js.html index 84d7544c..98f215e4 100644 --- a/docs/constructorio.js.html +++ b/docs/constructorio.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -60,6 +60,7 @@

constructorio.js

const helpers = require('./utils/helpers'); const { default: packageVersion } = require('./version'); const Quizzes = require('./modules/quizzes'); +const Agent = require('./modules/agent'); const Assistant = require('./modules/assistant'); // Compute package version string @@ -87,7 +88,8 @@

constructorio.js

* @param {string} parameters.apiKey - Constructor.io API key * @param {string} [parameters.serviceUrl='https://ac.cnstrc.com'] - API URL endpoint * @param {string} [parameters.quizzesServiceUrl='https://quizzes.cnstrc.com'] - Quizzes API URL endpoint - * @param {string} [parameters.assistantServiceUrl='https://assistant.cnstrc.com'] - AI Assistant API URL endpoint + * @param {string} [parameters.agentServiceUrl='https://agent.cnstrc.com'] - AI Shopping Agent API URL endpoint + * @param {string} [parameters.assistantServiceUrl='https://assistant.cnstrc.com'] - AI Shopping Assistant API URL endpoint * @param {array} [parameters.segments] - User segments * @param {object} [parameters.testCells] - User test cells * @param {string} [parameters.clientId] - Client ID, defaults to value supplied by 'constructorio-id' module @@ -109,6 +111,7 @@

constructorio.js

* @property {object} recommendations - Interface to {@link module:recommendations} * @property {object} tracker - Interface to {@link module:tracker} * @property {object} quizzes - Interface to {@link module:quizzes} + * @property {object} agent - Interface to {@link module:agent} * @property {object} assistant - Interface to {@link module:assistant} * @returns {class} */ @@ -118,6 +121,7 @@

constructorio.js

version: versionFromOptions, serviceUrl, quizzesServiceUrl, + agentServiceUrl, assistantServiceUrl, segments, testCells, @@ -164,6 +168,7 @@

constructorio.js

version: versionFromOptions || versionFromGlobal || computePackageVersion(), serviceUrl: helpers.addHTTPSToString(normalizedServiceUrl) || 'https://ac.cnstrc.com', quizzesServiceUrl: (quizzesServiceUrl && quizzesServiceUrl.replace(/\/$/, '')) || 'https://quizzes.cnstrc.com', + agentServiceUrl: (agentServiceUrl && agentServiceUrl.replace(/\/$/, '')) || 'https://agent.cnstrc.com', assistantServiceUrl: (assistantServiceUrl && assistantServiceUrl.replace(/\/$/, '')) || 'https://assistant.cnstrc.com', sessionId: sessionId || session_id, clientId: clientId || client_id, @@ -186,6 +191,7 @@

constructorio.js

this.recommendations = new Recommendations(this.options); this.tracker = new Tracker(this.options); this.quizzes = new Quizzes(this.options); + this.agent = new Agent(this.options); this.assistant = new Assistant(this.options); // Dispatch initialization event @@ -254,7 +260,7 @@

constructorio.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/index.html b/docs/index.html index 4ba851c0..5010da4c 100644 --- a/docs/index.html +++ b/docs/index.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -114,7 +114,7 @@

Development / npm commands


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-agent.html b/docs/module-agent.html new file mode 100644 index 00000000..faaea44b --- /dev/null +++ b/docs/module-agent.html @@ -0,0 +1,474 @@ + + + + + + agent - Documentation + + + + + + + + + + + + + + + + + + + + +
+ +

agent

+ + + + + + + +
+ +
+ +
+ +
+ +
+ + + +
+ +
Description:
+
  • Interface to agent SSE. +Replaces the previous Assistant module.
+ + + +
Source:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +
Interface to agent SSE. +Replaces the previous Assistant module.
+ + + + +
+ + + + + + + + + + + + + + + + + +

Methods

+ + + + + + +

(inner) getAgentResultsStream(intent, parametersopt) → {ReadableStream}

+ + + + + + +
+ +
Description:
+
  • Retrieve a stream of agent results from Constructor.io API
+ + + +
Source:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + +
Example
+ +
const readableStream = constructorio.agent.getAgentResultsStream('I want to get shoes', {
+    domain: "nike_sportswear",
+});
+const reader = readableStream.getReader();
+const { value, done } = await reader.read();
+ + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
intent + + +string + + + + + + + + + + + Intent to use to perform an intent based recommendations
parameters + + +object + + + + + + + <optional>
+ + + + + +
Additional parameters to refine result set +
Properties
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
domain + + +string + + + + + + + <optional>
+ + + + + +
domain name e.g. swimming sports gear, groceries
numResultsPerPage + + +number + + + + + + + <optional>
+ + + + + +
The total number of results to return
+ +
+ + + + + + + + + + + + + + + + +
Returns:
+ + +
+ Returns a ReadableStream. +
+ + + +
+
+ Type +
+
+ +ReadableStream + + + +
+
+ + + + + + + + + + + +
+ +
+ + + + + + +
+ +
+ +
+ Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. +
+ + + + + + + + + + + \ No newline at end of file diff --git a/docs/module-assistant.html b/docs/module-assistant.html index 78d4d05c..f66379ac 100644 --- a/docs/module-assistant.html +++ b/docs/module-assistant.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -59,14 +59,11 @@

assistant

-
Description:
-
  • Interface to assistant SSE.
-
Source:
@@ -85,6 +82,9 @@

assistant

+
Deprecated:
  • This module is deprecated and will be removed in a future version. Use the Agent module instead. +Interface to assistant SSE.
+ @@ -105,8 +105,6 @@

assistant

-
Interface to assistant SSE.
- @@ -128,317 +126,6 @@

assistant

-

Methods

- - - - - - -

(inner) getAssistantResultsStream(intent, parametersopt) → {ReadableStream}

- - - - - - -
- -
Description:
-
  • Retrieve a stream of assistant results from Constructor.io API
- - - -
Source:
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
Example
- -
const readableStream = constructorio.assistant.getAssistantResultsStream('I want to get shoes', {
-    domain: "nike_sportswear",
-});
-const reader = readableStream.getReader();
-const { value, done } = await reader.read();
- - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
intent - - -string - - - - - - - - - - - Intent to use to perform an intent based recommendations
parameters - - -object - - - - - - - <optional>
- - - - - -
Additional parameters to refine result set -
Properties
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
domain - - -string - - - - - - - <optional>
- - - - - -
domain name e.g. swimming sports gear, groceries
numResultsPerPage - - -number - - - - - - - <optional>
- - - - - -
The total number of results to return
- -
- - - - - - - - - - - - - - - - -
Returns:
- - -
- Returns a ReadableStream. -
- - - -
-
- Type -
-
- -ReadableStream - - - -
-
- - - - - - - @@ -457,7 +144,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-autocomplete.html b/docs/module-autocomplete.html index de449096..49d54de6 100644 --- a/docs/module-autocomplete.html +++ b/docs/module-autocomplete.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -798,7 +798,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-browse.html b/docs/module-browse.html index 1dffda0e..ddd759fd 100644 --- a/docs/module-browse.html +++ b/docs/module-browse.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -3216,7 +3216,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-quizzes.html b/docs/module-quizzes.html index 54fab57e..649f4f20 100644 --- a/docs/module-quizzes.html +++ b/docs/module-quizzes.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -1661,7 +1661,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-recommendations.html b/docs/module-recommendations.html index def47d89..732f8a92 100644 --- a/docs/module-recommendations.html +++ b/docs/module-recommendations.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -798,7 +798,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-search.html b/docs/module-search.html index e4459399..9df181fc 100644 --- a/docs/module-search.html +++ b/docs/module-search.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -1750,7 +1750,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-tracker.html b/docs/module-tracker.html index 02926176..0b257a10 100644 --- a/docs/module-tracker.html +++ b/docs/module-tracker.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -151,7 +151,7 @@

(inn
Source:
@@ -293,6 +293,2711 @@
Parameters:
+
Returns:
+ + + + +
+
+ Type +
+
+ +true +| + +Error + + + +
+
+ + + + + + + + + + +

(inner) trackAgentResultClick(parameters, networkParametersopt) → {true|Error}

+ + + + + + +
+ +
Description:
+
  • User clicked a result that appeared within an agent search result
+ + + +
Source:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + +
Example
+ +
constructorio.tracker.trackAgentResultClick(
+    {
+        variationId: 'KMH879-7632',
+        searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2',
+        intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2',
+        intent: 'show me a recipe for a cookie',
+        itemId: 'KMH876',
+    },
+);
+ + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
parameters + + +object + + + + + + + + + + + Additional parameters to be sent with request +
Properties
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
intent + + +string + + + + + + + + + + + intent of the user
searchResultId + + +string + + + + + + + + + + + result_id of the specific search result the clicked item belongs to
itemId + + +string + + + + + + + + + + + Product item unique identifier
itemName + + +string + + + + + + + + + + + Product item name
section + + +string + + + + + + + <optional>
+ + + + + +
The section name for the item Ex. "Products"
variationId + + +string + + + + + + + <optional>
+ + + + + +
Product item variation unique identifier
intentResultId + + +string + + + + + + + <optional>
+ + + + + +
Browse result identifier (returned in response from Constructor)
+ +
networkParameters + + +object + + + + + + + <optional>
+ + + + + +
Parameters relevant to the network request +
Properties
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
timeout + + +number + + + + + + + <optional>
+ + + + + +
Request timeout (in milliseconds)
+ +
+ + + + + + + + + + + + + + + + +
Returns:
+ + + + +
+
+ Type +
+
+ +true +| + +Error + + + +
+
+ + + + + + + + + + +

(inner) trackAgentResultLoadFinished(parameters, networkParametersopt) → {true|Error}

+ + + + + + +
+ +
Description:
+
  • Agent results page load finished
+ + + +
Source:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + +
Example
+ +
constructorio.tracker.trackAgentResultLoadFinished(
+    {
+        intent: 'show me a recipe for a cookie',
+        intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2',
+        searchResultCount: 5,
+    },
+);
+ + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
parameters + + +object + + + + + + + + + + + Additional parameters to be sent with request +
Properties
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
intent + + +string + + + + + + + + + + + Intent of user request
searchResultCount + + +number + + + + + + + + + + + Number of search results loaded
section + + +string + + + + + + + <optional>
+ + + + + +
The section name for the item Ex. "Products"
intentResultId + + +string + + + + + + + <optional>
+ + + + + +
The intent result id from the ASA response
+ +
networkParameters + + +object + + + + + + + <optional>
+ + + + + +
Parameters relevant to the network request +
Properties
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
timeout + + +number + + + + + + + <optional>
+ + + + + +
Request timeout (in milliseconds)
+ +
+ + + + + + + + + + + + + + + + +
Returns:
+ + + + +
+
+ Type +
+
+ +true +| + +Error + + + +
+
+ + + + + + + + + + +

(inner) trackAgentResultLoadStarted(parameters, networkParametersopt) → {true|Error}

+ + + + + + +
+ +
Description:
+
  • Agent results page load begun (but has not necessarily loaded completely)
+ + + +
Source:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + +
Example
+ +
constructorio.tracker.trackAgentResultLoadStarted(
+    {
+        intent: 'show me a recipe for a cookie',
+        intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2',
+    },
+);
+ + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
parameters + + +object + + + + + + + + + + + Additional parameters to be sent with request +
Properties
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
intent + + +string + + + + + + + + + + + Intent of user request
section + + +string + + + + + + + <optional>
+ + + + + +
The section name for the item Ex. "Products"
intentResultId + + +string + + + + + + + <optional>
+ + + + + +
The intent result id from the ASA response
+ +
networkParameters + + +object + + + + + + + <optional>
+ + + + + +
Parameters relevant to the network request +
Properties
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
timeout + + +number + + + + + + + <optional>
+ + + + + +
Request timeout (in milliseconds)
+ +
+ + + + + + + + + + + + + + + + +
Returns:
+ + + + +
+
+ Type +
+
+ +true +| + +Error + + + +
+
+ + + + + + + + + + +

(inner) trackAgentResultView(parameters, networkParametersopt) → {true|Error}

+ + + + + + +
+ +
Description:
+
  • User viewed a search result within an agent result
+ + + +
Source:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + +
Example
+ +
constructorio.tracker.trackAgentResultView(
+    {
+        searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2',
+        intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2',
+        intent: 'show me a recipe for a cookie',
+        numResultsViewed: 5,
+        items: [{itemId: 'KMH876'}, {itemId: 'KMH140'}, {itemId: 'KMH437'}],
+    },
+);
+ + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
parameters + + +object + + + + + + + + + + + Additional parameters to be sent with request +
Properties
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
intent + + +string + + + + + + + + + + + intent of the user
searchResultId + + +string + + + + + + + + + + + result_id of the specific search result the clicked item belongs to
numResultsViewed + + +number + + + + + + + + + + + Number of items viewed in this search result
items + + +Array.<object> + + + + + + + <optional>
+ + + + + +
List of product item objects viewed
section + + +string + + + + + + + <optional>
+ + + + + +
The section name for the item Ex. "Products"
intentResultId + + +string + + + + + + + <optional>
+ + + + + +
Browse result identifier (returned in response from Constructor)
+ +
networkParameters + + +object + + + + + + + <optional>
+ + + + + +
Parameters relevant to the network request +
Properties
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
timeout + + +number + + + + + + + <optional>
+ + + + + +
Request timeout (in milliseconds)
+ +
+ + + + + + + + + + + + + + + + +
Returns:
+ + + + +
+
+ Type +
+
+ +true +| + +Error + + + +
+
+ + + + + + + + + + +

(inner) trackAgentSearchSubmit(parameters, networkParametersopt) → {true|Error}

+ + + + + + +
+ +
Description:
+
  • User submitted an alternative agent search result search term
+ + + +
Source:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + +
Example
+ +
constructorio.tracker.trackAgentSearchSubmit({
+    {
+        searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2',
+        intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2',
+        intent: 'show me a recipe for a cookie',
+        searchTerm: 'flour',
+    },
+);
+ + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
parameters + + +object + + + + + + + + + + + Additional parameters to be sent with request +
Properties
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
intent + + +string + + + + + + + + + + + Intent of user request
searchTerm + + +string + + + + + + + + + + + Term of submitted assistant search event
searchResultId + + +string + + + + + + + + + + + resultId of search result the clicked item belongs to
section + + +string + + + + + + + <optional>
+ + + + + +
The section name for the item Ex. "Products"
intentResultId + + +string + + + + + + + <optional>
+ + + + + +
intentResultId from the ASA response
+ +
networkParameters + + +object + + + + + + + <optional>
+ + + + + +
Parameters relevant to the network request +
Properties
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
timeout + + +number + + + + + + + <optional>
+ + + + + +
Request timeout (in milliseconds)
+ +
+ + + + + + + + + + + + + + + + +
Returns:
+ + + + +
+
+ Type +
+
+ +true +| + +Error + + + +
+
+ + + + + + + + + + +

(inner) trackAgentSubmit(parameters, networkParametersopt) → {true|Error}

+ + + + + + +
+ +
Description:
+
  • User submitted an agent search + (pressing enter within agent input element, or clicking agent submit element)
+ + + +
Source:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + +
Example
+ +
constructorio.tracker.trackAgentSubmit(
+    {
+        intent: 'show me a recipe for a cookie',
+    },
+);
+ + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
parameters + + +object + + + + + + + + + + + Additional parameters to be sent with request +
Properties
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
intent + + +string + + + + + + + + + + + Intent of user request
section + + +string + + + + + + + <optional>
+ + + + + +
The section name for the item Ex. "Products"
+ +
networkParameters + + +object + + + + + + + <optional>
+ + + + + +
Parameters relevant to the network request +
Properties
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
timeout + + +number + + + + + + + <optional>
+ + + + + +
Request timeout (in milliseconds)
+ +
+ + + + + + + + + + + + + + + +
Returns:
@@ -339,7 +3044,7 @@

Source:
@@ -358,6 +3063,8 @@

Deprecated:
  • This method will be removed in a future version. Use trackAgentResultClick instead.
+ @@ -873,7 +3580,7 @@

Source:
@@ -892,6 +3599,8 @@

Deprecated:
  • This method will be removed in a future version. Use trackAgentResultLoadFinished instead.
+ @@ -1307,7 +4016,7 @@

Source:
@@ -1326,6 +4035,8 @@

Deprecated:
  • This method will be removed in a future version. Use trackAgentResultLoadStarted instead.
+ @@ -1708,7 +4419,7 @@

Source:
@@ -1727,6 +4438,8 @@

Deprecated:
  • This method will be removed in a future version. Use trackAgentResultView instead.
+ @@ -2210,7 +4923,7 @@

Source:
@@ -2229,6 +4942,8 @@

Deprecated:
  • This method will be removed in a future version. Use trackAgentSearchSubmit instead.
+ @@ -2678,7 +5393,7 @@

Source:
@@ -2697,6 +5412,8 @@

Deprecated:
  • This method will be removed in a future version. Use trackAgentSubmit instead.
+ @@ -7142,7 +9859,7 @@

Source:
@@ -7644,7 +10361,7 @@

Source:
@@ -8179,7 +10896,7 @@

Source:
@@ -8613,7 +11330,7 @@

Source:
@@ -9047,7 +11764,7 @@

Source:
@@ -9514,7 +12231,7 @@

Source:
@@ -9981,7 +12698,7 @@

Source:
@@ -10452,7 +13169,7 @@

Source:
@@ -17016,7 +19733,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_agent.js.html b/docs/modules_agent.js.html new file mode 100644 index 00000000..eee9ad2a --- /dev/null +++ b/docs/modules_agent.js.html @@ -0,0 +1,263 @@ + + + + + + modules/agent.js - Documentation + + + + + + + + + + + + + + + + + + + + +
+ +

modules/agent.js

+ + + + + + + +
+
+
const { cleanParams, trimNonBreakingSpaces, encodeURIComponentRFC3986, stringify } = require('../utils/helpers');
+
+// Create URL from supplied intent (term) and parameters
+function createAgentUrl(intent, parameters, options) {
+  const {
+    apiKey,
+    version,
+    sessionId,
+    clientId,
+    userId,
+    segments,
+    testCells,
+    agentServiceUrl,
+    assistantServiceUrl,
+  } = options;
+  let queryParams = { c: version };
+  queryParams.key = apiKey;
+  queryParams.i = clientId;
+  queryParams.s = sessionId;
+
+  const serviceUrl = agentServiceUrl || assistantServiceUrl;
+
+  // Validate intent is provided
+  if (!intent || typeof intent !== 'string') {
+    throw new Error('intent is a required parameter of type string');
+  }
+
+  // Validate domain is provided
+  if (!parameters.domain || typeof parameters.domain !== 'string') {
+    throw new Error('parameters.domain is a required parameter of type string');
+  }
+
+  // Pull test cells from options
+  if (testCells) {
+    Object.keys(testCells).forEach((testCellKey) => {
+      queryParams[`ef-${testCellKey}`] = testCells[testCellKey];
+    });
+  }
+
+  // Pull user segments from options
+  if (segments && segments.length) {
+    queryParams.us = segments;
+  }
+
+  // Pull user id from options and ensure string
+  if (userId) {
+    queryParams.ui = String(userId);
+  }
+
+  if (parameters) {
+    const { domain, numResultsPerPage } = parameters;
+
+    // Pull domain from parameters
+    if (domain) {
+      queryParams.domain = domain;
+    }
+
+    // Pull results number from parameters
+    if (numResultsPerPage) {
+      queryParams.num_results_per_page = numResultsPerPage;
+    }
+  }
+
+  // eslint-disable-next-line no-underscore-dangle
+  queryParams._dt = Date.now();
+  queryParams = cleanParams(queryParams);
+
+  const queryString = stringify(queryParams);
+  const cleanedQuery = intent.replace(/^\//, '|'); // For compatibility with backend API
+
+  return `${serviceUrl}/v1/intent/${encodeURIComponentRFC3986(trimNonBreakingSpaces(cleanedQuery))}?${queryString}`;
+}
+
+// Add event listeners to custom SSE that pushes data to the stream
+function setupEventListeners(eventSource, controller, eventTypes) {
+  const addListener = (type) => {
+    eventSource.addEventListener(type, (event) => {
+      const data = JSON.parse(event.data);
+
+      controller.enqueue({ type, data }); // Enqueue data into the stream
+    });
+  };
+
+  // Set up listeners for all event types except END
+  Object.values(eventTypes).forEach((type) => {
+    if (type !== eventTypes.END) {
+      addListener(type);
+    }
+  });
+
+  // Handle the END event separately to close the stream
+  eventSource.addEventListener(eventTypes.END, () => {
+    controller.close(); // Close the stream
+    eventSource.close(); // Close the EventSource connection
+  });
+
+  // Handle errors from the EventSource
+  // eslint-disable-next-line no-param-reassign
+  eventSource.onerror = (error) => {
+    controller.error(error); // Pass the error to the stream
+    eventSource.close(); // Close the EventSource connection
+  };
+}
+
+/**
+ * Interface to agent SSE.
+ * Replaces the previous Assistant module.
+ *
+ * @module agent
+ * @inner
+ * @returns {object}
+ */
+class Agent {
+  constructor(options) {
+    this.options = options || {};
+  }
+
+  static EventTypes = {
+    START: 'start', // Denotes the start of the stream
+    GROUP: 'group', // Represents a semantic grouping of search results, optionally having textual explanation
+    SEARCH_RESULT: 'search_result', // Represents a set of results with metadata (used to show results with search refinements)
+    ARTICLE_REFERENCE: 'article_reference', // Represents a set of content with metadata
+    RECIPE_INFO: 'recipe_info', // Represents recipes' auxiliary information like cooking times & serving sizes
+    RECIPE_INSTRUCTIONS: 'recipe_instructions', // Represents recipe instructions
+    SERVER_ERROR: 'server_error', // Server Error event
+    IMAGE_META: 'image_meta', // This event type is used for enhancing recommendations with media content such as images
+    END: 'end', // Represents the end of data stream
+  };
+
+  /**
+   * Retrieve agent results from EventStream
+   *
+   * @function getAgentResultsStream
+   * @description Retrieve a stream of agent results from Constructor.io API
+   * @param {string} intent - Intent to use to perform an intent based recommendations
+   * @param {object} [parameters] - Additional parameters to refine result set
+   * @param {string} [parameters.domain] - domain name e.g. swimming sports gear, groceries
+   * @param {number} [parameters.numResultsPerPage] - The total number of results to return
+   * @returns {ReadableStream} Returns a ReadableStream.
+   * @example
+   * const readableStream = constructorio.agent.getAgentResultsStream('I want to get shoes', {
+   *     domain: "nike_sportswear",
+   * });
+   * const reader = readableStream.getReader();
+   * const { value, done } = await reader.read();
+   */
+  getAgentResultsStream(query, parameters) {
+    let eventSource;
+    let readableStream;
+
+    try {
+      const requestUrl = createAgentUrl(query, parameters, this.options);
+
+      // Create an EventSource that connects to the Server Sent Events API
+      eventSource = new EventSource(requestUrl);
+
+      // Create a readable stream that data will be pushed into
+      readableStream = new ReadableStream({
+        // To be called on stream start
+        start(controller) {
+          // Listen to events emitted from ASA Server Sent Events and push data to the ReadableStream
+          setupEventListeners(eventSource, controller, Agent.EventTypes);
+        },
+        // To be called on stream cancelling
+        cancel() {
+          // Close the EventSource connection when the stream is prematurely canceled
+          eventSource.close();
+        },
+      });
+    } catch (e) {
+      if (readableStream) {
+        readableStream?.cancel();
+      } else {
+        // If the stream was not successfully created, close the EventSource directly
+        eventSource?.close();
+      }
+
+      throw new Error(e.message);
+    }
+
+    return readableStream;
+  }
+}
+
+module.exports = Agent;
+module.exports.createAgentUrl = createAgentUrl;
+module.exports.setupEventListeners = setupEventListeners;
+
+
+
+ + + + + + +
+ +
+ +
+ Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. +
+ + + + + + + + + + + diff --git a/docs/modules_assistant.js.html b/docs/modules_assistant.js.html index 7ca534c1..50eaeff4 100644 --- a/docs/modules_assistant.js.html +++ b/docs/modules_assistant.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -47,189 +47,30 @@

modules/assistant.js

-
const { cleanParams, trimNonBreakingSpaces, encodeURIComponentRFC3986, stringify } = require('../utils/helpers');
-
-// Create URL from supplied intent (term) and parameters
-function createAssistantUrl(intent, parameters, options) {
-  const {
-    apiKey,
-    version,
-    sessionId,
-    clientId,
-    userId,
-    segments,
-    testCells,
-    assistantServiceUrl,
-  } = options;
-  let queryParams = { c: version };
-
-  queryParams.key = apiKey;
-  queryParams.i = clientId;
-  queryParams.s = sessionId;
-
-  // Validate intent is provided
-  if (!intent || typeof intent !== 'string') {
-    throw new Error('intent is a required parameter of type string');
-  }
-
-  // Validate domain is provided
-  if (!parameters.domain || typeof parameters.domain !== 'string') {
-    throw new Error('parameters.domain is a required parameter of type string');
-  }
-
-  // Pull test cells from options
-  if (testCells) {
-    Object.keys(testCells).forEach((testCellKey) => {
-      queryParams[`ef-${testCellKey}`] = testCells[testCellKey];
-    });
-  }
-
-  // Pull user segments from options
-  if (segments && segments.length) {
-    queryParams.us = segments;
-  }
-
-  // Pull user id from options and ensure string
-  if (userId) {
-    queryParams.ui = String(userId);
-  }
-
-  if (parameters) {
-    const { domain, numResultsPerPage } = parameters;
-
-    // Pull domain from parameters
-    if (domain) {
-      queryParams.domain = domain;
-    }
-
-    // Pull results number from parameters
-    if (numResultsPerPage) {
-      queryParams.num_results_per_page = numResultsPerPage;
-    }
-  }
-
-  // eslint-disable-next-line no-underscore-dangle
-  queryParams._dt = Date.now();
-  queryParams = cleanParams(queryParams);
-
-  const queryString = stringify(queryParams);
-  const cleanedQuery = intent.replace(/^\//, '|'); // For compatibility with backend API
-
-  return `${assistantServiceUrl}/v1/intent/${encodeURIComponentRFC3986(trimNonBreakingSpaces(cleanedQuery))}?${queryString}`;
-}
-
-// Add event listeners to custom SSE that pushes data to the stream
-function setupEventListeners(eventSource, controller, eventTypes) {
-  const addListener = (type) => {
-    eventSource.addEventListener(type, (event) => {
-      const data = JSON.parse(event.data);
-
-      controller.enqueue({ type, data }); // Enqueue data into the stream
-    });
-  };
-
-  // Set up listeners for all event types except END
-  Object.values(eventTypes).forEach((type) => {
-    if (type !== eventTypes.END) {
-      addListener(type);
-    }
-  });
-
-  // Handle the END event separately to close the stream
-  eventSource.addEventListener(eventTypes.END, () => {
-    controller.close(); // Close the stream
-    eventSource.close(); // Close the EventSource connection
-  });
-
-  // Handle errors from the EventSource
-  // eslint-disable-next-line no-param-reassign
-  eventSource.onerror = (error) => {
-    controller.error(error); // Pass the error to the stream
-    eventSource.close(); // Close the EventSource connection
-  };
-}
+            
const Agent = require('./agent');
+const { createAgentUrl, setupEventListeners } = require('./agent');
 
 /**
+ * @deprecated This module is deprecated and will be removed in a future version. Use the Agent module instead.
  * Interface to assistant SSE.
  *
  * @module assistant
  * @inner
  * @returns {object}
  */
-class Assistant {
-  constructor(options) {
-    this.options = options || {};
-  }
-
-  static EventTypes = {
-    START: 'start', // Denotes the start of the stream
-    GROUP: 'group', // Represents a semantic grouping of search results, optionally having textual explanation
-    SEARCH_RESULT: 'search_result', // Represents a set of results with metadata (used to show results with search refinements)
-    ARTICLE_REFERENCE: 'article_reference', // Represents a set of content with metadata
-    RECIPE_INFO: 'recipe_info', // Represents recipes' auxiliary information like cooking times & serving sizes
-    RECIPE_INSTRUCTIONS: 'recipe_instructions', // Represents recipe instructions
-    SERVER_ERROR: 'server_error', // Server Error event
-    IMAGE_META: 'image_meta', // This event type is used for enhancing recommendations with media content such as images
-    END: 'end', // Represents the end of data stream
-  };
+class Assistant extends Agent {
+  EventTypes = Agent.EventTypes;
 
   /**
-   * Retrieve assistant results from EventStream
-   *
-   * @function getAssistantResultsStream
-   * @description Retrieve a stream of assistant results from Constructor.io API
-   * @param {string} intent - Intent to use to perform an intent based recommendations
-   * @param {object} [parameters] - Additional parameters to refine result set
-   * @param {string} [parameters.domain] - domain name e.g. swimming sports gear, groceries
-   * @param {number} [parameters.numResultsPerPage] - The total number of results to return
-   * @returns {ReadableStream} Returns a ReadableStream.
-   * @example
-   * const readableStream = constructorio.assistant.getAssistantResultsStream('I want to get shoes', {
-   *     domain: "nike_sportswear",
-   * });
-   * const reader = readableStream.getReader();
-   * const { value, done } = await reader.read();
+   * @deprecated Use getAgentResultsStream from the Agent module instead.
    */
   getAssistantResultsStream(query, parameters) {
-    let eventSource;
-    let readableStream;
-
-    try {
-      const requestUrl = createAssistantUrl(query, parameters, this.options);
-
-      // Create an EventSource that connects to the Server Sent Events API
-      eventSource = new EventSource(requestUrl);
-
-      // Create a readable stream that data will be pushed into
-      readableStream = new ReadableStream({
-        // To be called on stream start
-        start(controller) {
-          // Listen to events emitted from ASA Server Sent Events and push data to the ReadableStream
-          setupEventListeners(eventSource, controller, Assistant.EventTypes);
-        },
-        // To be called on stream cancelling
-        cancel() {
-          // Close the EventSource connection when the stream is prematurely canceled
-          eventSource.close();
-        },
-      });
-    } catch (e) {
-      if (readableStream) {
-        readableStream?.cancel();
-      } else {
-        // If the stream was not successfully created, close the EventSource directly
-        eventSource?.close();
-      }
-
-      throw new Error(e.message);
-    }
-
-    return readableStream;
+    return this.getAgentResultsStream(query, parameters);
   }
 }
 
 module.exports = Assistant;
-module.exports.createAssistantUrl = createAssistantUrl;
+module.exports.createAssistantUrl = createAgentUrl;
 module.exports.setupEventListeners = setupEventListeners;
 
@@ -245,7 +86,7 @@

modules/assistant.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_autocomplete.js.html b/docs/modules_autocomplete.js.html index 94666ed1..7d892a44 100644 --- a/docs/modules_autocomplete.js.html +++ b/docs/modules_autocomplete.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -276,7 +276,7 @@

modules/autocomplete.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_browse.js.html b/docs/modules_browse.js.html index 194b60e3..13321903 100644 --- a/docs/modules_browse.js.html +++ b/docs/modules_browse.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -593,7 +593,7 @@

modules/browse.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_quizzes.js.html b/docs/modules_quizzes.js.html index b3a7971a..52686905 100644 --- a/docs/modules_quizzes.js.html +++ b/docs/modules_quizzes.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -329,7 +329,7 @@

modules/quizzes.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_recommendations.js.html b/docs/modules_recommendations.js.html index 48a33153..04b43ffc 100644 --- a/docs/modules_recommendations.js.html +++ b/docs/modules_recommendations.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -243,7 +243,7 @@

modules/recommendations.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_search.js.html b/docs/modules_search.js.html index 96d3a175..9a07417d 100644 --- a/docs/modules_search.js.html +++ b/docs/modules_search.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -373,7 +373,7 @@

modules/search.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_tracker.js.html b/docs/modules_tracker.js.html index 1adaf1de..e455ccf8 100644 --- a/docs/modules_tracker.js.html +++ b/docs/modules_tracker.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -2452,26 +2452,26 @@

modules/tracker.js

/** * Send ASA request submitted event * - * @function trackAssistantSubmit + * @function trackAgentSubmit * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - Intent of user request * @param {string} [parameters.section] - The section name for the item Ex. "Products" * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} - * @description User submitted an assistant search - * (pressing enter within assistant input element, or clicking assistant submit element) + * @description User submitted an agent search + * (pressing enter within agent input element, or clicking agent submit element) * @example - * constructorio.tracker.trackAssistantSubmit( + * constructorio.tracker.trackAgentSubmit( * { * intent: 'show me a recipe for a cookie', * }, * ); */ - trackAssistantSubmit(parameters, networkParameters = {}) { + trackAgentSubmit(parameters, networkParameters = {}) { if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { // Ensure parameters are provided (required) - const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/assistant_submit?`; + const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/agent_submit?`; const { section, intent, @@ -2503,9 +2503,9 @@

modules/tracker.js

} /** - * Send assistant results page load started + * Send agent results page load started * - * @function trackAssistantResultLoadStarted + * @function trackAgentResultLoadStarted * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - Intent of user request * @param {string} [parameters.section] - The section name for the item Ex. "Products" @@ -2513,19 +2513,19 @@

modules/tracker.js

* @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} - * @description Assistant results page load begun (but has not necessarily loaded completely) + * @description Agent results page load begun (but has not necessarily loaded completely) * @example - * constructorio.tracker.trackAssistantResultLoadStarted( + * constructorio.tracker.trackAgentResultLoadStarted( * { * intent: 'show me a recipe for a cookie', * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', * }, * ); */ - trackAssistantResultLoadStarted(parameters, networkParameters = {}) { + trackAgentResultLoadStarted(parameters, networkParameters = {}) { if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { // Ensure parameters are provided (required) - const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/assistant_result_load_start?`; + const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/agent_result_load_start?`; const { section, intentResultId, @@ -2559,9 +2559,9 @@

modules/tracker.js

} /** - * Send assistant results page load finished + * Send agent results page load finished * - * @function trackAssistantResultLoadFinished + * @function trackAgentResultLoadFinished * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - Intent of user request * @param {number} parameters.searchResultCount - Number of search results loaded @@ -2570,9 +2570,9 @@

modules/tracker.js

* @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} - * @description Assistant results page load finished + * @description Agent results page load finished * @example - * constructorio.tracker.trackAssistantResultLoadFinished( + * constructorio.tracker.trackAgentResultLoadFinished( * { * intent: 'show me a recipe for a cookie', * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', @@ -2580,10 +2580,10 @@

modules/tracker.js

* }, * ); */ - trackAssistantResultLoadFinished(parameters, networkParameters = {}) { + trackAgentResultLoadFinished(parameters, networkParameters = {}) { // Ensure parameters are provided (required) if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { - const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/assistant_result_load_finish?`; + const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/agent_result_load_finish?`; const { section, searchResultCount, @@ -2619,9 +2619,9 @@

modules/tracker.js

} /** - * Send assistant result click event to API + * Send agent result click event to API * - * @function trackAssistantResultClick + * @function trackAgentResultClick * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - intent of the user * @param {string} parameters.searchResultId - result_id of the specific search result the clicked item belongs to @@ -2633,9 +2633,9 @@

modules/tracker.js

* @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} - * @description User clicked a result that appeared within an assistant search result + * @description User clicked a result that appeared within an agent search result * @example - * constructorio.tracker.trackAssistantResultClick( + * constructorio.tracker.trackAgentResultClick( * { * variationId: 'KMH879-7632', * searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2', @@ -2645,10 +2645,10 @@

modules/tracker.js

* }, * ); */ - trackAssistantResultClick(parameters, networkParameters = {}) { + trackAgentResultClick(parameters, networkParameters = {}) { // Ensure parameters are provided (required) if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { - const requestPath = `${this.options.serviceUrl}/v2/behavioral_action/assistant_search_result_click?`; + const requestPath = `${this.options.serviceUrl}/v2/behavioral_action/agent_search_result_click?`; const { section = 'Products', variationId, @@ -2690,9 +2690,9 @@

modules/tracker.js

} /** - * Send assistant search result view event to API + * Send agent search result view event to API * - * @function trackAssistantResultView + * @function trackAgentResultView * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - intent of the user * @param {string} parameters.searchResultId - result_id of the specific search result the clicked item belongs to @@ -2703,9 +2703,9 @@

modules/tracker.js

* @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} - * @description User viewed a search result within an assistant result + * @description User viewed a search result within an agent result * @example - * constructorio.tracker.trackAssistantResultView( + * constructorio.tracker.trackAgentResultView( * { * searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2', * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', @@ -2715,10 +2715,10 @@

modules/tracker.js

* }, * ); */ - trackAssistantResultView(parameters, networkParameters = {}) { + trackAgentResultView(parameters, networkParameters = {}) { // Ensure parameters are provided (required) if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { - const requestPath = `${this.options.serviceUrl}/v2/behavioral_action/assistant_search_result_view?`; + const requestPath = `${this.options.serviceUrl}/v2/behavioral_action/agent_search_result_view?`; const { section = 'Products', items, @@ -2760,7 +2760,7 @@

modules/tracker.js

/** * Send ASA search submitted event * - * @function trackAssistantSearchSubmit + * @function trackAgentSearchSubmit * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - Intent of user request * @param {string} parameters.searchTerm - Term of submitted assistant search event @@ -2770,9 +2770,9 @@

modules/tracker.js

* @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} - * @description User submitted an alternative assistant search result search term + * @description User submitted an alternative agent search result search term * @example - * constructorio.tracker.trackAssistantSearchSubmit({ + * constructorio.tracker.trackAgentSearchSubmit({ * { * searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2', * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', @@ -2781,11 +2781,11 @@

modules/tracker.js

* }, * ); */ - trackAssistantSearchSubmit(parameters, networkParameters = {}) { + trackAgentSearchSubmit(parameters, networkParameters = {}) { // Ensure parameters are provided (required) if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { // Ensure parameters are provided (required) - const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/assistant_search_submit?`; + const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/agent_search_submit?`; const { section, intent, @@ -2823,6 +2823,174 @@

modules/tracker.js

return new Error('parameters is a required parameter of type object'); } + /** + * Send ASA request submitted event + * + * @deprecated This method will be removed in a future version. Use trackAgentSubmit instead. + * @function trackAssistantSubmit + * @param {object} parameters - Additional parameters to be sent with request + * @param {string} parameters.intent - Intent of user request + * @param {string} [parameters.section] - The section name for the item Ex. "Products" + * @param {object} [networkParameters] - Parameters relevant to the network request + * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) + * @returns {(true|Error)} + * @description User submitted an assistant search + * (pressing enter within assistant input element, or clicking assistant submit element) + * @example + * constructorio.tracker.trackAssistantSubmit( + * { + * intent: 'show me a recipe for a cookie', + * }, + * ); + */ + trackAssistantSubmit(parameters, networkParameters = {}) { + return this.trackAgentSubmit(parameters, networkParameters); + } + + /** + * Send assistant results page load started + * + * @deprecated This method will be removed in a future version. Use trackAgentResultLoadStarted instead. + * @function trackAssistantResultLoadStarted + * @param {object} parameters - Additional parameters to be sent with request + * @param {string} parameters.intent - Intent of user request + * @param {string} [parameters.section] - The section name for the item Ex. "Products" + * @param {string} [parameters.intentResultId] - The intent result id from the ASA response + * @param {object} [networkParameters] - Parameters relevant to the network request + * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) + * @returns {(true|Error)} + * @description Assistant results page load begun (but has not necessarily loaded completely) + * @example + * constructorio.tracker.trackAssistantResultLoadStarted( + * { + * intent: 'show me a recipe for a cookie', + * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', + * }, + * ); + */ + trackAssistantResultLoadStarted(parameters, networkParameters = {}) { + return this.trackAgentResultLoadStarted(parameters, networkParameters); + } + + /** + * Send assistant results page load finished + * + * @deprecated This method will be removed in a future version. Use trackAgentResultLoadFinished instead. + * @function trackAssistantResultLoadFinished + * @param {object} parameters - Additional parameters to be sent with request + * @param {string} parameters.intent - Intent of user request + * @param {number} parameters.searchResultCount - Number of search results loaded + * @param {string} [parameters.section] - The section name for the item Ex. "Products" + * @param {string} [parameters.intentResultId] - The intent result id from the ASA response + * @param {object} [networkParameters] - Parameters relevant to the network request + * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) + * @returns {(true|Error)} + * @description Assistant results page load finished + * @example + * constructorio.tracker.trackAssistantResultLoadFinished( + * { + * intent: 'show me a recipe for a cookie', + * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', + * searchResultCount: 5, + * }, + * ); + */ + trackAssistantResultLoadFinished(parameters, networkParameters = {}) { + return this.trackAgentResultLoadFinished(parameters, networkParameters); + } + + /** + * Send assistant result click event to API + * + * @deprecated This method will be removed in a future version. Use trackAgentResultClick instead. + * @function trackAssistantResultClick + * @param {object} parameters - Additional parameters to be sent with request + * @param {string} parameters.intent - intent of the user + * @param {string} parameters.searchResultId - result_id of the specific search result the clicked item belongs to + * @param {string} parameters.itemId - Product item unique identifier + * @param {string} parameters.itemName - Product item name + * @param {string} [parameters.section] - The section name for the item Ex. "Products" + * @param {string} [parameters.variationId] - Product item variation unique identifier + * @param {string} [parameters.intentResultId] - Browse result identifier (returned in response from Constructor) + * @param {object} [networkParameters] - Parameters relevant to the network request + * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) + * @returns {(true|Error)} + * @description User clicked a result that appeared within an assistant search result + * @example + * constructorio.tracker.trackAssistantResultClick( + * { + * variationId: 'KMH879-7632', + * searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2', + * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', + * intent: 'show me a recipe for a cookie', + * itemId: 'KMH876', + * }, + * ); + */ + trackAssistantResultClick(parameters, networkParameters = {}) { + return this.trackAgentResultClick(parameters, networkParameters); + } + + /** + * Send assistant search result view event to API + * + * @deprecated This method will be removed in a future version. Use trackAgentResultView instead. + * @function trackAssistantResultView + * @param {object} parameters - Additional parameters to be sent with request + * @param {string} parameters.intent - intent of the user + * @param {string} parameters.searchResultId - result_id of the specific search result the clicked item belongs to + * @param {number} parameters.numResultsViewed - Number of items viewed in this search result + * @param {object[]} [parameters.items] - List of product item objects viewed + * @param {string} [parameters.section] - The section name for the item Ex. "Products" + * @param {string} [parameters.intentResultId] - Browse result identifier (returned in response from Constructor) + * @param {object} [networkParameters] - Parameters relevant to the network request + * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) + * @returns {(true|Error)} + * @description User viewed a search result within an assistant result + * @example + * constructorio.tracker.trackAssistantResultView( + * { + * searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2', + * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', + * intent: 'show me a recipe for a cookie', + * numResultsViewed: 5, + * items: [{itemId: 'KMH876'}, {itemId: 'KMH140'}, {itemId: 'KMH437'}], + * }, + * ); + */ + trackAssistantResultView(parameters, networkParameters = {}) { + this.trackAgentResultView(parameters, networkParameters); + } + + /** + * Send ASA search submitted event + * + * @deprecated This method will be removed in a future version. Use trackAgentSearchSubmit instead. + * @function trackAssistantSearchSubmit + * @param {object} parameters - Additional parameters to be sent with request + * @param {string} parameters.intent - Intent of user request + * @param {string} parameters.searchTerm - Term of submitted assistant search event + * @param {string} parameters.searchResultId - resultId of search result the clicked item belongs to + * @param {string} [parameters.section] - The section name for the item Ex. "Products" + * @param {string} [parameters.intentResultId] - intentResultId from the ASA response + * @param {object} [networkParameters] - Parameters relevant to the network request + * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) + * @returns {(true|Error)} + * @description User submitted an alternative assistant search result search term + * @example + * constructorio.tracker.trackAssistantSearchSubmit({ + * { + * searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2', + * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', + * intent: 'show me a recipe for a cookie', + * searchTerm: 'flour', + * }, + * ); + */ + trackAssistantSearchSubmit(parameters, networkParameters = {}) { + return this.trackAgentSearchSubmit(parameters, networkParameters); + } + /** * Send product insights agent view events * @@ -3438,7 +3606,7 @@

modules/tracker.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/src/modules/tracker.js b/src/modules/tracker.js index 11d8e0da..888f0a03 100644 --- a/src/modules/tracker.js +++ b/src/modules/tracker.js @@ -2943,45 +2943,45 @@ class Tracker { } /** - * Send product insights agent view events - * - * @function trackProductInsightsAgentViews - * @param {object} parameters - Additional parameters to be sent with request - * @param {array.<{question: string}>} parameters.questions - List of pre-defined questions shown to the user - * @param {string} parameters.itemId - Product id whose page we are on - * @param {string} parameters.itemName - Product name whose page we are on - * @param {array.<{start: string | undefined, - * end: string | undefined}>} parameters.viewTimespans - List of timestamp pairs in ISO_8601 format - * @param {string} [parameters.variationId] - Variation id whose page we are on - * @param {string} [parameters.section] - The section name for the item Ex. "Products" - * @param {object} [networkParameters] - Parameters relevant to the network request - * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) - * @returns {(true|Error)} - * @description The product insights agent element appeared in the visible part of the page - * @example - * constructorio.tracker.trackProductInsightsAgentViews({ - * { - * 'itemId': '1', - * 'itemName': 'item1', - * 'variationId': '2', - * 'questions': [ - * { question: 'Why choose this?' }, - * { question: 'How is this product made?' }, - * { question: 'What are the dimensions of this product?' } - * ], - * 'viewTimespans': [ - * { - * 'start': '2025-05-19T14:30:00+02:00', - * 'end': '2025-05-19T14:30:05+02:00' - * }, - * { - * 'start': '2025-05-19T14:30:10+02:00', - * 'end': '2025-05-19T14:30:15+02:00' - * } - * ] - * }, - * ); - */ + * Send product insights agent view events + * + * @function trackProductInsightsAgentViews + * @param {object} parameters - Additional parameters to be sent with request + * @param {array.<{question: string}>} parameters.questions - List of pre-defined questions shown to the user + * @param {string} parameters.itemId - Product id whose page we are on + * @param {string} parameters.itemName - Product name whose page we are on + * @param {array.<{start: string | undefined, + * end: string | undefined}>} parameters.viewTimespans - List of timestamp pairs in ISO_8601 format + * @param {string} [parameters.variationId] - Variation id whose page we are on + * @param {string} [parameters.section] - The section name for the item Ex. "Products" + * @param {object} [networkParameters] - Parameters relevant to the network request + * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) + * @returns {(true|Error)} + * @description The product insights agent element appeared in the visible part of the page + * @example + * constructorio.tracker.trackProductInsightsAgentViews({ + * { + * 'itemId': '1', + * 'itemName': 'item1', + * 'variationId': '2', + * 'questions': [ + * { question: 'Why choose this?' }, + * { question: 'How is this product made?' }, + * { question: 'What are the dimensions of this product?' } + * ], + * 'viewTimespans': [ + * { + * 'start': '2025-05-19T14:30:00+02:00', + * 'end': '2025-05-19T14:30:05+02:00' + * }, + * { + * 'start': '2025-05-19T14:30:10+02:00', + * 'end': '2025-05-19T14:30:15+02:00' + * } + * ] + * }, + * ); + */ trackProductInsightsAgentViews(parameters, networkParameters = {}) { // Ensure parameters are provided (required) if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { From 2c0e9afc6fac57cb026e52c66f66aca798592a16 Mon Sep 17 00:00:00 2001 From: Evan Yan Date: Tue, 15 Jul 2025 16:17:19 -0700 Subject: [PATCH 06/14] Update types in constructorio --- docs/ConstructorIO.html | 2 +- docs/constructorio.js.html | 2 +- docs/index.html | 2 +- docs/module-agent.html | 2 +- docs/module-assistant.html | 2 +- docs/module-autocomplete.html | 2 +- docs/module-browse.html | 2 +- docs/module-quizzes.html | 2 +- docs/module-recommendations.html | 2 +- docs/module-search.html | 2 +- docs/module-tracker.html | 4 ++-- docs/modules_agent.js.html | 2 +- docs/modules_assistant.js.html | 2 +- docs/modules_autocomplete.js.html | 2 +- docs/modules_browse.js.html | 2 +- docs/modules_quizzes.js.html | 2 +- docs/modules_recommendations.js.html | 2 +- docs/modules_search.js.html | 2 +- docs/modules_tracker.js.html | 4 ++-- src/modules/tracker.js | 2 +- src/types/constructorio.d.ts | 5 ++++- 21 files changed, 26 insertions(+), 23 deletions(-) diff --git a/docs/ConstructorIO.html b/docs/ConstructorIO.html index 541f75d8..f83177b0 100644 --- a/docs/ConstructorIO.html +++ b/docs/ConstructorIO.html @@ -1671,7 +1671,7 @@
Properties

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/constructorio.js.html b/docs/constructorio.js.html index 98f215e4..5d9fda6f 100644 --- a/docs/constructorio.js.html +++ b/docs/constructorio.js.html @@ -260,7 +260,7 @@

constructorio.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/index.html b/docs/index.html index 5010da4c..d63cec3c 100644 --- a/docs/index.html +++ b/docs/index.html @@ -114,7 +114,7 @@

Development / npm commands


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-agent.html b/docs/module-agent.html index faaea44b..7b940a89 100644 --- a/docs/module-agent.html +++ b/docs/module-agent.html @@ -459,7 +459,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-assistant.html b/docs/module-assistant.html index f66379ac..000037e8 100644 --- a/docs/module-assistant.html +++ b/docs/module-assistant.html @@ -144,7 +144,7 @@

assistant


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-autocomplete.html b/docs/module-autocomplete.html index 49d54de6..f6c210a4 100644 --- a/docs/module-autocomplete.html +++ b/docs/module-autocomplete.html @@ -798,7 +798,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-browse.html b/docs/module-browse.html index ddd759fd..bc16b747 100644 --- a/docs/module-browse.html +++ b/docs/module-browse.html @@ -3216,7 +3216,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-quizzes.html b/docs/module-quizzes.html index 649f4f20..2b8a57a2 100644 --- a/docs/module-quizzes.html +++ b/docs/module-quizzes.html @@ -1661,7 +1661,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-recommendations.html b/docs/module-recommendations.html index 732f8a92..d6d5141e 100644 --- a/docs/module-recommendations.html +++ b/docs/module-recommendations.html @@ -798,7 +798,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-search.html b/docs/module-search.html index 9df181fc..c6774932 100644 --- a/docs/module-search.html +++ b/docs/module-search.html @@ -1750,7 +1750,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-tracker.html b/docs/module-tracker.html index 0b257a10..a26b01f3 100644 --- a/docs/module-tracker.html +++ b/docs/module-tracker.html @@ -2406,7 +2406,7 @@
Properties
- Term of submitted assistant search event + Term of submitted agent search event @@ -19733,7 +19733,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_agent.js.html b/docs/modules_agent.js.html index eee9ad2a..aec56cda 100644 --- a/docs/modules_agent.js.html +++ b/docs/modules_agent.js.html @@ -248,7 +248,7 @@

modules/agent.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_assistant.js.html b/docs/modules_assistant.js.html index 50eaeff4..2a153ace 100644 --- a/docs/modules_assistant.js.html +++ b/docs/modules_assistant.js.html @@ -86,7 +86,7 @@

modules/assistant.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_autocomplete.js.html b/docs/modules_autocomplete.js.html index 7d892a44..af1d9ad2 100644 --- a/docs/modules_autocomplete.js.html +++ b/docs/modules_autocomplete.js.html @@ -276,7 +276,7 @@

modules/autocomplete.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_browse.js.html b/docs/modules_browse.js.html index 13321903..9262f9cf 100644 --- a/docs/modules_browse.js.html +++ b/docs/modules_browse.js.html @@ -593,7 +593,7 @@

modules/browse.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_quizzes.js.html b/docs/modules_quizzes.js.html index 52686905..db560775 100644 --- a/docs/modules_quizzes.js.html +++ b/docs/modules_quizzes.js.html @@ -329,7 +329,7 @@

modules/quizzes.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_recommendations.js.html b/docs/modules_recommendations.js.html index 04b43ffc..813e99d0 100644 --- a/docs/modules_recommendations.js.html +++ b/docs/modules_recommendations.js.html @@ -243,7 +243,7 @@

modules/recommendations.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_search.js.html b/docs/modules_search.js.html index 9a07417d..858cfa5c 100644 --- a/docs/modules_search.js.html +++ b/docs/modules_search.js.html @@ -373,7 +373,7 @@

modules/search.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_tracker.js.html b/docs/modules_tracker.js.html index e455ccf8..8aecc751 100644 --- a/docs/modules_tracker.js.html +++ b/docs/modules_tracker.js.html @@ -2763,7 +2763,7 @@

modules/tracker.js

* @function trackAgentSearchSubmit * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - Intent of user request - * @param {string} parameters.searchTerm - Term of submitted assistant search event + * @param {string} parameters.searchTerm - Term of submitted agent search event * @param {string} parameters.searchResultId - resultId of search result the clicked item belongs to * @param {string} [parameters.section] - The section name for the item Ex. "Products" * @param {string} [parameters.intentResultId] - intentResultId from the ASA response @@ -3606,7 +3606,7 @@

modules/tracker.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:08:40 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/src/modules/tracker.js b/src/modules/tracker.js index 888f0a03..29a06e4c 100644 --- a/src/modules/tracker.js +++ b/src/modules/tracker.js @@ -2714,7 +2714,7 @@ class Tracker { * @function trackAgentSearchSubmit * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - Intent of user request - * @param {string} parameters.searchTerm - Term of submitted assistant search event + * @param {string} parameters.searchTerm - Term of submitted agent search event * @param {string} parameters.searchResultId - resultId of search result the clicked item belongs to * @param {string} [parameters.section] - The section name for the item Ex. "Products" * @param {string} [parameters.intentResultId] - intentResultId from the ASA response diff --git a/src/types/constructorio.d.ts b/src/types/constructorio.d.ts index 17f9896a..f68df4af 100644 --- a/src/types/constructorio.d.ts +++ b/src/types/constructorio.d.ts @@ -3,6 +3,7 @@ import Browse from './browse'; import Autocomplete from './autocomplete'; import Recommendations from './recommendations'; import Quizzes from './quizzes'; +import Agent from './agent'; import Assistant from './assistant'; import Tracker from './tracker'; import { ConstructorClientOptions } from '.'; @@ -24,6 +25,8 @@ declare class ConstructorIO { quizzes: Quizzes; + agent: Agent; + assistant: Assistant; tracker: Tracker; @@ -32,5 +35,5 @@ declare class ConstructorIO { } declare namespace ConstructorIO { - export { Search, Browse, Autocomplete, Recommendations, Quizzes, Tracker, Assistant }; + export { Search, Browse, Autocomplete, Recommendations, Quizzes, Tracker, Agent, Assistant }; } From e981519842e29686045064a2a49b328dcabc11f2 Mon Sep 17 00:00:00 2001 From: Evan Yan Date: Wed, 16 Jul 2025 20:42:31 -0700 Subject: [PATCH 07/14] Retain assistant method doc --- docs/ConstructorIO.html | 4 +- docs/constructorio.js.html | 4 +- docs/index.html | 4 +- docs/module-agent.html | 4 +- docs/module-assistant.html | 317 ++++++++++++++++++++++++++- docs/module-autocomplete.html | 4 +- docs/module-browse.html | 4 +- docs/module-quizzes.html | 4 +- docs/module-recommendations.html | 4 +- docs/module-search.html | 4 +- docs/module-tracker.html | 4 +- docs/modules_agent.js.html | 4 +- docs/modules_assistant.js.html | 21 +- docs/modules_autocomplete.js.html | 4 +- docs/modules_browse.js.html | 4 +- docs/modules_quizzes.js.html | 4 +- docs/modules_recommendations.js.html | 4 +- docs/modules_search.js.html | 4 +- docs/modules_tracker.js.html | 4 +- src/modules/assistant.js | 17 +- 20 files changed, 383 insertions(+), 40 deletions(-) diff --git a/docs/ConstructorIO.html b/docs/ConstructorIO.html index f83177b0..9d34d708 100644 --- a/docs/ConstructorIO.html +++ b/docs/ConstructorIO.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -1671,7 +1671,7 @@
Properties

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/constructorio.js.html b/docs/constructorio.js.html index 5d9fda6f..8de1c35d 100644 --- a/docs/constructorio.js.html +++ b/docs/constructorio.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -260,7 +260,7 @@

constructorio.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/index.html b/docs/index.html index d63cec3c..d24aeb58 100644 --- a/docs/index.html +++ b/docs/index.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -114,7 +114,7 @@

Development / npm commands


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-agent.html b/docs/module-agent.html index 7b940a89..60e203cd 100644 --- a/docs/module-agent.html +++ b/docs/module-agent.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -459,7 +459,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-assistant.html b/docs/module-assistant.html index 000037e8..28f12480 100644 --- a/docs/module-assistant.html +++ b/docs/module-assistant.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -126,6 +126,319 @@

assistant

+

Methods

+ + + + + + +

(inner) getAssistantResultsStream(intent, parametersopt) → {ReadableStream}

+ + + + + + +
+ +
Description:
+
  • Retrieve a stream of assistant results from Constructor.io API
+ + + +
Source:
+
+ + + + + + + + + + + + + + + + + +
Deprecated:
  • Use getAssistantResultsStream from the Agent module instead.
+ + + + + + + + + + + + + + + +
+ + + + + + + + + + + +
Example
+ +
const readableStream = constructorio.agent.getAssistantResultsStream('I want to get shoes', {
+    domain: "nike_sportswear",
+});
+const reader = readableStream.getReader();
+const { value, done } = await reader.read();
+ + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
intent + + +string + + + + + + + + + + + Intent to use to perform an intent based recommendations
parameters + + +object + + + + + + + <optional>
+ + + + + +
Additional parameters to refine result set +
Properties
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
domain + + +string + + + + + + + <optional>
+ + + + + +
domain name e.g. swimming sports gear, groceries
numResultsPerPage + + +number + + + + + + + <optional>
+ + + + + +
The total number of results to return
+ +
+ + + + + + + + + + + + + + + + +
Returns:
+ + +
+ Returns a ReadableStream. +
+ + + +
+
+ Type +
+
+ +ReadableStream + + + +
+
+ + + + + + + @@ -144,7 +457,7 @@

assistant


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-autocomplete.html b/docs/module-autocomplete.html index f6c210a4..9174d8e5 100644 --- a/docs/module-autocomplete.html +++ b/docs/module-autocomplete.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -798,7 +798,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-browse.html b/docs/module-browse.html index bc16b747..5c48fad6 100644 --- a/docs/module-browse.html +++ b/docs/module-browse.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -3216,7 +3216,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-quizzes.html b/docs/module-quizzes.html index 2b8a57a2..0dccf181 100644 --- a/docs/module-quizzes.html +++ b/docs/module-quizzes.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -1661,7 +1661,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-recommendations.html b/docs/module-recommendations.html index d6d5141e..23f81ca4 100644 --- a/docs/module-recommendations.html +++ b/docs/module-recommendations.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -798,7 +798,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-search.html b/docs/module-search.html index c6774932..edbc0c00 100644 --- a/docs/module-search.html +++ b/docs/module-search.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -1750,7 +1750,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-tracker.html b/docs/module-tracker.html index a26b01f3..02c7bbd2 100644 --- a/docs/module-tracker.html +++ b/docs/module-tracker.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -19733,7 +19733,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_agent.js.html b/docs/modules_agent.js.html index aec56cda..2c0befca 100644 --- a/docs/modules_agent.js.html +++ b/docs/modules_agent.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -248,7 +248,7 @@

modules/agent.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_assistant.js.html b/docs/modules_assistant.js.html index 2a153ace..783abf68 100644 --- a/docs/modules_assistant.js.html +++ b/docs/modules_assistant.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -62,7 +62,22 @@

modules/assistant.js

EventTypes = Agent.EventTypes; /** - * @deprecated Use getAgentResultsStream from the Agent module instead. + * Retrieve assistant results from EventStream + * + * @deprecated Use getAssistantResultsStream from the Agent module instead. + * @function getAssistantResultsStream + * @description Retrieve a stream of assistant results from Constructor.io API + * @param {string} intent - Intent to use to perform an intent based recommendations + * @param {object} [parameters] - Additional parameters to refine result set + * @param {string} [parameters.domain] - domain name e.g. swimming sports gear, groceries + * @param {number} [parameters.numResultsPerPage] - The total number of results to return + * @returns {ReadableStream} Returns a ReadableStream. + * @example + * const readableStream = constructorio.agent.getAssistantResultsStream('I want to get shoes', { + * domain: "nike_sportswear", + * }); + * const reader = readableStream.getReader(); + * const { value, done } = await reader.read(); */ getAssistantResultsStream(query, parameters) { return this.getAgentResultsStream(query, parameters); @@ -86,7 +101,7 @@

modules/assistant.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_autocomplete.js.html b/docs/modules_autocomplete.js.html index af1d9ad2..51a4ebe0 100644 --- a/docs/modules_autocomplete.js.html +++ b/docs/modules_autocomplete.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -276,7 +276,7 @@

modules/autocomplete.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_browse.js.html b/docs/modules_browse.js.html index 9262f9cf..a1fccdb1 100644 --- a/docs/modules_browse.js.html +++ b/docs/modules_browse.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -593,7 +593,7 @@

modules/browse.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_quizzes.js.html b/docs/modules_quizzes.js.html index db560775..c2fac024 100644 --- a/docs/modules_quizzes.js.html +++ b/docs/modules_quizzes.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -329,7 +329,7 @@

modules/quizzes.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_recommendations.js.html b/docs/modules_recommendations.js.html index 813e99d0..48ebe3c8 100644 --- a/docs/modules_recommendations.js.html +++ b/docs/modules_recommendations.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -243,7 +243,7 @@

modules/recommendations.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_search.js.html b/docs/modules_search.js.html index 858cfa5c..c7a5b654 100644 --- a/docs/modules_search.js.html +++ b/docs/modules_search.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -373,7 +373,7 @@

modules/search.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_tracker.js.html b/docs/modules_tracker.js.html index 8aecc751..2861de4f 100644 --- a/docs/modules_tracker.js.html +++ b/docs/modules_tracker.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -3606,7 +3606,7 @@

modules/tracker.js


- Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 16:16:23 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/src/modules/assistant.js b/src/modules/assistant.js index 3661e62b..c9278067 100644 --- a/src/modules/assistant.js +++ b/src/modules/assistant.js @@ -13,7 +13,22 @@ class Assistant extends Agent { EventTypes = Agent.EventTypes; /** - * @deprecated Use getAgentResultsStream from the Agent module instead. + * Retrieve assistant results from EventStream + * + * @deprecated Use getAssistantResultsStream from the Agent module instead. + * @function getAssistantResultsStream + * @description Retrieve a stream of assistant results from Constructor.io API + * @param {string} intent - Intent to use to perform an intent based recommendations + * @param {object} [parameters] - Additional parameters to refine result set + * @param {string} [parameters.domain] - domain name e.g. swimming sports gear, groceries + * @param {number} [parameters.numResultsPerPage] - The total number of results to return + * @returns {ReadableStream} Returns a ReadableStream. + * @example + * const readableStream = constructorio.agent.getAssistantResultsStream('I want to get shoes', { + * domain: "nike_sportswear", + * }); + * const reader = readableStream.getReader(); + * const { value, done } = await reader.read(); */ getAssistantResultsStream(query, parameters) { return this.getAgentResultsStream(query, parameters); From 761f97685dc9c216998fd597b95bb038ff1e806f Mon Sep 17 00:00:00 2001 From: Evan Yan Date: Wed, 16 Jul 2025 20:45:49 -0700 Subject: [PATCH 08/14] Update doc in assistant module --- docs/ConstructorIO.html | 2 +- docs/constructorio.js.html | 2 +- docs/index.html | 2 +- docs/module-agent.html | 2 +- docs/module-assistant.html | 4 ++-- docs/module-autocomplete.html | 2 +- docs/module-browse.html | 2 +- docs/module-quizzes.html | 2 +- docs/module-recommendations.html | 2 +- docs/module-search.html | 2 +- docs/module-tracker.html | 2 +- docs/modules_agent.js.html | 2 +- docs/modules_assistant.js.html | 4 ++-- docs/modules_autocomplete.js.html | 2 +- docs/modules_browse.js.html | 2 +- docs/modules_quizzes.js.html | 2 +- docs/modules_recommendations.js.html | 2 +- docs/modules_search.js.html | 2 +- docs/modules_tracker.js.html | 2 +- src/modules/assistant.js | 2 +- 20 files changed, 22 insertions(+), 22 deletions(-) diff --git a/docs/ConstructorIO.html b/docs/ConstructorIO.html index 9d34d708..47d0621d 100644 --- a/docs/ConstructorIO.html +++ b/docs/ConstructorIO.html @@ -1671,7 +1671,7 @@
Properties

- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/constructorio.js.html b/docs/constructorio.js.html index 8de1c35d..5c804647 100644 --- a/docs/constructorio.js.html +++ b/docs/constructorio.js.html @@ -260,7 +260,7 @@

constructorio.js


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/index.html b/docs/index.html index d24aeb58..e8d1339b 100644 --- a/docs/index.html +++ b/docs/index.html @@ -114,7 +114,7 @@

Development / npm commands


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-agent.html b/docs/module-agent.html index 60e203cd..dbe8dc7b 100644 --- a/docs/module-agent.html +++ b/docs/module-agent.html @@ -459,7 +459,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-assistant.html b/docs/module-assistant.html index 28f12480..ddf4a252 100644 --- a/docs/module-assistant.html +++ b/docs/module-assistant.html @@ -198,7 +198,7 @@

Example

-
const readableStream = constructorio.agent.getAssistantResultsStream('I want to get shoes', {
+    
const readableStream = constructorio.assistant.getAssistantResultsStream('I want to get shoes', {
     domain: "nike_sportswear",
 });
 const reader = readableStream.getReader();
@@ -457,7 +457,7 @@ 
Returns:

- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-autocomplete.html b/docs/module-autocomplete.html index 9174d8e5..87eb7f18 100644 --- a/docs/module-autocomplete.html +++ b/docs/module-autocomplete.html @@ -798,7 +798,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-browse.html b/docs/module-browse.html index 5c48fad6..2ad7367b 100644 --- a/docs/module-browse.html +++ b/docs/module-browse.html @@ -3216,7 +3216,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-quizzes.html b/docs/module-quizzes.html index 0dccf181..8e45851b 100644 --- a/docs/module-quizzes.html +++ b/docs/module-quizzes.html @@ -1661,7 +1661,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-recommendations.html b/docs/module-recommendations.html index 23f81ca4..b45756db 100644 --- a/docs/module-recommendations.html +++ b/docs/module-recommendations.html @@ -798,7 +798,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-search.html b/docs/module-search.html index edbc0c00..5fc8f644 100644 --- a/docs/module-search.html +++ b/docs/module-search.html @@ -1750,7 +1750,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-tracker.html b/docs/module-tracker.html index 02c7bbd2..835af02e 100644 --- a/docs/module-tracker.html +++ b/docs/module-tracker.html @@ -19733,7 +19733,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_agent.js.html b/docs/modules_agent.js.html index 2c0befca..dc7047b8 100644 --- a/docs/modules_agent.js.html +++ b/docs/modules_agent.js.html @@ -248,7 +248,7 @@

modules/agent.js


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_assistant.js.html b/docs/modules_assistant.js.html index 783abf68..ebe12d31 100644 --- a/docs/modules_assistant.js.html +++ b/docs/modules_assistant.js.html @@ -73,7 +73,7 @@

modules/assistant.js

* @param {number} [parameters.numResultsPerPage] - The total number of results to return * @returns {ReadableStream} Returns a ReadableStream. * @example - * const readableStream = constructorio.agent.getAssistantResultsStream('I want to get shoes', { + * const readableStream = constructorio.assistant.getAssistantResultsStream('I want to get shoes', { * domain: "nike_sportswear", * }); * const reader = readableStream.getReader(); @@ -101,7 +101,7 @@

modules/assistant.js


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_autocomplete.js.html b/docs/modules_autocomplete.js.html index 51a4ebe0..d95a2165 100644 --- a/docs/modules_autocomplete.js.html +++ b/docs/modules_autocomplete.js.html @@ -276,7 +276,7 @@

modules/autocomplete.js


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_browse.js.html b/docs/modules_browse.js.html index a1fccdb1..7b89c7cd 100644 --- a/docs/modules_browse.js.html +++ b/docs/modules_browse.js.html @@ -593,7 +593,7 @@

modules/browse.js


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_quizzes.js.html b/docs/modules_quizzes.js.html index c2fac024..7fd8c3d9 100644 --- a/docs/modules_quizzes.js.html +++ b/docs/modules_quizzes.js.html @@ -329,7 +329,7 @@

modules/quizzes.js


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_recommendations.js.html b/docs/modules_recommendations.js.html index 48ebe3c8..fed1a4fb 100644 --- a/docs/modules_recommendations.js.html +++ b/docs/modules_recommendations.js.html @@ -243,7 +243,7 @@

modules/recommendations.js


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_search.js.html b/docs/modules_search.js.html index c7a5b654..12db8289 100644 --- a/docs/modules_search.js.html +++ b/docs/modules_search.js.html @@ -373,7 +373,7 @@

modules/search.js


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_tracker.js.html b/docs/modules_tracker.js.html index 2861de4f..58f45e3c 100644 --- a/docs/modules_tracker.js.html +++ b/docs/modules_tracker.js.html @@ -3606,7 +3606,7 @@

modules/tracker.js


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:41:18 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/src/modules/assistant.js b/src/modules/assistant.js index c9278067..49821d4a 100644 --- a/src/modules/assistant.js +++ b/src/modules/assistant.js @@ -24,7 +24,7 @@ class Assistant extends Agent { * @param {number} [parameters.numResultsPerPage] - The total number of results to return * @returns {ReadableStream} Returns a ReadableStream. * @example - * const readableStream = constructorio.agent.getAssistantResultsStream('I want to get shoes', { + * const readableStream = constructorio.assistant.getAssistantResultsStream('I want to get shoes', { * domain: "nike_sportswear", * }); * const reader = readableStream.getReader(); From a1965471e3614b987e5a3bed006369013b13c750 Mon Sep 17 00:00:00 2001 From: Evan Yan Date: Thu, 17 Jul 2025 11:09:32 -0700 Subject: [PATCH 09/14] Revert changes for docs --- docs/ConstructorIO.html | 74 +- docs/constructorio.js.html | 12 +- docs/index.html | 4 +- docs/module-assistant.html | 18 +- docs/module-autocomplete.html | 4 +- docs/module-browse.html | 4 +- docs/module-quizzes.html | 4 +- docs/module-recommendations.html | 4 +- docs/module-search.html | 4 +- docs/module-tracker.html | 2751 +------------------------- docs/modules_assistant.js.html | 164 +- docs/modules_autocomplete.js.html | 4 +- docs/modules_browse.js.html | 4 +- docs/modules_quizzes.js.html | 4 +- docs/modules_recommendations.js.html | 4 +- docs/modules_search.js.html | 4 +- docs/modules_tracker.js.html | 244 +-- 17 files changed, 248 insertions(+), 3059 deletions(-) diff --git a/docs/ConstructorIO.html b/docs/ConstructorIO.html index 47d0621d..dde9cab5 100644 --- a/docs/ConstructorIO.html +++ b/docs/ConstructorIO.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -83,7 +83,7 @@

new Cons
Source:
@@ -288,30 +288,6 @@

Properties:
- - - agent - - - - - -object - - - - - - - - - - - Interface to module:agent - - - - assistant @@ -537,46 +513,6 @@
Properties
- - - agentServiceUrl - - - - - -string - - - - - - - - - - <optional>
- - - - - - - - - - - - 'https://agent.cnstrc.com' - - - - - AI Shopping Agent API URL endpoint - - - - assistantServiceUrl @@ -612,7 +548,7 @@
Properties
- AI Shopping Assistant API URL endpoint + AI Assistant API URL endpoint @@ -1339,7 +1275,7 @@

setCl
Source:
@@ -1671,7 +1607,7 @@

Properties

- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/constructorio.js.html b/docs/constructorio.js.html index 5c804647..84d7544c 100644 --- a/docs/constructorio.js.html +++ b/docs/constructorio.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -60,7 +60,6 @@

constructorio.js

const helpers = require('./utils/helpers'); const { default: packageVersion } = require('./version'); const Quizzes = require('./modules/quizzes'); -const Agent = require('./modules/agent'); const Assistant = require('./modules/assistant'); // Compute package version string @@ -88,8 +87,7 @@

constructorio.js

* @param {string} parameters.apiKey - Constructor.io API key * @param {string} [parameters.serviceUrl='https://ac.cnstrc.com'] - API URL endpoint * @param {string} [parameters.quizzesServiceUrl='https://quizzes.cnstrc.com'] - Quizzes API URL endpoint - * @param {string} [parameters.agentServiceUrl='https://agent.cnstrc.com'] - AI Shopping Agent API URL endpoint - * @param {string} [parameters.assistantServiceUrl='https://assistant.cnstrc.com'] - AI Shopping Assistant API URL endpoint + * @param {string} [parameters.assistantServiceUrl='https://assistant.cnstrc.com'] - AI Assistant API URL endpoint * @param {array} [parameters.segments] - User segments * @param {object} [parameters.testCells] - User test cells * @param {string} [parameters.clientId] - Client ID, defaults to value supplied by 'constructorio-id' module @@ -111,7 +109,6 @@

constructorio.js

* @property {object} recommendations - Interface to {@link module:recommendations} * @property {object} tracker - Interface to {@link module:tracker} * @property {object} quizzes - Interface to {@link module:quizzes} - * @property {object} agent - Interface to {@link module:agent} * @property {object} assistant - Interface to {@link module:assistant} * @returns {class} */ @@ -121,7 +118,6 @@

constructorio.js

version: versionFromOptions, serviceUrl, quizzesServiceUrl, - agentServiceUrl, assistantServiceUrl, segments, testCells, @@ -168,7 +164,6 @@

constructorio.js

version: versionFromOptions || versionFromGlobal || computePackageVersion(), serviceUrl: helpers.addHTTPSToString(normalizedServiceUrl) || 'https://ac.cnstrc.com', quizzesServiceUrl: (quizzesServiceUrl && quizzesServiceUrl.replace(/\/$/, '')) || 'https://quizzes.cnstrc.com', - agentServiceUrl: (agentServiceUrl && agentServiceUrl.replace(/\/$/, '')) || 'https://agent.cnstrc.com', assistantServiceUrl: (assistantServiceUrl && assistantServiceUrl.replace(/\/$/, '')) || 'https://assistant.cnstrc.com', sessionId: sessionId || session_id, clientId: clientId || client_id, @@ -191,7 +186,6 @@

constructorio.js

this.recommendations = new Recommendations(this.options); this.tracker = new Tracker(this.options); this.quizzes = new Quizzes(this.options); - this.agent = new Agent(this.options); this.assistant = new Assistant(this.options); // Dispatch initialization event @@ -260,7 +254,7 @@

constructorio.js


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/index.html b/docs/index.html index e8d1339b..4ba851c0 100644 --- a/docs/index.html +++ b/docs/index.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -114,7 +114,7 @@

Development / npm commands


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-assistant.html b/docs/module-assistant.html index ddf4a252..78d4d05c 100644 --- a/docs/module-assistant.html +++ b/docs/module-assistant.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -59,11 +59,14 @@

assistant

+
Description:
+
  • Interface to assistant SSE.
+
Source:
@@ -82,9 +85,6 @@

assistant

-
Deprecated:
  • This module is deprecated and will be removed in a future version. Use the Agent module instead. -Interface to assistant SSE.
- @@ -105,6 +105,8 @@

assistant

+
Interface to assistant SSE.
+ @@ -149,7 +151,7 @@

Source:
@@ -168,8 +170,6 @@

Deprecated:
  • Use getAssistantResultsStream from the Agent module instead.
- @@ -457,7 +457,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-autocomplete.html b/docs/module-autocomplete.html index 87eb7f18..de449096 100644 --- a/docs/module-autocomplete.html +++ b/docs/module-autocomplete.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -798,7 +798,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-browse.html b/docs/module-browse.html index 2ad7367b..1dffda0e 100644 --- a/docs/module-browse.html +++ b/docs/module-browse.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -3216,7 +3216,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-quizzes.html b/docs/module-quizzes.html index 8e45851b..54fab57e 100644 --- a/docs/module-quizzes.html +++ b/docs/module-quizzes.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -1661,7 +1661,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-recommendations.html b/docs/module-recommendations.html index b45756db..def47d89 100644 --- a/docs/module-recommendations.html +++ b/docs/module-recommendations.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -798,7 +798,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-search.html b/docs/module-search.html index 5fc8f644..e4459399 100644 --- a/docs/module-search.html +++ b/docs/module-search.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -1750,7 +1750,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/module-tracker.html b/docs/module-tracker.html index 835af02e..02926176 100644 --- a/docs/module-tracker.html +++ b/docs/module-tracker.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -151,7 +151,7 @@

(inn
Source:
@@ -293,2711 +293,6 @@
Parameters:
-
Returns:
- - - - -
-
- Type -
-
- -true -| - -Error - - - -
-
- - - - - - - - - - -

(inner) trackAgentResultClick(parameters, networkParametersopt) → {true|Error}

- - - - - - -
- -
Description:
-
  • User clicked a result that appeared within an agent search result
- - - -
Source:
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
Example
- -
constructorio.tracker.trackAgentResultClick(
-    {
-        variationId: 'KMH879-7632',
-        searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2',
-        intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2',
-        intent: 'show me a recipe for a cookie',
-        itemId: 'KMH876',
-    },
-);
- - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
parameters - - -object - - - - - - - - - - - Additional parameters to be sent with request -
Properties
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
intent - - -string - - - - - - - - - - - intent of the user
searchResultId - - -string - - - - - - - - - - - result_id of the specific search result the clicked item belongs to
itemId - - -string - - - - - - - - - - - Product item unique identifier
itemName - - -string - - - - - - - - - - - Product item name
section - - -string - - - - - - - <optional>
- - - - - -
The section name for the item Ex. "Products"
variationId - - -string - - - - - - - <optional>
- - - - - -
Product item variation unique identifier
intentResultId - - -string - - - - - - - <optional>
- - - - - -
Browse result identifier (returned in response from Constructor)
- -
networkParameters - - -object - - - - - - - <optional>
- - - - - -
Parameters relevant to the network request -
Properties
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
timeout - - -number - - - - - - - <optional>
- - - - - -
Request timeout (in milliseconds)
- -
- - - - - - - - - - - - - - - - -
Returns:
- - - - -
-
- Type -
-
- -true -| - -Error - - - -
-
- - - - - - - - - - -

(inner) trackAgentResultLoadFinished(parameters, networkParametersopt) → {true|Error}

- - - - - - -
- -
Description:
-
  • Agent results page load finished
- - - -
Source:
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
Example
- -
constructorio.tracker.trackAgentResultLoadFinished(
-    {
-        intent: 'show me a recipe for a cookie',
-        intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2',
-        searchResultCount: 5,
-    },
-);
- - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
parameters - - -object - - - - - - - - - - - Additional parameters to be sent with request -
Properties
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
intent - - -string - - - - - - - - - - - Intent of user request
searchResultCount - - -number - - - - - - - - - - - Number of search results loaded
section - - -string - - - - - - - <optional>
- - - - - -
The section name for the item Ex. "Products"
intentResultId - - -string - - - - - - - <optional>
- - - - - -
The intent result id from the ASA response
- -
networkParameters - - -object - - - - - - - <optional>
- - - - - -
Parameters relevant to the network request -
Properties
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
timeout - - -number - - - - - - - <optional>
- - - - - -
Request timeout (in milliseconds)
- -
- - - - - - - - - - - - - - - - -
Returns:
- - - - -
-
- Type -
-
- -true -| - -Error - - - -
-
- - - - - - - - - - -

(inner) trackAgentResultLoadStarted(parameters, networkParametersopt) → {true|Error}

- - - - - - -
- -
Description:
-
  • Agent results page load begun (but has not necessarily loaded completely)
- - - -
Source:
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
Example
- -
constructorio.tracker.trackAgentResultLoadStarted(
-    {
-        intent: 'show me a recipe for a cookie',
-        intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2',
-    },
-);
- - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
parameters - - -object - - - - - - - - - - - Additional parameters to be sent with request -
Properties
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
intent - - -string - - - - - - - - - - - Intent of user request
section - - -string - - - - - - - <optional>
- - - - - -
The section name for the item Ex. "Products"
intentResultId - - -string - - - - - - - <optional>
- - - - - -
The intent result id from the ASA response
- -
networkParameters - - -object - - - - - - - <optional>
- - - - - -
Parameters relevant to the network request -
Properties
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
timeout - - -number - - - - - - - <optional>
- - - - - -
Request timeout (in milliseconds)
- -
- - - - - - - - - - - - - - - - -
Returns:
- - - - -
-
- Type -
-
- -true -| - -Error - - - -
-
- - - - - - - - - - -

(inner) trackAgentResultView(parameters, networkParametersopt) → {true|Error}

- - - - - - -
- -
Description:
-
  • User viewed a search result within an agent result
- - - -
Source:
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
Example
- -
constructorio.tracker.trackAgentResultView(
-    {
-        searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2',
-        intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2',
-        intent: 'show me a recipe for a cookie',
-        numResultsViewed: 5,
-        items: [{itemId: 'KMH876'}, {itemId: 'KMH140'}, {itemId: 'KMH437'}],
-    },
-);
- - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
parameters - - -object - - - - - - - - - - - Additional parameters to be sent with request -
Properties
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
intent - - -string - - - - - - - - - - - intent of the user
searchResultId - - -string - - - - - - - - - - - result_id of the specific search result the clicked item belongs to
numResultsViewed - - -number - - - - - - - - - - - Number of items viewed in this search result
items - - -Array.<object> - - - - - - - <optional>
- - - - - -
List of product item objects viewed
section - - -string - - - - - - - <optional>
- - - - - -
The section name for the item Ex. "Products"
intentResultId - - -string - - - - - - - <optional>
- - - - - -
Browse result identifier (returned in response from Constructor)
- -
networkParameters - - -object - - - - - - - <optional>
- - - - - -
Parameters relevant to the network request -
Properties
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
timeout - - -number - - - - - - - <optional>
- - - - - -
Request timeout (in milliseconds)
- -
- - - - - - - - - - - - - - - - -
Returns:
- - - - -
-
- Type -
-
- -true -| - -Error - - - -
-
- - - - - - - - - - -

(inner) trackAgentSearchSubmit(parameters, networkParametersopt) → {true|Error}

- - - - - - -
- -
Description:
-
  • User submitted an alternative agent search result search term
- - - -
Source:
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
Example
- -
constructorio.tracker.trackAgentSearchSubmit({
-    {
-        searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2',
-        intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2',
-        intent: 'show me a recipe for a cookie',
-        searchTerm: 'flour',
-    },
-);
- - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
parameters - - -object - - - - - - - - - - - Additional parameters to be sent with request -
Properties
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
intent - - -string - - - - - - - - - - - Intent of user request
searchTerm - - -string - - - - - - - - - - - Term of submitted agent search event
searchResultId - - -string - - - - - - - - - - - resultId of search result the clicked item belongs to
section - - -string - - - - - - - <optional>
- - - - - -
The section name for the item Ex. "Products"
intentResultId - - -string - - - - - - - <optional>
- - - - - -
intentResultId from the ASA response
- -
networkParameters - - -object - - - - - - - <optional>
- - - - - -
Parameters relevant to the network request -
Properties
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
timeout - - -number - - - - - - - <optional>
- - - - - -
Request timeout (in milliseconds)
- -
- - - - - - - - - - - - - - - - -
Returns:
- - - - -
-
- Type -
-
- -true -| - -Error - - - -
-
- - - - - - - - - - -

(inner) trackAgentSubmit(parameters, networkParametersopt) → {true|Error}

- - - - - - -
- -
Description:
-
  • User submitted an agent search - (pressing enter within agent input element, or clicking agent submit element)
- - - -
Source:
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
Example
- -
constructorio.tracker.trackAgentSubmit(
-    {
-        intent: 'show me a recipe for a cookie',
-    },
-);
- - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
parameters - - -object - - - - - - - - - - - Additional parameters to be sent with request -
Properties
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
intent - - -string - - - - - - - - - - - Intent of user request
section - - -string - - - - - - - <optional>
- - - - - -
The section name for the item Ex. "Products"
- -
networkParameters - - -object - - - - - - - <optional>
- - - - - -
Parameters relevant to the network request -
Properties
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
timeout - - -number - - - - - - - <optional>
- - - - - -
Request timeout (in milliseconds)
- -
- - - - - - - - - - - - - - - -
Returns:
@@ -3044,7 +339,7 @@

Source:
@@ -3063,8 +358,6 @@

Deprecated:
  • This method will be removed in a future version. Use trackAgentResultClick instead.
- @@ -3580,7 +873,7 @@

Source:
@@ -3599,8 +892,6 @@

Deprecated:
  • This method will be removed in a future version. Use trackAgentResultLoadFinished instead.
- @@ -4016,7 +1307,7 @@

Source:
@@ -4035,8 +1326,6 @@

Deprecated:
  • This method will be removed in a future version. Use trackAgentResultLoadStarted instead.
- @@ -4419,7 +1708,7 @@

Source:
@@ -4438,8 +1727,6 @@

Deprecated:
  • This method will be removed in a future version. Use trackAgentResultView instead.
- @@ -4923,7 +2210,7 @@

Source:
@@ -4942,8 +2229,6 @@

Deprecated:
  • This method will be removed in a future version. Use trackAgentSearchSubmit instead.
- @@ -5393,7 +2678,7 @@

Source:
@@ -5412,8 +2697,6 @@

Deprecated:
  • This method will be removed in a future version. Use trackAgentSubmit instead.
- @@ -9859,7 +7142,7 @@

Source:
@@ -10361,7 +7644,7 @@

Source:
@@ -10896,7 +8179,7 @@

Source:
@@ -11330,7 +8613,7 @@

Source:
@@ -11764,7 +9047,7 @@

Source:
@@ -12231,7 +9514,7 @@

Source:
@@ -12698,7 +9981,7 @@

Source:
@@ -13169,7 +10452,7 @@

Source:
@@ -19733,7 +17016,7 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_assistant.js.html b/docs/modules_assistant.js.html index ebe12d31..7ca534c1 100644 --- a/docs/modules_assistant.js.html +++ b/docs/modules_assistant.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -47,24 +47,135 @@

modules/assistant.js

-
const Agent = require('./agent');
-const { createAgentUrl, setupEventListeners } = require('./agent');
+            
const { cleanParams, trimNonBreakingSpaces, encodeURIComponentRFC3986, stringify } = require('../utils/helpers');
+
+// Create URL from supplied intent (term) and parameters
+function createAssistantUrl(intent, parameters, options) {
+  const {
+    apiKey,
+    version,
+    sessionId,
+    clientId,
+    userId,
+    segments,
+    testCells,
+    assistantServiceUrl,
+  } = options;
+  let queryParams = { c: version };
+
+  queryParams.key = apiKey;
+  queryParams.i = clientId;
+  queryParams.s = sessionId;
+
+  // Validate intent is provided
+  if (!intent || typeof intent !== 'string') {
+    throw new Error('intent is a required parameter of type string');
+  }
+
+  // Validate domain is provided
+  if (!parameters.domain || typeof parameters.domain !== 'string') {
+    throw new Error('parameters.domain is a required parameter of type string');
+  }
+
+  // Pull test cells from options
+  if (testCells) {
+    Object.keys(testCells).forEach((testCellKey) => {
+      queryParams[`ef-${testCellKey}`] = testCells[testCellKey];
+    });
+  }
+
+  // Pull user segments from options
+  if (segments && segments.length) {
+    queryParams.us = segments;
+  }
+
+  // Pull user id from options and ensure string
+  if (userId) {
+    queryParams.ui = String(userId);
+  }
+
+  if (parameters) {
+    const { domain, numResultsPerPage } = parameters;
+
+    // Pull domain from parameters
+    if (domain) {
+      queryParams.domain = domain;
+    }
+
+    // Pull results number from parameters
+    if (numResultsPerPage) {
+      queryParams.num_results_per_page = numResultsPerPage;
+    }
+  }
+
+  // eslint-disable-next-line no-underscore-dangle
+  queryParams._dt = Date.now();
+  queryParams = cleanParams(queryParams);
+
+  const queryString = stringify(queryParams);
+  const cleanedQuery = intent.replace(/^\//, '|'); // For compatibility with backend API
+
+  return `${assistantServiceUrl}/v1/intent/${encodeURIComponentRFC3986(trimNonBreakingSpaces(cleanedQuery))}?${queryString}`;
+}
+
+// Add event listeners to custom SSE that pushes data to the stream
+function setupEventListeners(eventSource, controller, eventTypes) {
+  const addListener = (type) => {
+    eventSource.addEventListener(type, (event) => {
+      const data = JSON.parse(event.data);
+
+      controller.enqueue({ type, data }); // Enqueue data into the stream
+    });
+  };
+
+  // Set up listeners for all event types except END
+  Object.values(eventTypes).forEach((type) => {
+    if (type !== eventTypes.END) {
+      addListener(type);
+    }
+  });
+
+  // Handle the END event separately to close the stream
+  eventSource.addEventListener(eventTypes.END, () => {
+    controller.close(); // Close the stream
+    eventSource.close(); // Close the EventSource connection
+  });
+
+  // Handle errors from the EventSource
+  // eslint-disable-next-line no-param-reassign
+  eventSource.onerror = (error) => {
+    controller.error(error); // Pass the error to the stream
+    eventSource.close(); // Close the EventSource connection
+  };
+}
 
 /**
- * @deprecated This module is deprecated and will be removed in a future version. Use the Agent module instead.
  * Interface to assistant SSE.
  *
  * @module assistant
  * @inner
  * @returns {object}
  */
-class Assistant extends Agent {
-  EventTypes = Agent.EventTypes;
+class Assistant {
+  constructor(options) {
+    this.options = options || {};
+  }
+
+  static EventTypes = {
+    START: 'start', // Denotes the start of the stream
+    GROUP: 'group', // Represents a semantic grouping of search results, optionally having textual explanation
+    SEARCH_RESULT: 'search_result', // Represents a set of results with metadata (used to show results with search refinements)
+    ARTICLE_REFERENCE: 'article_reference', // Represents a set of content with metadata
+    RECIPE_INFO: 'recipe_info', // Represents recipes' auxiliary information like cooking times & serving sizes
+    RECIPE_INSTRUCTIONS: 'recipe_instructions', // Represents recipe instructions
+    SERVER_ERROR: 'server_error', // Server Error event
+    IMAGE_META: 'image_meta', // This event type is used for enhancing recommendations with media content such as images
+    END: 'end', // Represents the end of data stream
+  };
 
   /**
    * Retrieve assistant results from EventStream
    *
-   * @deprecated Use getAssistantResultsStream from the Agent module instead.
    * @function getAssistantResultsStream
    * @description Retrieve a stream of assistant results from Constructor.io API
    * @param {string} intent - Intent to use to perform an intent based recommendations
@@ -80,12 +191,45 @@ 

modules/assistant.js

* const { value, done } = await reader.read(); */ getAssistantResultsStream(query, parameters) { - return this.getAgentResultsStream(query, parameters); + let eventSource; + let readableStream; + + try { + const requestUrl = createAssistantUrl(query, parameters, this.options); + + // Create an EventSource that connects to the Server Sent Events API + eventSource = new EventSource(requestUrl); + + // Create a readable stream that data will be pushed into + readableStream = new ReadableStream({ + // To be called on stream start + start(controller) { + // Listen to events emitted from ASA Server Sent Events and push data to the ReadableStream + setupEventListeners(eventSource, controller, Assistant.EventTypes); + }, + // To be called on stream cancelling + cancel() { + // Close the EventSource connection when the stream is prematurely canceled + eventSource.close(); + }, + }); + } catch (e) { + if (readableStream) { + readableStream?.cancel(); + } else { + // If the stream was not successfully created, close the EventSource directly + eventSource?.close(); + } + + throw new Error(e.message); + } + + return readableStream; } } module.exports = Assistant; -module.exports.createAssistantUrl = createAgentUrl; +module.exports.createAssistantUrl = createAssistantUrl; module.exports.setupEventListeners = setupEventListeners;
@@ -101,7 +245,7 @@

modules/assistant.js


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_autocomplete.js.html b/docs/modules_autocomplete.js.html index d95a2165..94666ed1 100644 --- a/docs/modules_autocomplete.js.html +++ b/docs/modules_autocomplete.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -276,7 +276,7 @@

modules/autocomplete.js


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_browse.js.html b/docs/modules_browse.js.html index 7b89c7cd..194b60e3 100644 --- a/docs/modules_browse.js.html +++ b/docs/modules_browse.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -593,7 +593,7 @@

modules/browse.js


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_quizzes.js.html b/docs/modules_quizzes.js.html index 7fd8c3d9..b3a7971a 100644 --- a/docs/modules_quizzes.js.html +++ b/docs/modules_quizzes.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -329,7 +329,7 @@

modules/quizzes.js


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_recommendations.js.html b/docs/modules_recommendations.js.html index fed1a4fb..48a33153 100644 --- a/docs/modules_recommendations.js.html +++ b/docs/modules_recommendations.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -243,7 +243,7 @@

modules/recommendations.js


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_search.js.html b/docs/modules_search.js.html index 12db8289..96d3a175 100644 --- a/docs/modules_search.js.html +++ b/docs/modules_search.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -373,7 +373,7 @@

modules/search.js


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/modules_tracker.js.html b/docs/modules_tracker.js.html index 58f45e3c..1adaf1de 100644 --- a/docs/modules_tracker.js.html +++ b/docs/modules_tracker.js.html @@ -31,7 +31,7 @@ -

Home

Classes

Modules

+

Home

Classes

Modules

@@ -2452,26 +2452,26 @@

modules/tracker.js

/** * Send ASA request submitted event * - * @function trackAgentSubmit + * @function trackAssistantSubmit * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - Intent of user request * @param {string} [parameters.section] - The section name for the item Ex. "Products" * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} - * @description User submitted an agent search - * (pressing enter within agent input element, or clicking agent submit element) + * @description User submitted an assistant search + * (pressing enter within assistant input element, or clicking assistant submit element) * @example - * constructorio.tracker.trackAgentSubmit( + * constructorio.tracker.trackAssistantSubmit( * { * intent: 'show me a recipe for a cookie', * }, * ); */ - trackAgentSubmit(parameters, networkParameters = {}) { + trackAssistantSubmit(parameters, networkParameters = {}) { if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { // Ensure parameters are provided (required) - const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/agent_submit?`; + const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/assistant_submit?`; const { section, intent, @@ -2503,9 +2503,9 @@

modules/tracker.js

} /** - * Send agent results page load started + * Send assistant results page load started * - * @function trackAgentResultLoadStarted + * @function trackAssistantResultLoadStarted * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - Intent of user request * @param {string} [parameters.section] - The section name for the item Ex. "Products" @@ -2513,19 +2513,19 @@

modules/tracker.js

* @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} - * @description Agent results page load begun (but has not necessarily loaded completely) + * @description Assistant results page load begun (but has not necessarily loaded completely) * @example - * constructorio.tracker.trackAgentResultLoadStarted( + * constructorio.tracker.trackAssistantResultLoadStarted( * { * intent: 'show me a recipe for a cookie', * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', * }, * ); */ - trackAgentResultLoadStarted(parameters, networkParameters = {}) { + trackAssistantResultLoadStarted(parameters, networkParameters = {}) { if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { // Ensure parameters are provided (required) - const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/agent_result_load_start?`; + const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/assistant_result_load_start?`; const { section, intentResultId, @@ -2559,9 +2559,9 @@

modules/tracker.js

} /** - * Send agent results page load finished + * Send assistant results page load finished * - * @function trackAgentResultLoadFinished + * @function trackAssistantResultLoadFinished * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - Intent of user request * @param {number} parameters.searchResultCount - Number of search results loaded @@ -2570,9 +2570,9 @@

modules/tracker.js

* @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} - * @description Agent results page load finished + * @description Assistant results page load finished * @example - * constructorio.tracker.trackAgentResultLoadFinished( + * constructorio.tracker.trackAssistantResultLoadFinished( * { * intent: 'show me a recipe for a cookie', * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', @@ -2580,10 +2580,10 @@

modules/tracker.js

* }, * ); */ - trackAgentResultLoadFinished(parameters, networkParameters = {}) { + trackAssistantResultLoadFinished(parameters, networkParameters = {}) { // Ensure parameters are provided (required) if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { - const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/agent_result_load_finish?`; + const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/assistant_result_load_finish?`; const { section, searchResultCount, @@ -2619,9 +2619,9 @@

modules/tracker.js

} /** - * Send agent result click event to API + * Send assistant result click event to API * - * @function trackAgentResultClick + * @function trackAssistantResultClick * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - intent of the user * @param {string} parameters.searchResultId - result_id of the specific search result the clicked item belongs to @@ -2633,9 +2633,9 @@

modules/tracker.js

* @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} - * @description User clicked a result that appeared within an agent search result + * @description User clicked a result that appeared within an assistant search result * @example - * constructorio.tracker.trackAgentResultClick( + * constructorio.tracker.trackAssistantResultClick( * { * variationId: 'KMH879-7632', * searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2', @@ -2645,10 +2645,10 @@

modules/tracker.js

* }, * ); */ - trackAgentResultClick(parameters, networkParameters = {}) { + trackAssistantResultClick(parameters, networkParameters = {}) { // Ensure parameters are provided (required) if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { - const requestPath = `${this.options.serviceUrl}/v2/behavioral_action/agent_search_result_click?`; + const requestPath = `${this.options.serviceUrl}/v2/behavioral_action/assistant_search_result_click?`; const { section = 'Products', variationId, @@ -2690,9 +2690,9 @@

modules/tracker.js

} /** - * Send agent search result view event to API + * Send assistant search result view event to API * - * @function trackAgentResultView + * @function trackAssistantResultView * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - intent of the user * @param {string} parameters.searchResultId - result_id of the specific search result the clicked item belongs to @@ -2703,9 +2703,9 @@

modules/tracker.js

* @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} - * @description User viewed a search result within an agent result + * @description User viewed a search result within an assistant result * @example - * constructorio.tracker.trackAgentResultView( + * constructorio.tracker.trackAssistantResultView( * { * searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2', * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', @@ -2715,10 +2715,10 @@

modules/tracker.js

* }, * ); */ - trackAgentResultView(parameters, networkParameters = {}) { + trackAssistantResultView(parameters, networkParameters = {}) { // Ensure parameters are provided (required) if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { - const requestPath = `${this.options.serviceUrl}/v2/behavioral_action/agent_search_result_view?`; + const requestPath = `${this.options.serviceUrl}/v2/behavioral_action/assistant_search_result_view?`; const { section = 'Products', items, @@ -2760,19 +2760,19 @@

modules/tracker.js

/** * Send ASA search submitted event * - * @function trackAgentSearchSubmit + * @function trackAssistantSearchSubmit * @param {object} parameters - Additional parameters to be sent with request * @param {string} parameters.intent - Intent of user request - * @param {string} parameters.searchTerm - Term of submitted agent search event + * @param {string} parameters.searchTerm - Term of submitted assistant search event * @param {string} parameters.searchResultId - resultId of search result the clicked item belongs to * @param {string} [parameters.section] - The section name for the item Ex. "Products" * @param {string} [parameters.intentResultId] - intentResultId from the ASA response * @param {object} [networkParameters] - Parameters relevant to the network request * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) * @returns {(true|Error)} - * @description User submitted an alternative agent search result search term + * @description User submitted an alternative assistant search result search term * @example - * constructorio.tracker.trackAgentSearchSubmit({ + * constructorio.tracker.trackAssistantSearchSubmit({ * { * searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2', * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', @@ -2781,11 +2781,11 @@

modules/tracker.js

* }, * ); */ - trackAgentSearchSubmit(parameters, networkParameters = {}) { + trackAssistantSearchSubmit(parameters, networkParameters = {}) { // Ensure parameters are provided (required) if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { // Ensure parameters are provided (required) - const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/agent_search_submit?`; + const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/assistant_search_submit?`; const { section, intent, @@ -2823,174 +2823,6 @@

modules/tracker.js

return new Error('parameters is a required parameter of type object'); } - /** - * Send ASA request submitted event - * - * @deprecated This method will be removed in a future version. Use trackAgentSubmit instead. - * @function trackAssistantSubmit - * @param {object} parameters - Additional parameters to be sent with request - * @param {string} parameters.intent - Intent of user request - * @param {string} [parameters.section] - The section name for the item Ex. "Products" - * @param {object} [networkParameters] - Parameters relevant to the network request - * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) - * @returns {(true|Error)} - * @description User submitted an assistant search - * (pressing enter within assistant input element, or clicking assistant submit element) - * @example - * constructorio.tracker.trackAssistantSubmit( - * { - * intent: 'show me a recipe for a cookie', - * }, - * ); - */ - trackAssistantSubmit(parameters, networkParameters = {}) { - return this.trackAgentSubmit(parameters, networkParameters); - } - - /** - * Send assistant results page load started - * - * @deprecated This method will be removed in a future version. Use trackAgentResultLoadStarted instead. - * @function trackAssistantResultLoadStarted - * @param {object} parameters - Additional parameters to be sent with request - * @param {string} parameters.intent - Intent of user request - * @param {string} [parameters.section] - The section name for the item Ex. "Products" - * @param {string} [parameters.intentResultId] - The intent result id from the ASA response - * @param {object} [networkParameters] - Parameters relevant to the network request - * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) - * @returns {(true|Error)} - * @description Assistant results page load begun (but has not necessarily loaded completely) - * @example - * constructorio.tracker.trackAssistantResultLoadStarted( - * { - * intent: 'show me a recipe for a cookie', - * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', - * }, - * ); - */ - trackAssistantResultLoadStarted(parameters, networkParameters = {}) { - return this.trackAgentResultLoadStarted(parameters, networkParameters); - } - - /** - * Send assistant results page load finished - * - * @deprecated This method will be removed in a future version. Use trackAgentResultLoadFinished instead. - * @function trackAssistantResultLoadFinished - * @param {object} parameters - Additional parameters to be sent with request - * @param {string} parameters.intent - Intent of user request - * @param {number} parameters.searchResultCount - Number of search results loaded - * @param {string} [parameters.section] - The section name for the item Ex. "Products" - * @param {string} [parameters.intentResultId] - The intent result id from the ASA response - * @param {object} [networkParameters] - Parameters relevant to the network request - * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) - * @returns {(true|Error)} - * @description Assistant results page load finished - * @example - * constructorio.tracker.trackAssistantResultLoadFinished( - * { - * intent: 'show me a recipe for a cookie', - * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', - * searchResultCount: 5, - * }, - * ); - */ - trackAssistantResultLoadFinished(parameters, networkParameters = {}) { - return this.trackAgentResultLoadFinished(parameters, networkParameters); - } - - /** - * Send assistant result click event to API - * - * @deprecated This method will be removed in a future version. Use trackAgentResultClick instead. - * @function trackAssistantResultClick - * @param {object} parameters - Additional parameters to be sent with request - * @param {string} parameters.intent - intent of the user - * @param {string} parameters.searchResultId - result_id of the specific search result the clicked item belongs to - * @param {string} parameters.itemId - Product item unique identifier - * @param {string} parameters.itemName - Product item name - * @param {string} [parameters.section] - The section name for the item Ex. "Products" - * @param {string} [parameters.variationId] - Product item variation unique identifier - * @param {string} [parameters.intentResultId] - Browse result identifier (returned in response from Constructor) - * @param {object} [networkParameters] - Parameters relevant to the network request - * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) - * @returns {(true|Error)} - * @description User clicked a result that appeared within an assistant search result - * @example - * constructorio.tracker.trackAssistantResultClick( - * { - * variationId: 'KMH879-7632', - * searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2', - * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', - * intent: 'show me a recipe for a cookie', - * itemId: 'KMH876', - * }, - * ); - */ - trackAssistantResultClick(parameters, networkParameters = {}) { - return this.trackAgentResultClick(parameters, networkParameters); - } - - /** - * Send assistant search result view event to API - * - * @deprecated This method will be removed in a future version. Use trackAgentResultView instead. - * @function trackAssistantResultView - * @param {object} parameters - Additional parameters to be sent with request - * @param {string} parameters.intent - intent of the user - * @param {string} parameters.searchResultId - result_id of the specific search result the clicked item belongs to - * @param {number} parameters.numResultsViewed - Number of items viewed in this search result - * @param {object[]} [parameters.items] - List of product item objects viewed - * @param {string} [parameters.section] - The section name for the item Ex. "Products" - * @param {string} [parameters.intentResultId] - Browse result identifier (returned in response from Constructor) - * @param {object} [networkParameters] - Parameters relevant to the network request - * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) - * @returns {(true|Error)} - * @description User viewed a search result within an assistant result - * @example - * constructorio.tracker.trackAssistantResultView( - * { - * searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2', - * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', - * intent: 'show me a recipe for a cookie', - * numResultsViewed: 5, - * items: [{itemId: 'KMH876'}, {itemId: 'KMH140'}, {itemId: 'KMH437'}], - * }, - * ); - */ - trackAssistantResultView(parameters, networkParameters = {}) { - this.trackAgentResultView(parameters, networkParameters); - } - - /** - * Send ASA search submitted event - * - * @deprecated This method will be removed in a future version. Use trackAgentSearchSubmit instead. - * @function trackAssistantSearchSubmit - * @param {object} parameters - Additional parameters to be sent with request - * @param {string} parameters.intent - Intent of user request - * @param {string} parameters.searchTerm - Term of submitted assistant search event - * @param {string} parameters.searchResultId - resultId of search result the clicked item belongs to - * @param {string} [parameters.section] - The section name for the item Ex. "Products" - * @param {string} [parameters.intentResultId] - intentResultId from the ASA response - * @param {object} [networkParameters] - Parameters relevant to the network request - * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds) - * @returns {(true|Error)} - * @description User submitted an alternative assistant search result search term - * @example - * constructorio.tracker.trackAssistantSearchSubmit({ - * { - * searchResultId: '019927c2-f955-4020-8b8d-6b21b93cb5a2', - * intentResultId: 'Zde93fd-f955-4020-8b8d-6b21b93cb5a2', - * intent: 'show me a recipe for a cookie', - * searchTerm: 'flour', - * }, - * ); - */ - trackAssistantSearchSubmit(parameters, networkParameters = {}) { - return this.trackAgentSearchSubmit(parameters, networkParameters); - } - /** * Send product insights agent view events * @@ -3606,7 +3438,7 @@

modules/tracker.js


- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. + Documentation generated by JSDoc 4.0.2 on Tue Jul 15 2025 13:59:42 GMT-0700 (Pacific Daylight Time) using the docdash theme.
From 83e7e103c80e1eacf6cb67ba17608ed538bd9839 Mon Sep 17 00:00:00 2001 From: Evan Yan Date: Thu, 17 Jul 2025 11:10:27 -0700 Subject: [PATCH 10/14] Revert docs for agent module --- docs/module-agent.html | 474 ------------------------------------- docs/modules_agent.js.html | 263 -------------------- 2 files changed, 737 deletions(-) delete mode 100644 docs/module-agent.html delete mode 100644 docs/modules_agent.js.html diff --git a/docs/module-agent.html b/docs/module-agent.html deleted file mode 100644 index dbe8dc7b..00000000 --- a/docs/module-agent.html +++ /dev/null @@ -1,474 +0,0 @@ - - - - - - agent - Documentation - - - - - - - - - - - - - - - - - - - - -
- -

agent

- - - - - - - -
- -
- -
- -
- -
- - - -
- -
Description:
-
  • Interface to agent SSE. -Replaces the previous Assistant module.
- - - -
Source:
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - -
Interface to agent SSE. -Replaces the previous Assistant module.
- - - - -
- - - - - - - - - - - - - - - - - -

Methods

- - - - - - -

(inner) getAgentResultsStream(intent, parametersopt) → {ReadableStream}

- - - - - - -
- -
Description:
-
  • Retrieve a stream of agent results from Constructor.io API
- - - -
Source:
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - -
Example
- -
const readableStream = constructorio.agent.getAgentResultsStream('I want to get shoes', {
-    domain: "nike_sportswear",
-});
-const reader = readableStream.getReader();
-const { value, done } = await reader.read();
- - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
intent - - -string - - - - - - - - - - - Intent to use to perform an intent based recommendations
parameters - - -object - - - - - - - <optional>
- - - - - -
Additional parameters to refine result set -
Properties
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeAttributesDescription
domain - - -string - - - - - - - <optional>
- - - - - -
domain name e.g. swimming sports gear, groceries
numResultsPerPage - - -number - - - - - - - <optional>
- - - - - -
The total number of results to return
- -
- - - - - - - - - - - - - - - - -
Returns:
- - -
- Returns a ReadableStream. -
- - - -
-
- Type -
-
- -ReadableStream - - - -
-
- - - - - - - - - - - -
- -
- - - - - - -
- -
- -
- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. -
- - - - - - - - - - - \ No newline at end of file diff --git a/docs/modules_agent.js.html b/docs/modules_agent.js.html deleted file mode 100644 index dc7047b8..00000000 --- a/docs/modules_agent.js.html +++ /dev/null @@ -1,263 +0,0 @@ - - - - - - modules/agent.js - Documentation - - - - - - - - - - - - - - - - - - - - -
- -

modules/agent.js

- - - - - - - -
-
-
const { cleanParams, trimNonBreakingSpaces, encodeURIComponentRFC3986, stringify } = require('../utils/helpers');
-
-// Create URL from supplied intent (term) and parameters
-function createAgentUrl(intent, parameters, options) {
-  const {
-    apiKey,
-    version,
-    sessionId,
-    clientId,
-    userId,
-    segments,
-    testCells,
-    agentServiceUrl,
-    assistantServiceUrl,
-  } = options;
-  let queryParams = { c: version };
-  queryParams.key = apiKey;
-  queryParams.i = clientId;
-  queryParams.s = sessionId;
-
-  const serviceUrl = agentServiceUrl || assistantServiceUrl;
-
-  // Validate intent is provided
-  if (!intent || typeof intent !== 'string') {
-    throw new Error('intent is a required parameter of type string');
-  }
-
-  // Validate domain is provided
-  if (!parameters.domain || typeof parameters.domain !== 'string') {
-    throw new Error('parameters.domain is a required parameter of type string');
-  }
-
-  // Pull test cells from options
-  if (testCells) {
-    Object.keys(testCells).forEach((testCellKey) => {
-      queryParams[`ef-${testCellKey}`] = testCells[testCellKey];
-    });
-  }
-
-  // Pull user segments from options
-  if (segments && segments.length) {
-    queryParams.us = segments;
-  }
-
-  // Pull user id from options and ensure string
-  if (userId) {
-    queryParams.ui = String(userId);
-  }
-
-  if (parameters) {
-    const { domain, numResultsPerPage } = parameters;
-
-    // Pull domain from parameters
-    if (domain) {
-      queryParams.domain = domain;
-    }
-
-    // Pull results number from parameters
-    if (numResultsPerPage) {
-      queryParams.num_results_per_page = numResultsPerPage;
-    }
-  }
-
-  // eslint-disable-next-line no-underscore-dangle
-  queryParams._dt = Date.now();
-  queryParams = cleanParams(queryParams);
-
-  const queryString = stringify(queryParams);
-  const cleanedQuery = intent.replace(/^\//, '|'); // For compatibility with backend API
-
-  return `${serviceUrl}/v1/intent/${encodeURIComponentRFC3986(trimNonBreakingSpaces(cleanedQuery))}?${queryString}`;
-}
-
-// Add event listeners to custom SSE that pushes data to the stream
-function setupEventListeners(eventSource, controller, eventTypes) {
-  const addListener = (type) => {
-    eventSource.addEventListener(type, (event) => {
-      const data = JSON.parse(event.data);
-
-      controller.enqueue({ type, data }); // Enqueue data into the stream
-    });
-  };
-
-  // Set up listeners for all event types except END
-  Object.values(eventTypes).forEach((type) => {
-    if (type !== eventTypes.END) {
-      addListener(type);
-    }
-  });
-
-  // Handle the END event separately to close the stream
-  eventSource.addEventListener(eventTypes.END, () => {
-    controller.close(); // Close the stream
-    eventSource.close(); // Close the EventSource connection
-  });
-
-  // Handle errors from the EventSource
-  // eslint-disable-next-line no-param-reassign
-  eventSource.onerror = (error) => {
-    controller.error(error); // Pass the error to the stream
-    eventSource.close(); // Close the EventSource connection
-  };
-}
-
-/**
- * Interface to agent SSE.
- * Replaces the previous Assistant module.
- *
- * @module agent
- * @inner
- * @returns {object}
- */
-class Agent {
-  constructor(options) {
-    this.options = options || {};
-  }
-
-  static EventTypes = {
-    START: 'start', // Denotes the start of the stream
-    GROUP: 'group', // Represents a semantic grouping of search results, optionally having textual explanation
-    SEARCH_RESULT: 'search_result', // Represents a set of results with metadata (used to show results with search refinements)
-    ARTICLE_REFERENCE: 'article_reference', // Represents a set of content with metadata
-    RECIPE_INFO: 'recipe_info', // Represents recipes' auxiliary information like cooking times & serving sizes
-    RECIPE_INSTRUCTIONS: 'recipe_instructions', // Represents recipe instructions
-    SERVER_ERROR: 'server_error', // Server Error event
-    IMAGE_META: 'image_meta', // This event type is used for enhancing recommendations with media content such as images
-    END: 'end', // Represents the end of data stream
-  };
-
-  /**
-   * Retrieve agent results from EventStream
-   *
-   * @function getAgentResultsStream
-   * @description Retrieve a stream of agent results from Constructor.io API
-   * @param {string} intent - Intent to use to perform an intent based recommendations
-   * @param {object} [parameters] - Additional parameters to refine result set
-   * @param {string} [parameters.domain] - domain name e.g. swimming sports gear, groceries
-   * @param {number} [parameters.numResultsPerPage] - The total number of results to return
-   * @returns {ReadableStream} Returns a ReadableStream.
-   * @example
-   * const readableStream = constructorio.agent.getAgentResultsStream('I want to get shoes', {
-   *     domain: "nike_sportswear",
-   * });
-   * const reader = readableStream.getReader();
-   * const { value, done } = await reader.read();
-   */
-  getAgentResultsStream(query, parameters) {
-    let eventSource;
-    let readableStream;
-
-    try {
-      const requestUrl = createAgentUrl(query, parameters, this.options);
-
-      // Create an EventSource that connects to the Server Sent Events API
-      eventSource = new EventSource(requestUrl);
-
-      // Create a readable stream that data will be pushed into
-      readableStream = new ReadableStream({
-        // To be called on stream start
-        start(controller) {
-          // Listen to events emitted from ASA Server Sent Events and push data to the ReadableStream
-          setupEventListeners(eventSource, controller, Agent.EventTypes);
-        },
-        // To be called on stream cancelling
-        cancel() {
-          // Close the EventSource connection when the stream is prematurely canceled
-          eventSource.close();
-        },
-      });
-    } catch (e) {
-      if (readableStream) {
-        readableStream?.cancel();
-      } else {
-        // If the stream was not successfully created, close the EventSource directly
-        eventSource?.close();
-      }
-
-      throw new Error(e.message);
-    }
-
-    return readableStream;
-  }
-}
-
-module.exports = Agent;
-module.exports.createAgentUrl = createAgentUrl;
-module.exports.setupEventListeners = setupEventListeners;
-
-
-
- - - - - - -
- -
- -
- Documentation generated by JSDoc 4.0.2 on Wed Jul 16 2025 20:45:32 GMT-0700 (Pacific Daylight Time) using the docdash theme. -
- - - - - - - - - - - From 6604dbe10474abd0b62dc6da0b181f73c297b7e1 Mon Sep 17 00:00:00 2001 From: Evan Yan Date: Mon, 21 Jul 2025 10:48:31 -0700 Subject: [PATCH 11/14] Add deprecation notice to assistantServiceUrl --- src/constructorio.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/constructorio.js b/src/constructorio.js index f2777857..958f94cd 100644 --- a/src/constructorio.js +++ b/src/constructorio.js @@ -40,7 +40,7 @@ class ConstructorIO { * @param {string} [parameters.serviceUrl='https://ac.cnstrc.com'] - API URL endpoint * @param {string} [parameters.quizzesServiceUrl='https://quizzes.cnstrc.com'] - Quizzes API URL endpoint * @param {string} [parameters.agentServiceUrl='https://agent.cnstrc.com'] - AI Shopping Agent API URL endpoint - * @param {string} [parameters.assistantServiceUrl='https://assistant.cnstrc.com'] - AI Shopping Assistant API URL endpoint + * @param {string} [parameters.assistantServiceUrl='https://assistant.cnstrc.com'] - AI Shopping Assistant API URL endpoint @deprecated This parameter is deprecated and will be removed in a future version. Use parameters.agentServiceUrl instead. * @param {array} [parameters.segments] - User segments * @param {object} [parameters.testCells] - User test cells * @param {string} [parameters.clientId] - Client ID, defaults to value supplied by 'constructorio-id' module @@ -63,7 +63,7 @@ class ConstructorIO { * @property {object} tracker - Interface to {@link module:tracker} * @property {object} quizzes - Interface to {@link module:quizzes} * @property {object} agent - Interface to {@link module:agent} - * @property {object} assistant - Interface to {@link module:assistant} + * @property {object} assistant - Interface to {@link module:assistant} @deprecated This property is deprecated and will be removed in a future version. Use the agent property instead. * @returns {class} */ constructor(options = {}) { From d66cc742a71b8ce88fcc7ef8ff5f5415d7f3a141 Mon Sep 17 00:00:00 2001 From: Evan Yan Date: Mon, 21 Jul 2025 10:49:56 -0700 Subject: [PATCH 12/14] Add missing statement --- src/modules/tracker.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/tracker.js b/src/modules/tracker.js index 29a06e4c..63fc2335 100644 --- a/src/modules/tracker.js +++ b/src/modules/tracker.js @@ -2910,7 +2910,7 @@ class Tracker { * ); */ trackAssistantResultView(parameters, networkParameters = {}) { - this.trackAgentResultView(parameters, networkParameters); + return this.trackAgentResultView(parameters, networkParameters); } /** From 6414abb22a95e2abc4eaa7e21c7d60fb8c7c9eca Mon Sep 17 00:00:00 2001 From: Evan Yan Date: Tue, 22 Jul 2025 14:14:48 -0700 Subject: [PATCH 13/14] Update assistant method types --- src/types/tracker.d.ts | 157 +++++++++++++++++------------------------ 1 file changed, 64 insertions(+), 93 deletions(-) diff --git a/src/types/tracker.d.ts b/src/types/tracker.d.ts index 35a02d73..75f08487 100644 --- a/src/types/tracker.d.ts +++ b/src/types/tracker.d.ts @@ -331,136 +331,107 @@ declare class Tracker { networkParameters?: NetworkParameters ): true | Error; - trackAssistantSubmit( + trackAssistantSubmit: typeof Tracker.prototype.trackAgentSubmit; + + trackAssistantResultLoadStarted: typeof Tracker.prototype.trackAgentResultLoadStarted; + + trackAssistantResultLoadFinished: typeof Tracker.prototype.trackAgentResultLoadFinished; + + trackAssistantResultClick: typeof Tracker.prototype.trackAgentResultClick; + + trackAssistantResultView: typeof Tracker.prototype.trackAgentResultView; + + trackAssistantSearchSubmit: typeof Tracker.prototype.trackAgentSearchSubmit; + + trackProductInsightsAgentViews( parameters: { - intent: string; + questions: Question[]; + itemId: string; + itemName: string; + viewTimespans: TimeSpan[]; + variationId?: string; section?: string; }, networkParameters?: NetworkParameters ): true | Error; - trackAssistantResultLoadStarted( + trackProductInsightsAgentView( parameters: { - intent: string; + questions: Question[]; + itemId: string; + itemName: string; + variationId?: string; section?: string; - intentResultId?: string; }, networkParameters?: NetworkParameters ): true | Error; - trackAssistantResultLoadFinished( + trackProductInsightsAgentOutOfView( parameters: { - intent: string; - searchResultCount: number; + itemId: string; + itemName: string; + variationId?: string; section?: string; - intentResultId?: string; }, networkParameters?: NetworkParameters ): true | Error; - trackAssistantResultClick( + trackProductInsightsAgentFocus( parameters: { - intent: string; - searchResultId: string; - itemId?: string; - itemName?: string; + itemId: string; + itemName: string; variationId?: string; section?: string; - intentResultId?: string; }, networkParameters?: NetworkParameters ): true | Error; - trackAssistantResultView( + trackProductInsightsAgentQuestionClick( parameters: { - intent: string; - searchResultId: string; - numResultsViewed: number; - items?: ItemTracked[]; - intentResultId?: string; + itemId: string; + itemName: string; + question: string; + variationId?: string; section?: string; }, networkParameters?: NetworkParameters ): true | Error; - trackAssistantSearchSubmit( + trackProductInsightsAgentQuestionSubmit( parameters: { - intent: string; - searchTerm: string; - userInput: string; - searchResultId: string; - groupId?: string; + itemId: string; + itemName: string; + question: string; + variationId?: string; section?: string; - intentResultId?: string; }, networkParameters?: NetworkParameters ): true | Error; - trackProductInsightsAgentViews(parameters: { - questions: Question[]; - itemId: string; - itemName: string; - viewTimespans: TimeSpan[]; - variationId?: string; - section?: string; - }, networkParameters?: NetworkParameters): true | Error; - - trackProductInsightsAgentView(parameters: { - questions: Question[]; - itemId: string; - itemName: string; - variationId?: string; - section?: string; - }, networkParameters?: NetworkParameters): true | Error; - - trackProductInsightsAgentOutOfView(parameters: { - itemId: string; - itemName: string; - variationId?: string; - section?: string; - }, networkParameters?: NetworkParameters): true | Error; - - trackProductInsightsAgentFocus(parameters: { - itemId: string; - itemName: string; - variationId?: string; - section?: string; - }, networkParameters?: NetworkParameters): true | Error; - - trackProductInsightsAgentQuestionClick(parameters: { - itemId: string; - itemName: string; - question: string; - variationId?: string; - section?: string; - }, networkParameters?: NetworkParameters): true | Error; - - trackProductInsightsAgentQuestionSubmit(parameters: { - itemId: string; - itemName: string; - question: string; - variationId?: string; - section?: string; - }, networkParameters?: NetworkParameters): true | Error; - - trackProductInsightsAgentAnswerView(parameters: { - itemId: string; - itemName: string; - question: string; - answerText: string; - qnaResultId?: string; - variationId?: string; - section?: string; - }, networkParameters?: NetworkParameters): true | Error; - - trackProductInsightsAgentAnswerFeedback(parameters: { - itemId: string; - itemName: string; - feedbackLabel: string; - qnaResultId?: string; - variationId?: string; - section?: string; - }, networkParameters?: NetworkParameters): true | Error; + trackProductInsightsAgentAnswerView( + parameters: { + itemId: string; + itemName: string; + question: string; + answerText: string; + qnaResultId?: string; + variationId?: string; + section?: string; + }, + networkParameters?: NetworkParameters + ): true | Error; + + trackProductInsightsAgentAnswerFeedback( + parameters: { + itemId: string; + itemName: string; + feedbackLabel: string; + qnaResultId?: string; + variationId?: string; + section?: string; + }, + networkParameters?: NetworkParameters + ): true | Error; on(messageType: string, callback: Function): true | Error; } From 8a29afb42db3410704708ada9f20f9d37e3b1f07 Mon Sep 17 00:00:00 2001 From: Evan Yan Date: Wed, 23 Jul 2025 16:30:07 -0700 Subject: [PATCH 14/14] Use correct endpoints --- src/modules/tracker.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/modules/tracker.js b/src/modules/tracker.js index 63fc2335..065d35e1 100644 --- a/src/modules/tracker.js +++ b/src/modules/tracker.js @@ -2422,7 +2422,7 @@ class Tracker { trackAgentSubmit(parameters, networkParameters = {}) { if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { // Ensure parameters are provided (required) - const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/agent_submit?`; + const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/assistant_submit?`; const { section, intent, @@ -2476,7 +2476,7 @@ class Tracker { trackAgentResultLoadStarted(parameters, networkParameters = {}) { if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { // Ensure parameters are provided (required) - const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/agent_result_load_start?`; + const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/assistant_result_load_start?`; const { section, intentResultId, @@ -2534,7 +2534,7 @@ class Tracker { trackAgentResultLoadFinished(parameters, networkParameters = {}) { // Ensure parameters are provided (required) if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { - const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/agent_result_load_finish?`; + const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/assistant_result_load_finish?`; const { section, searchResultCount, @@ -2599,7 +2599,7 @@ class Tracker { trackAgentResultClick(parameters, networkParameters = {}) { // Ensure parameters are provided (required) if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { - const requestPath = `${this.options.serviceUrl}/v2/behavioral_action/agent_search_result_click?`; + const requestPath = `${this.options.serviceUrl}/v2/behavioral_action/assistant_search_result_click?`; const { section = 'Products', variationId, @@ -2669,7 +2669,7 @@ class Tracker { trackAgentResultView(parameters, networkParameters = {}) { // Ensure parameters are provided (required) if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { - const requestPath = `${this.options.serviceUrl}/v2/behavioral_action/agent_search_result_view?`; + const requestPath = `${this.options.serviceUrl}/v2/behavioral_action/assistant_search_result_view?`; const { section = 'Products', items, @@ -2736,7 +2736,7 @@ class Tracker { // Ensure parameters are provided (required) if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) { // Ensure parameters are provided (required) - const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/agent_search_submit?`; + const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/assistant_search_submit?`; const { section, intent,