From dfeafb769f0e4d33884bcecc0403ba9cede98142 Mon Sep 17 00:00:00 2001 From: nbramblett Date: Fri, 4 Aug 2023 16:34:36 -0400 Subject: [PATCH] Add provider layer and hide constructor --- docs/chat-core.chatcore._constructor_.md | 21 --------------- docs/chat-core.chatcore.getnextmessage.md | 28 -------------------- docs/chat-core.chatcore.md | 19 ++----------- docs/chat-core.chatcore.streamnextmessage.md | 28 -------------------- docs/chat-core.md | 8 +++++- docs/chat-core.providechatcore.md | 24 +++++++++++++++++ etc/chat-core.api.md | 26 +++++++++++++----- package.json | 2 +- src/ChatCore.ts | 13 ++++++--- src/CoreProvider.ts | 21 +++++++++++++++ src/index.ts | 7 ++++- src/models/ChatConfig.ts | 9 +++++++ src/models/index.ts | 2 +- tests/ChatCore.test.ts | 18 ++++++------- 14 files changed, 109 insertions(+), 117 deletions(-) delete mode 100644 docs/chat-core.chatcore._constructor_.md delete mode 100644 docs/chat-core.chatcore.getnextmessage.md delete mode 100644 docs/chat-core.chatcore.streamnextmessage.md create mode 100644 docs/chat-core.providechatcore.md create mode 100644 src/CoreProvider.ts diff --git a/docs/chat-core.chatcore._constructor_.md b/docs/chat-core.chatcore._constructor_.md deleted file mode 100644 index 7240301..0000000 --- a/docs/chat-core.chatcore._constructor_.md +++ /dev/null @@ -1,21 +0,0 @@ - - -[Home](./index.md) > [@yext/chat-core](./chat-core.md) > [ChatCore](./chat-core.chatcore.md) > [(constructor)](./chat-core.chatcore._constructor_.md) - -## ChatCore.(constructor) - -Constructs a new instance of the `ChatCore` class - -**Signature:** - -```typescript -constructor(chatConfig: ChatConfig, promptPackage?: ChatPrompt); -``` - -## Parameters - -| Parameter | Type | Description | -| --- | --- | --- | -| chatConfig | [ChatConfig](./chat-core.chatconfig.md) | | -| promptPackage | [ChatPrompt](./chat-core.chatprompt.md) | _(Optional)_ | - diff --git a/docs/chat-core.chatcore.getnextmessage.md b/docs/chat-core.chatcore.getnextmessage.md deleted file mode 100644 index 2595915..0000000 --- a/docs/chat-core.chatcore.getnextmessage.md +++ /dev/null @@ -1,28 +0,0 @@ - - -[Home](./index.md) > [@yext/chat-core](./chat-core.md) > [ChatCore](./chat-core.chatcore.md) > [getNextMessage](./chat-core.chatcore.getnextmessage.md) - -## ChatCore.getNextMessage() method - -Make a request to Chat API to generate the next message. - -**Signature:** - -```typescript -getNextMessage(request: MessageRequest): Promise; -``` - -## Parameters - -| Parameter | Type | Description | -| --- | --- | --- | -| request | [MessageRequest](./chat-core.messagerequest.md) | request to get next message | - -**Returns:** - -Promise<[MessageResponse](./chat-core.messageresponse.md)> - -## Remarks - -If rejected, an Error is returned. - diff --git a/docs/chat-core.chatcore.md b/docs/chat-core.chatcore.md index 17f7056..77a17cb 100644 --- a/docs/chat-core.chatcore.md +++ b/docs/chat-core.chatcore.md @@ -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 ChatCore 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. | - diff --git a/docs/chat-core.chatcore.streamnextmessage.md b/docs/chat-core.chatcore.streamnextmessage.md deleted file mode 100644 index c564265..0000000 --- a/docs/chat-core.chatcore.streamnextmessage.md +++ /dev/null @@ -1,28 +0,0 @@ - - -[Home](./index.md) > [@yext/chat-core](./chat-core.md) > [ChatCore](./chat-core.chatcore.md) > [streamNextMessage](./chat-core.chatcore.streamnextmessage.md) - -## ChatCore.streamNextMessage() method - -Make a request to Chat streaming API to generate the next message and consume its tokens via server-sent events. - -**Signature:** - -```typescript -streamNextMessage(request: MessageRequest): Promise; -``` - -## Parameters - -| Parameter | Type | Description | -| --- | --- | --- | -| request | [MessageRequest](./chat-core.messagerequest.md) | request to get next message | - -**Returns:** - -Promise<[StreamResponse](./chat-core.streamresponse.md)> - -## Remarks - -If rejected, an Error is returned. - diff --git a/docs/chat-core.md b/docs/chat-core.md index 80d2648..5bb7af2 100644 --- a/docs/chat-core.md +++ b/docs/chat-core.md @@ -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 @@ -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 | @@ -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. | diff --git a/docs/chat-core.providechatcore.md b/docs/chat-core.providechatcore.md new file mode 100644 index 0000000..c6bafc2 --- /dev/null +++ b/docs/chat-core.providechatcore.md @@ -0,0 +1,24 @@ + + +[Home](./index.md) > [@yext/chat-core](./chat-core.md) > [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 + diff --git a/etc/chat-core.api.md b/etc/chat-core.api.md index f42e820..801ee0f 100644 --- a/etc/chat-core.api.md +++ b/etc/chat-core.api.md @@ -17,12 +17,10 @@ export interface ChatConfig { version?: "STAGING" | "PRODUCTION" | number; } -// @public -export class ChatCore { - constructor(chatConfig: ChatConfig, promptPackage?: ChatPrompt); - getNextMessage(request: MessageRequest): Promise; - streamNextMessage(request: MessageRequest): Promise; -} +// 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"; @@ -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; @@ -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; diff --git a/package.json b/package.json index a303bb9..8b965e2 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/ChatCore.ts b/src/ChatCore.ts index 66a5ca2..72e4945 100644 --- a/src/ChatCore.ts +++ b/src/ChatCore.ts @@ -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, @@ -15,7 +20,7 @@ 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; @@ -23,12 +28,12 @@ export class ChatCore { 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"; } /** diff --git a/src/CoreProvider.ts b/src/CoreProvider.ts new file mode 100644 index 0000000..d3be08d --- /dev/null +++ b/src/CoreProvider.ts @@ -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); +} diff --git a/src/index.ts b/src/index.ts index c7dcc92..6d42828 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; diff --git a/src/models/ChatConfig.ts b/src/models/ChatConfig.ts index 79e03a3..d98334e 100644 --- a/src/models/ChatConfig.ts +++ b/src/models/ChatConfig.ts @@ -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}. @@ -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; +} diff --git a/src/models/index.ts b/src/models/index.ts index cc35f17..052cb8f 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -1,4 +1,4 @@ -export { ChatConfig } from "./ChatConfig"; +export { ChatConfig, InternalConfig } from "./ChatConfig"; export { Endpoints } from "./endpoints/Endpoints"; export { Environment } from "./endpoints/Environment"; diff --git a/tests/ChatCore.test.ts b/tests/ChatCore.test.ts index 2b215c7..4cd4d82 100644 --- a/tests/ChatCore.test.ts +++ b/tests/ChatCore.test.ts @@ -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"; @@ -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", @@ -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( @@ -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", @@ -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", @@ -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/", @@ -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/", @@ -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", @@ -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",