npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

amazon-q-connectjs

v2.0.0

Published

Amazon Q Connect JavaScript Client

Downloads

14

Readme

amazon-q-connectjs

Amazon Q in Connect, an LLM-enhanced evolution of Amazon Connect Wisdom. The Amazon Q Connect JavaScript library (QConnectJS) gives you the power to build your own Amazon Q Connect widget.

The library uses an Amazon Connect authentication token to make API calls to Amazon Q Connect and supports all Amazon Q Connect Agent Assistant functionality. For example, you can manually query knowledge documents, get knowledge content, or start generating automated suggestions.

QConnectJS supports the following APIs:

Note that this library must be used in conjunction with amazon-connect-streams.

Learn More

For more advanced features, all Amazon Q Connect functionality is accessible using the public API. For example, you can create an assistant and a knowledge base. Check out Amazon Q Connect available via the AWS CLI.

To learn more about Amazon Q Connect and its capabilities, please check out the Amazon Q Connect User Guide.

To learn more about Amazon Connect and its capabilities, please check out the Amazon Connect User Guide.

Getting Started

Prerequisites

Create an Amazon Connect Instance

The first step in setting up your Amazon Connect contact center is to create a virtual contact center instance. Each instance contains all the resources and settings related to your contact center. Follow the Get started with Amazon Connect user guide to get started.

Enable Amazon Q Connect For Your Instance

To utilize QConnectJS you should start by enabling Amazon Q Connect for your Amazon Connect instance. Follow the Enable Amazon Q Connect user guide to get started.

Set Up Integrated Applications

All domains looking to integrate with Amazon Connect and Amazon Q Connect must be explicitly allowed for cross-domain access to the instance. For example, to integrate with your custom agent application, you must place your agent application domain in an allow list. To allow list a domain URL follow the app integration guide.

A few things to note:

  • Allowlisted domains must be HTTPS.
  • All of the pages that attempt to initialize the QConnectJS library must be hosted on domains that are allowlisted.

Usage

Install from NPM

npm install amazon-q-connectjs

Build with NPM

$ git clone https://github.com/aws/amazon-q-connectjs
cd amazon-q-connectjs
npm install
npm run bundle

Find build artifacts in the release directory. This will generate a file called amazon-q-connectjs.js and a minified version amazon-q-connectjs-min.js. This is the full QConnectJS client which you will want to include in your page.

Download from Github

amazon-q-connectjs is available on NPM but if you'd like to download it here, you can find build artificacts in the release directory.

Load from CDN

amazon-q-connectjs is also available on open source CDNs. If you'd like to load build artifacts from a CDN, you can use either of the script tags below.

<script src="https://cdn.jsdelivr.net/npm/amazon-q-connectjs@1/release/amazon-q-connectjs.js"></script>
<script src="https://cdn.jsdelivr.net/npm/amazon-q-connectjs@1/release/amazon-q-connectjs-min.js"></script>

Initialization

Initializing the QConnectJS client is the fist step to verify that you have everything setup correctly.

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="connect-streams-min.js"></script>
    <script type="text/javascript" src="amazon-q-connectjs-min.js"></script>
  </head>
  <!-- Add the call to init() as an onload so it will only run once the page is loaded -->
  <body onload="init()">
    <div id='ccp-container' style="width: 400px; height: 800px;"></div>
    <script type="text/javascript">
      const instanceUrl = 'https://my-instance-domain.awsapps.com/connect';

      function init() {
        // Initialize StreamsJS API
        connect.agentApp.initApp(
          'ccp',
          'ccp-container',
          `${instanceUrl}/ccp-v2/`,
          {
            ccpParams: {
              style: 'width:400px; height:600px;',
            }
          }
        );
  
        // Initialize QConnectJS client with either "QConnectClient" or "Client"
        const qConnectClient = new connect.qconnectjs.QConnectClient({
          instanceUrl,
        });

        const qConnectClient = new connect.qconnectjs.Client({
          instanceUrl: instanceUrl,                                        // REQUIRED
          endpoint: "https://my-instance-domain.awsapps.com/connect/api",  // optional, defaults to '<instanceUrl>'
          callSource: "agent-app",                                         // optional, defaults to 'agent-app'
          serviceId: 'AmazonQConnect',                                     // optional, defaults to 'AmazonQConnect'
          maxAttempts: 3,                                                  // optional, defaults to 3
          logger: {},                                                      // optional, if provided overrides default logger
          headers: {},                                                     // optional, if provided overrides request headers
          requestHandler: {},                                              // optional, if provided overrides the default request handler
        });
      }
    </script>
  </body>
