Skip to content

Commit

Permalink
Add context docs
Browse files Browse the repository at this point in the history
  • Loading branch information
peterszerzo committed Mar 22, 2024
1 parent 0b64f85 commit bcc532e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
12 changes: 6 additions & 6 deletions packages/chat-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,25 @@ convo.sendText("hello");

The package exports a main function called `createConversation`, which is called with the bot configuration and returns a conversation handler object.

The conversation handler has the following methods:
The conversation handler has the following methods. For each send method, conversation context can be optionally specified as a second argument.

#### `sendText: (text: string) => void`
#### `sendText: (text: string, context?: Record<string, unknown>) => void`

Send a simple text to your bot.

#### `sendChoice: (choiceId: string) => void`
#### `sendChoice: (choiceId: string, context?: Record<string, unknown>) => void`

Your bot may send a list of choices to choose from, each with a `choiceText` and a `choiceId` field. You can use `choiceText` as button labels, and include the `choiceId` in this method when sending responses.

#### `sendSlots: (slots: Array<{ slotId: string; value: unknown }>) => void`
#### `sendSlots: (slots: Record<string, unknown>, context?: Record<string, unknown>) => void`

Send slot values directly through custom widgets such as interactive maps.

#### `sendIntent: (intentId: string) => void`
#### `sendIntent: (intentId: string, context?: Record<string, unknown>) => void`

Trigger a specific intent. The most common use of this method is to show welcome messages by sending the `NLX.Welcome` intent.

#### `sendStructured: (request: StructuredRequest) => void`
#### `sendStructured: (request: StructuredRequest, context?: Record<string, unknown>) => void`

Send a combination of choice, slots and intent ID in one request.

Expand Down
5 changes: 5 additions & 0 deletions packages/website/src/components/ChatConfiguration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ export const BehaviorEditor: FC<{
label: "Send custom intent after a period of inactivity",
id: "custom",
},
{
value: Behavior.InitializeWithContext,
label: "Initialize conversation with context",
id: "context",
},
{
value: Behavior.UseSessionStorage,
label: "Retain conversation through refreshes (SessionStorage)",
Expand Down
12 changes: 6 additions & 6 deletions packages/website/src/content/05-02-headless-api-reference.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ ${coreSetupSnippet}
## Handler API
The conversation handler has the following methods:
The conversation handler has the following methods. For each send method, conversation context can be optionally specified as a second argument.
### \`sendText: (text: string) => void\`
### \`sendText: (text: string, context?: Record<string, unknown>) => void\`
Send a simple text to your bot.
### \`sendChoice: (choiceId: string) => void\`
### \`sendChoice: (choiceId: string, context?: Record<string, unknown>) => void\`
Your bot may send a list of choices to choose from, each with a \`choiceText\` and a \`choiceId\` field. You can use \`choiceText\` as button labels, and include the \`choiceId\` in this method when sending responses.
### \`sendSlots: (slots: Record<string, any>) => void\`
### \`sendSlots: (slots: Record<string, unknown>, context?: Record<string, unknown>) => void\`
Send slot values directly through custom widgets such as interactive maps.
### \`sendIntent: (intentId: string) => void\`
### \`sendIntent: (intentId: string, context?: Record<string, unknown>) => void\`
Trigger a specific intent. The most common use of this method is to show welcome messages by sending the \`NLX.Welcome\` intent.
### \`sendStructured: (request: StructuredRequest) => void\`
### \`sendStructured: (request: StructuredRequest, context?: Record<string, unknown>) => void\`
Send a combination of choice, slots, and intent ID in one request.
Expand Down
24 changes: 23 additions & 1 deletion packages/website/src/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum Behavior {
Simple,
WelcomeIntentOnOpen,
CustomIntentOnInactivity,
InitializeWithContext,
UseSessionStorage,
UseLocalStorage,
}
Expand Down Expand Up @@ -168,6 +169,17 @@ storeIn: "localStorage",
// CUSTOM BEHAVIOR SNIPPET
${sendWelcomeOnTimeoutSnippet}
// CUSTOM BEHAVIOR SNIPPET END`,
)
: ""
}${
behavior === Behavior.InitializeWithContext
? indentBy(
" ",
`
// CUSTOM BEHAVIOR SNIPPET
${initializeWithContextSnippet}
// CUSTOM BEHAVIOR SNIPPET END`,
)
: ""
Expand All @@ -186,6 +198,14 @@ export const sendWelcomeOnTimeoutSnippet = `setTimeout(() => {
}
}, 16000);`;

export const initializeWithContextSnippet = `const conversationHandler = widget.getConversationHandler();
const context = {
firstName: "Joe"
};
conversationHandler.sendWelcomeIntent(context);`;

export const customWidgetSnippet = `
import { useChat } from "@nlxai/chat-react";
Expand Down Expand Up @@ -620,7 +640,9 @@ type ScriptTagsType = Record<keyof typeof umdScriptSrc, string>;
export const umdScriptTags = Object.keys(umdScriptSrc).reduce<ScriptTagsType>(
(acc, key) => ({
...acc,
[key]: `<script defer src="${umdScriptSrc[key as keyof typeof umdScriptSrc]}"></script>`,
[key]: `<script defer src="${
umdScriptSrc[key as keyof typeof umdScriptSrc]
}"></script>`,
}),
{} as ScriptTagsType,
);
Expand Down

0 comments on commit bcc532e

Please sign in to comment.