Skip to content

Commit

Permalink
Add provider layer and hide constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
nbramblett committed Aug 4, 2023
1 parent 9f50a9e commit dfeafb7
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 117 deletions.
21 changes: 0 additions & 21 deletions docs/chat-core.chatcore._constructor_.md

This file was deleted.

28 changes: 0 additions & 28 deletions docs/chat-core.chatcore.getnextmessage.md

This file was deleted.

19 changes: 2 additions & 17 deletions docs/chat-core.chatcore.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,11 @@

[Home](./index.md) > [@yext/chat-core](./chat-core.md) > [ChatCore](./chat-core.chatcore.md)

## ChatCore class
## ChatCore type

The entrypoint to the chat-core library. Provides methods for interacting with Chat API.

**Signature:**

```typescript
export declare class ChatCore
export type ChatCore = typeof Core;
```

## Constructors

| Constructor | Modifiers | Description |
| --- | --- | --- |
| [(constructor)(chatConfig, promptPackage)](./chat-core.chatcore._constructor_.md) | | Constructs a new instance of the <code>ChatCore</code> class |

## Methods

| Method | Modifiers | Description |
| --- | --- | --- |
| [getNextMessage(request)](./chat-core.chatcore.getnextmessage.md) | | Make a request to Chat API to generate the next message. |
| [streamNextMessage(request)](./chat-core.chatcore.streamnextmessage.md) | | Make a request to Chat streaming API to generate the next message and consume its tokens via server-sent events. |

28 changes: 0 additions & 28 deletions docs/chat-core.chatcore.streamnextmessage.md

This file was deleted.

8 changes: 7 additions & 1 deletion docs/chat-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

| Class | Description |
| --- | --- |
| [ChatCore](./chat-core.chatcore.md) | The entrypoint to the chat-core library. Provides methods for interacting with Chat API. |
| [StreamResponse](./chat-core.streamresponse.md) | Wrapper class around [RawResponse](./chat-core.rawresponse.md) that provides an interface for working with Chat's streaming data in both browser and Node environments. |

## Enumerations
Expand All @@ -20,6 +19,12 @@
| [Region](./chat-core.region.md) | The region to send the requests to. |
| [StreamEventName](./chat-core.streameventname.md) | Names of stream events returned from Chat Stream API. |

## Functions

| Function | Description |
| --- | --- |
| [ProvideChatCore(config)](./chat-core.providechatcore.md) | Provider for the ChatCore library |

## Interfaces

| Interface | Description |
Expand All @@ -39,6 +44,7 @@

| Type Alias | Description |
| --- | --- |
| [ChatCore](./chat-core.chatcore.md) | |
| [ChatPrompt](./chat-core.chatprompt.md) | The type of prompts to be used by the Chat bot. Experimental changes to prompts will be applied to the "nightly" package and subsequently moved into "stable" after performance and reliability are verified. It is STRONGLY recommended not to use "nightly" in production Chat bots. |
| [EnumOrLiteral](./chat-core.enumorliteral.md) | Produces a union type from the enum passed as a generic which consists of the enum values and the string literals of the enum. |
| [RawResponse](./chat-core.rawresponse.md) | Raw response from Chat API. |
Expand Down
24 changes: 24 additions & 0 deletions docs/chat-core.providechatcore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@yext/chat-core](./chat-core.md) &gt; [ProvideChatCore](./chat-core.providechatcore.md)

## ProvideChatCore() function

Provider for the ChatCore library

**Signature:**