</html>

The QConnectJS client integrates with Connect by loading the pre-built Amazon Q Connect widget located at <instanceUrl>/wisdom-v2 into an iframe and placing it into a container div. API requests are funneled through this widget and made available to your JS client code.

  • instanceUrl: The Connect instance url.
  • endpoint: Optional, set to override the Connect endpoint to use.
  • callSource: Optional, set to override the call source identifier on requests.
  • headers: This object is optional and allows overriding the headers provided to the HTTP handler.
  • logger: This object is optional and allows overriding the default Logger for logging debug/info/warn/error messages.
  • maxAttempts: Optional, set to specify how many times a request will be made at most in case of retry.
  • requestHandler: This object is optional and allows overriding the default request handler.
  • serviceId: Optional, set to override the unique service identifier on requests.

ES Modules

Imports

QConnectJS is modularized by client and commands. To send a request, you only need to import the QConnectClient and the commands you need, for example GetRecommendations:

// ES5 example
const { Client, GetRecommendations } = require("amazon-q-connectjs");
// ES6+ example
import { Client, GetRecommendations } from "amazon-q-connectjs";

Convenience Methods

The QConnectJS client can also send requests using convenience methods. However, it results in a bigger bundle size.

import { QConnectClient } from "amazon-q-connectjs";

const qConnectClient = new QConnectClient({
  instanceUrl: "https://your-connect-instance.my.connect.aws",
});

// async/await.
try {
  const response = await qConnectClient.getRecommendations(params);
  // process response.
} catch (error) {
  // error handling.
}

// Promises.
qConnectClient
  .getRecommendations(params)
  .then((response) => {
    // process response.
  })
  .catch((error) => {
    // error handling.
  });

Sending Requests

To send a request, you:

  • Initiate the client with the desired configuration (e.g. instanceUrl, endpoint).
  • call the desired API
const qConnectClient = new QConnectClient({
  instanceUrl: "https://your-connect-instance.my.connect.aws",
});

qConnectClient.getRecommendations({
  // input parameters
});

Reading Responses

All API calls through QConnectJS return a promise. The promise resolves/rejects to provide the response from the API call.

Async/await

We recommend using the await operator.

// async/await.
try {
  const response = await qConnectClient.getRecommendations({
    // input parameters
  });
  // process response.
} catch (error) {
  // error handling.
} finally {
  // finally.
}

async-await is clean, concise, intuitive, easy to debug and has better error handling as compared to using Promise chains.

Promises

You can also use Promise chaining.

qConnectClient.getRecommendations({
  // input parameters
}).then((response) => {
  // process response.
}).catch((error) => {
  // error handling.
}).finally(() => {
  // finally.
});

APIs

QueryAssistant

Performs a manual search against the specified assistant. To retrieve recommendations for an assistant, use GetRecommendations. For more information check out the QueryAssistant API reference.

URI Request Parameters

  • assistantId: The identifier of the Amazon Q Connect assistant. Can be either the ID or the ARN. URLs cannot contain the ARN.
  • queryText: The text to search for.
  • maxResults: The maximum number of results to return per page.
  • nextToken: The token for the next set of results. Use the value returned in the previous response in the next request to retrieve the next set of results.

A few things to note:

  • The assistantId can be retrieved by using the ListIntegrationAssociations API provided by QConnectJS to look up the assistant and knowledge base that has been configured for Amazon Q Connect. See ListIntegrationAssociations for more information.

Response Syntax

If the action is successful, the service sends back an HTTP 200 response.

{
  "nextToken": "string",
  "results": [
    {
      "document": {
        "contentReference": {
          "contentArn": "string",
          "contentId": "string",
          "knowledgeBaseArn": "string",
          "knowledgeBaseId": "string"
        },
        "excerpt": {
          "highlights": [
            {
              "beginOffsetInclusive": "number",
              "endOffsetExclusive": "number"
            }
          ],
          "text": "string"
        },
        "title": {
          "highlights": [
            {
              "beginOffsetInclusive": "number",
              "endOffsetExclusive": "number"
            }
          ],
          "text": "string"
        }
      },
      "relevanceScore": "number",
      "resultId": "string"
    }
  ]
}

Sample Query

const queryAssistantCommand = new QueryAssistant({
  assistantId: 'b5b0e4af-026e-4472-9371-d171a9fdf75a',
  maxResults: 10,
  queryText: 'cancel order',
});

try {
  const response = await qConnectClient.call(queryAssistantCommand);
    // process response.
} catch (error) {
  // error handling.
}

GetContact

Retrieves contact details, including the Amazon Q Connect session ARN, for a specified contact.

URI Request Parameters

  • awsAccountId: The identifier of the AWS account. You can find the awsAccountId in the ARN of the instance.
  • InstanceId: The identifier of the Amazon Connect instance. You can find the instanceId in the ARN of the instance.
  • contactId: The identifier of the Connect contact. Can be either the ID or the ARN. URLs cannot contain the ARN.

A few things to note:

  • One of the request parameters of the GetContact API is the Amazon Connect contactId. The StreamsJS Contact API provides event subscription methods and action methods which can be called on behalf of a Contact and used to retrieve the Amazon Connect contactId. See StreamsJS Integration below for more information.

Response Syntax

If the action is successful, the service sends back an HTTP 200 response.

{
  "contactId": "string",
  "contactState": "string",
  "contactSchemaVersion": "number",
  "channel": "string",
  "targetQueueResourceId": "string",
  "agentResourceId": "string",
  "targetAgentResourceId": "string",
  "attributes": {},
  "participants": [],
  "contactFeature": {
    "loggingEnabled": "boolean",
    "textToSpeechFeatures": {},
    "voiceIdFeatures": {},
    "wisdomFeatures": {
      "wisdomConfig": {
        "sessionArn": "string"
      }
    }
  },
  "routingAttributes": {},
  "languageCode": "string",
  "channelContext": {},
}

Sample Query

const getContactCommand = new GetContact({
  awsAccountId: 'b5b0e4af-026e-4472-9371-d171a9fdf75a',
  instanceId: 'b5b0e4af-026e-4472-9371-d171a9fdf75a',
  contactId: 'b5b0e4af-026e-4472-9371-d171a9fdf75a',
});

try {
  const response = await qConnectClient.call(getContactCommand);
    // process response.
} catch (error) {
  // error handling.
}

GetRecommendations

Retrieves recommendations for the specified session. To avoid retrieving the same recommendations in subsequent calls, use NotifyRecommendationsReceived. This API supports long-polling behavior with the waitTimeSeconds parameter. Short poll is the default behavior and only returns recommendations already available. To perform a manual query against an assistant, use the QueryAssistant API. For more information check out the GetRecommendations API reference.

URI Request Parameters

  • assistantId: The identifier of the Amazon Q Connect assistant. Can be either the ID or the ARN. URLs cannot contain the ARN.
  • maxResult: The maximum number of results to return per page.
  • sessionId: The identifier of the session. Can be either the ID or the ARN. URLs cannot contain the ARN.
  • waitTimeSeconds: The duration (in seconds) for which the call waits for a recommendation to be made available before returning. If a recommendation is available, the call returns sooner than WaitTimeSeconds. If no messages are available and the wait time expires, the call returns successfully with an empty list.

A few things to note:

  • The assistantId can be retrieved by using the ListIntegrationAssociations API provided by QConnectJS to look up the assistant and knowledge base that has been configured for Amazon Q Connect. See ListIntegrationAssociations for more information.
  • The session ARN can be retrieved by used the GetContact API provided by QConnectJS to look up the session associated with a given active contact. See GetContact for more information.
  • To avoid retrieving the same recommendations on subsequent calls, the NotifyRecommendationsReceived API should be called after each response. See NotifyRecommendationsReceived for more information.

Response Syntax