```typescript
export declare function ProvideChatCore(config: ChatConfig): ChatCore;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| config | [ChatConfig](./chat-core.chatconfig.md) | |

**Returns:**

ChatCore

26 changes: 20 additions & 6 deletions etc/chat-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ export interface ChatConfig {
version?: "STAGING" | "PRODUCTION" | number;
}

// @public
export class ChatCore {
constructor(chatConfig: ChatConfig, promptPackage?: ChatPrompt);
getNextMessage(request: MessageRequest): Promise<MessageResponse>;
streamNextMessage(request: MessageRequest): Promise<StreamResponse>;
}
// Warning: (ae-forgotten-export) The symbol "ChatCore_2" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
export type ChatCore = typeof ChatCore_2;

// @public
export type ChatPrompt = "stable" | "nightly";
Expand Down Expand Up @@ -50,6 +48,14 @@ export enum Environment {
SANDBOX = "SANDBOX"
}

// Warning: (ae-internal-missing-underscore) The name "InternalConfig" should be prefixed with an underscore because the declaration is marked as @internal
//
// @internal
export interface InternalConfig {
// (undocumented)
promptPackage?: ChatPrompt;
}

// @public
export interface Message {
responseId?: string;
Expand Down Expand Up @@ -91,6 +97,14 @@ export enum MessageSource {
USER = "USER"
}

// @public
export function ProvideChatCore(config: ChatConfig): ChatCore_2;

// Warning: (ae-internal-missing-underscore) The name "ProvideChatCoreInternal" should be prefixed with an underscore because the declaration is marked as @internal
//
// @internal
export function ProvideChatCoreInternal(config: ChatConfig, internal: InternalConfig): ChatCore_2;

// @public
export type RawResponse = Response | Response_2;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yext/chat-core",
"version": "0.5.4",
"version": "0.6.0",
"description": "Typescript Networking Library for the Yext Chat API",
"main": "./dist/commonjs/index.js",
"module": "./dist/esm/index.js",
Expand Down
13 changes: 9 additions & 4 deletions src/ChatCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { defaultApiVersion } from "./constants";
import { HttpService, HttpServiceImpl } from "./http/HttpService";
import { EndpointsFactory } from "./infra/EndpointsFactory";
import { StreamResponse } from "./infra/StreamResponse";
import { ChatConfig, MessageRequest, MessageResponse } from "./models";
import {
ChatConfig,
InternalConfig,
MessageRequest,
MessageResponse,
} from "./models";
import { Endpoints } from "./models/endpoints/Endpoints";
import {
ApiMessageRequest,
Expand All @@ -15,20 +20,20 @@ import { ApiResponseValidator } from "./validation/ApiResponseValidator";
/**
* The entrypoint to the chat-core library. Provides methods for interacting with Chat API.
*
* @public
* @internal
*/
export class ChatCore {
private chatConfig: ChatConfig;
private httpService: HttpService;
private endpoints: Endpoints;
private promptPackage: ChatPrompt;

constructor(chatConfig: ChatConfig, promptPackage?: ChatPrompt) {
constructor(chatConfig: ChatConfig, internalConfig?: InternalConfig) {
this.chatConfig = chatConfig;
this.httpService = new HttpServiceImpl();
this.endpoints =
chatConfig.endpoints ?? EndpointsFactory.getEndpoints(this.chatConfig);
this.promptPackage = promptPackage ?? "stable";
this.promptPackage = internalConfig?.promptPackage ?? "stable";
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/CoreProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ChatCore } from "./ChatCore";
import { ChatConfig, InternalConfig } from "./models";

/**
* Provider for the ChatCore library
* @public
*/
export function ProvideChatCore(config: ChatConfig): ChatCore {
return new ChatCore(config);
}

/**
* Provider for the ChatCore library with additional internal-only configuration
* @internal
*/
export function ProvideChatCoreInternal(
config: ChatConfig,
internal: InternalConfig
): ChatCore {
return new ChatCore(config, internal);
}
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export { ChatCore } from "./ChatCore";
import { ChatCore as Core } from "./ChatCore";
/**
* @public
*/
export type ChatCore = typeof Core;
export { StreamResponse } from "./infra/StreamResponse";
export { ProvideChatCore, ProvideChatCoreInternal } from "./CoreProvider";
export * from "./models";
9 changes: 9 additions & 0 deletions src/models/ChatConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Region } from "./endpoints/Region";
import { Environment } from "./endpoints/Environment";
import { EnumOrLiteral } from "./utils/EnumOrLiteral";
import { Endpoints } from "./endpoints/Endpoints";
import { ChatPrompt } from "./endpoints/MessageRequest";

/**
* The configuration options for {@link ChatCore}.
Expand Down Expand Up @@ -43,3 +44,11 @@ export interface ChatConfig {
/** Overrides for the URLs which are used when making requests to the Chat API. */
endpoints?: Endpoints;
}

/**
* Experimental or internal-only features for Chat Core
* @internal
*/
export interface InternalConfig {
promptPackage?: ChatPrompt;
}
2 changes: 1 addition & 1 deletion src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { ChatConfig } from "./ChatConfig";
export { ChatConfig, InternalConfig } from "./ChatConfig";

export { Endpoints } from "./endpoints/Endpoints";
export { Environment } from "./endpoints/Environment";
Expand Down
18 changes: 9 additions & 9 deletions tests/ChatCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
MessageRequest,
MessageResponse,
MessageSource,
ProvideChatCore,
} from "../src";
import { ChatCore } from "../src/ChatCore";
import { defaultApiVersion } from "../src/constants";
import { HttpServiceImpl } from "../src/http/HttpService";

Expand Down Expand Up @@ -49,7 +49,7 @@ it("returns message response on successful API response", async () => {
response: expectedMessageResponse,
meta: {},
});
const chatCore = new ChatCore({
const chatCore = ProvideChatCore({
botId: "my-bot",
apiKey: "my-api-key",
version: "STAGING",
Expand Down Expand Up @@ -77,7 +77,7 @@ it("returns rejected promise on a failed API response", async () => {
},
false
);
const chatCore = new ChatCore(defaultConfig);
const chatCore = ProvideChatCore(defaultConfig);
await expect(
chatCore.getNextMessage(mockedMessageRequest)
).rejects.toThrowError(
Expand All @@ -88,7 +88,7 @@ it("returns rejected promise on a failed API response", async () => {
describe("URL and http request construction", () => {
it("sets default endpoint and businessId when not specified for Chat API", async () => {
const httpServiceSpy = mockHttpPost();
const chatCore = new ChatCore(defaultConfig);
const chatCore = ProvideChatCore(defaultConfig);
await chatCore.getNextMessage(mockedMessageRequest);
expect(httpServiceSpy).toHaveBeenCalledWith(
"https://liveapi.yext.com/v2/accounts/me/chat/my-bot/message",
Expand All @@ -100,7 +100,7 @@ describe("URL and http request construction", () => {

it("sets default endpoint and businessId when not specified for Chat Stream API", async () => {
const httpServiceSpy = mockHttpPost();
const chatCore = new ChatCore(defaultConfig);
const chatCore = ProvideChatCore(defaultConfig);
await chatCore.streamNextMessage(mockedMessageRequest);
expect(httpServiceSpy).toHaveBeenCalledWith(
"https://liveapi.yext.com/v2/accounts/me/chat/my-bot/message/streaming",
Expand All @@ -120,7 +120,7 @@ describe("URL and http request construction", () => {

it("sets custom endpoints when specified for Chat API", async () => {
const httpServiceSpy = mockHttpPost();
const chatCore = new ChatCore(configWithEndpoints);
const chatCore = ProvideChatCore(configWithEndpoints);
await chatCore.getNextMessage(mockedMessageRequest);
expect(httpServiceSpy).toHaveBeenCalledWith(
"https://my-custom-domain.com/",
Expand All @@ -139,7 +139,7 @@ describe("URL and http request construction", () => {

it("sets custom endpoints when specified for Chat Stream API", async () => {
const httpServiceSpy = mockHttpPost();
const chatCore = new ChatCore(configWithEndpoints);
const chatCore = ProvideChatCore(configWithEndpoints);
await chatCore.streamNextMessage(mockedMessageRequest);
expect(httpServiceSpy).toHaveBeenCalledWith(
"https://my-custom-stream-domain.com/",
Expand All @@ -163,7 +163,7 @@ describe("URL and http request construction", () => {

it("sets custom version when specified for Chat API", async () => {
const httpServiceSpy = mockHttpPost();
const chatCore = new ChatCore(configWithVersion);
const chatCore = ProvideChatCore(configWithVersion);
await chatCore.getNextMessage(mockedMessageRequest);
expect(httpServiceSpy).toHaveBeenCalledWith(
"https://liveapi.yext.com/v2/accounts/me/chat/my-bot/message",
Expand All @@ -183,7 +183,7 @@ describe("URL and http request construction", () => {

it("sets custom version when specified for Chat Stream API", async () => {
const httpServiceSpy = mockHttpPost();
const chatCore = new ChatCore(configWithVersion);
const chatCore = ProvideChatCore(configWithVersion);
await chatCore.streamNextMessage(mockedMessageRequest);
expect(httpServiceSpy).toHaveBeenCalledWith(
"https://liveapi.yext.com/v2/accounts/me/chat/my-bot/message/streaming",
Expand Down

0 comments on commit dfeafb7

Please sign in to comment.