{
  "recommendations": [
    {
      "document": {
        "contentReference": {
          "contentArn": "string",
          "contentId": "string",
          "knowledgeBaseArn": "string",
          "knowledgeBaseId": "string"
        },
        "excerpt": {
          "highlights": [
            {
              "beginOffsetInclusive": "number",
              "endOffsetExclusive": "number"
            }
          ],
          "text": "string"
        },
        "title": {
          "highlights": [
            {
              "beginOffsetInclusive": "number",
              "endOffsetExclusive": "number"
            }
          ],
          "text": "string"
        }
      },
      "recommendationId": "string",
      "relevanceLevel": "string",
      "relevanceScore": "number"
    }
  ]
}

Sample Query

const getRecommendationsCommand = new GetRecommendations({
  assistantId: 'b5b0e4af-026e-4472-9371-d171a9fdf75a',
  sessionId: '9f2b6fab-9200-46d8-977a-c89be3b34639',
  maxResults: 10,
  waitTimeSeconds: 5,
});

try {
  const response = await qConnectClient.call(getRecommendationsCommand);
    // process response.
} catch (error) {
  // error handling.
}

NotifyRecommendationsReceived

Removes the specified recommendations from the specified assistant's queue of newly available recommendations. You can use this API in conjunction with GetRecommendations and a waitTimeSeconds input for long-polling behavior and avoiding duplicate recommendations. For more information check out the NotifyRecommendationsReceived API reference.

URI Request Parameters

  • assistantId: The identifier of the Amazon Q Connect assistant. Can be either the ID or the ARN. URLs cannot contain the ARN.
  • sessionId: The identifier of the session. Can be either the ID or the ARN. URLs cannot contain the ARN.
  • recommendationIds: The identifier of the recommendations.

A few things to note:

  • The assistantId can be retrieved by using the ListIntegrationAssociations API provided by QConnectJS to look up the assistant and knowledge base that has been configured for Amazon Q Connect. See ListIntegrationAssociations for more information.
  • The session ARN can be retrieved by used the GetContact API provided by QConnectJS to look up the session associated with a given active contact. See GetContact for more information.

Response Syntax

{
  "errors": [
    {
      "message": "string",
      "recommendationId": "string"
    }
  ],
  "recommendationIds": [ "string" ]
}

Sample Query

const notifyRecommendationsReceivedCommand = new NotifyRecommendationsReceived({
  assistantId: 'b5b0e4af-026e-4472-9371-d171a9fdf75a',
  sessionId: '9f2b6fab-9200-46d8-977a-c89be3b34639',
  recommendationIds: [
    'f9b5fa90-b3ce-45c9-9967-582c87074864',
  ],
});

try {
  const response = await qConnectClient.call(notifyRecommendationsReceivedCommand);
    // process response.
} catch (error) {
  // error handling.
}

GetContent

Retrieves content, including a pre-signed URL to download the content. The contentId and knowledgeBaseId request parameters are part of search results response syntax when calling QueryAssistant or recommendations response syntax when calling GetRecommendations. For more information check out the GetContent API reference.

URI Request Parameters

  • contentId: The identifier of the content. Can be either the ID or the ARN. URLs cannot contain the ARN.
  • knowledgeBaseId: The identifier of the knowledge base. Can be either the ID or the ARN. URLs cannot contain the ARN.

A few things to note:

  • The contentId and knowledgeBaseId can be found by using either the QueryAssistant or GetRecommendations APIs to retrieve knowledge documents. Each of the documents from either response will include a contentReference. See QueryAssistant and GetRecommendations for more information.

Sample Query

const getContentCommand = new GetContent({
  contentId: '9f2b6fab-9200-46d8-977a-c89be3b34639',
  knowledgeBaseId: 'f9b5fa90-b3ce-45c9-9967-582c87074864',
});

try {
  const response = await qConnectClient.call(getContentCommand);
    // process response.
} catch (error) {
  // error handling.
}

SearchSessions

Searches for sessions. For more information check out the SearchSessions API reference.

URI Request Parameters

  • assistantId: The identifier of the Amazon Q Connect assistant. Can be either the ID or the ARN. URLs cannot contain the ARN.
  • searchExpression: The search expression to filter results.
  • maxResults: The maximum number of results to return per page.
  • nextToken: The token for the next set of results. Use the value returned in the previous response in the next request to retrieve the next set of results.

A few things to note:

  • The assistantId can be retrieved by using the ListIntegrationAssociations API provided by QConnectJS to look up the assistant and knowledge base that has been configured for Amazon Q Connect. See ListIntegrationAssociations for more information.

Response Syntax

{
  "nextToken": "string",
  "sessionSummaries": [
    {
      "assistantArn": "string",
      "assistantId": "string",
      "sessionArn": "string",
      "sessionId": "string"
    }
  ]
}

Sample Query

const searchSessionsCommand = new SearchSessions({
  assistantId: 'b5b0e4af-026e-4472-9371-d171a9fdf75a',
  searchExpression: {
    filters: [
      {
        operator: 'equals',
        field: 'name',
        value: '249bbb30-aede-42a8-be85-d8483c317686',
      }
    ]
  }
});

try {
  const response = await qConnectClient.call(searchSessionsCommand);
    // process response.
} catch (error) {
  // error handling.
}

ListIntegrationAssociations

Retrieves Connect integrations, including assistant and knowledge base integrations. For more information check out the ListIntegrationAssociations API reference.

URI Request Parameters

  • InstanceId: The identifier of the Amazon Connect instance. You can find the instanceId in the ARN of the instance.
  • IntegrationType: The integration type.
  • MaxResults: The maximum number of results to return per page.
  • nextToken: The token for the next set of results. Use the value returned in the previous response in the next request to retrieve the next set of results.

A few things to note:

  • One of the request parameters of the ListIntegrationAssociations API is the Amazon Connect instanceId. The StreamsJS Agent API provides event subscription methods and action methods which can be called on behalf of the agent and used to retrieve the Amazon Connect instanceId. See StreamsJS Integration below for more information.

Response Syntax

If the action is successful, the service sends back an HTTP 200 response.

{
  "IntegrationAssociationSummaryList": [
    {
      "InstanceId": "string",
      "IntegrationArn": "string",
      "IntegrationAssociationArn": "string",
      "IntegrationAssociationId": "string",
      "IntegrationType": "string",
      "SourceApplicationName": "string",
      "SourceApplicationUrl": "string",
      "SourceType": "string"
    }
  ],
  "NextToken": "string"
}

Sample Query

const listIntegrationAssociationsCommand = new ListIntegrationAssociations({
  InstanceId: 'b5b0e4af-026e-4472-9371-d171a9fdf75a',
  IntegrationType: 'WISDOM_ASSISTANT',
  MaxResults: 10,
});

try {
  const response = await qConnectClient.call(listIntegrationAssociations);
    // process response.
} catch (error) {
  // error handling.
}

StreamsJS Integration

In order to use QConnectJS, the library must be used in conjunction with amazon-connect-streams. Integrating with Amazon Connect Streams provides enables you to handle agent and contact state events directly through an object oriented event driven interface.

Agent

The StreamsJS Agent API provides event subscription methods and action methods which can be called on behalf of the agent. For more information check out the StreamsJS Agent API reference.

InstanceId

The StreamsJS Agent API can be used to retrieve the Amazon Connect instanceId using the Agent routingProfileId. The routing profile contains the following fields:

  • channelConcurrencyMap: See agent.getChannelConcurrency() for more info.
  • defaultOutboundQueue: The default queue which should be associated with outbound contacts. See queues for details on properties.
  • name: The name of th routing profile.
  • queues: The queues contained in the routing profile. Each queue object has the following properties:
    • name: The name of the queue.
    • queueARN: The ARN of the queue.
    • queueId: Alias for the queueARN.
  • routingProfileARN: The routing profile ARN.
  • routingProfileId: Alias for the routingProfileARN.
const routingProfile = agent.getRoutingProfile();

const instanceId = routingProfile.routingProfileId.match(
  /instance\/([0-9a-fA-F|-]+)\//
)[1];

Contact

The StreamsJS Contact API provides event subscription methods and action methods which can be called on behalf of a contact. For more information check out the StreamsJS Contact API reference.

ContactId

The StreamsJS Contact API can be used to retrieve the Amazon Connect contactId using the Contact getContactId method.

const contactId = contact.getContactId();

Troubleshooting

When the service returns an exception, the error will include the exception information.

try {
  const data = await client.call(command);
  // process data.
} catch (error) {
  console.log(error);
  // error handling.
}

Contributing

See CONTRIBUTING for more information.

License

QConnectJS is distributed under the Apache License, Version 2.0, see LICENSE for more information.