@botonic/plugin-flow-builder 0.34.2 → 0.35.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/action/fallback.d.ts +1 -1
- package/lib/cjs/action/fallback.js +2 -2
- package/lib/cjs/action/fallback.js.map +1 -1
- package/lib/cjs/action/first-interaction.js +5 -5
- package/lib/cjs/action/first-interaction.js.map +1 -1
- package/lib/cjs/action/index.d.ts +1 -1
- package/lib/cjs/action/index.js +2 -3
- package/lib/cjs/action/index.js.map +1 -1
- package/lib/cjs/action/knowledge-bases.d.ts +1 -1
- package/lib/cjs/action/knowledge-bases.js +2 -2
- package/lib/cjs/action/knowledge-bases.js.map +1 -1
- package/lib/cjs/action/payload.d.ts +1 -1
- package/lib/cjs/action/payload.js +2 -2
- package/lib/cjs/action/payload.js.map +1 -1
- package/lib/cjs/api.d.ts +6 -6
- package/lib/cjs/api.js +24 -24
- package/lib/cjs/api.js.map +1 -1
- package/lib/cjs/content-fields/flow-handoff.js +2 -2
- package/lib/cjs/content-fields/flow-handoff.js.map +1 -1
- package/lib/cjs/content-fields/hubtype-fields/common.d.ts +1 -0
- package/lib/cjs/content-fields/hubtype-fields/index.d.ts +0 -1
- package/lib/cjs/content-fields/hubtype-fields/index.js +0 -1
- package/lib/cjs/content-fields/hubtype-fields/index.js.map +1 -1
- package/lib/cjs/content-fields/hubtype-fields/node-types.d.ts +0 -1
- package/lib/cjs/content-fields/hubtype-fields/node-types.js +0 -1
- package/lib/cjs/content-fields/hubtype-fields/node-types.js.map +1 -1
- package/lib/cjs/content-fields/hubtype-fields/nodes.d.ts +1 -2
- package/lib/cjs/functions/conditional-country.js +1 -1
- package/lib/cjs/functions/conditional-country.js.map +1 -1
- package/lib/cjs/helpers.d.ts +2 -4
- package/lib/cjs/helpers.js +8 -5
- package/lib/cjs/helpers.js.map +1 -1
- package/lib/cjs/index.d.ts +6 -7
- package/lib/cjs/index.js +14 -17
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/tracking.d.ts +0 -1
- package/lib/cjs/tracking.js +0 -1
- package/lib/cjs/tracking.js.map +1 -1
- package/lib/cjs/types.d.ts +6 -8
- package/lib/cjs/types.js.map +1 -1
- package/lib/cjs/user-input/index.d.ts +2 -2
- package/lib/cjs/user-input/index.js +0 -8
- package/lib/cjs/user-input/index.js.map +1 -1
- package/lib/cjs/utils.d.ts +1 -2
- package/lib/cjs/utils.js +2 -2
- package/lib/cjs/utils.js.map +1 -1
- package/lib/esm/action/fallback.d.ts +1 -1
- package/lib/esm/action/fallback.js +2 -2
- package/lib/esm/action/fallback.js.map +1 -1
- package/lib/esm/action/first-interaction.js +5 -5
- package/lib/esm/action/first-interaction.js.map +1 -1
- package/lib/esm/action/index.d.ts +1 -1
- package/lib/esm/action/index.js +2 -3
- package/lib/esm/action/index.js.map +1 -1
- package/lib/esm/action/knowledge-bases.d.ts +1 -1
- package/lib/esm/action/knowledge-bases.js +2 -2
- package/lib/esm/action/knowledge-bases.js.map +1 -1
- package/lib/esm/action/payload.d.ts +1 -1
- package/lib/esm/action/payload.js +2 -2
- package/lib/esm/action/payload.js.map +1 -1
- package/lib/esm/api.d.ts +6 -6
- package/lib/esm/api.js +24 -24
- package/lib/esm/api.js.map +1 -1
- package/lib/esm/content-fields/flow-handoff.js +2 -2
- package/lib/esm/content-fields/flow-handoff.js.map +1 -1
- package/lib/esm/content-fields/hubtype-fields/common.d.ts +1 -0
- package/lib/esm/content-fields/hubtype-fields/index.d.ts +0 -1
- package/lib/esm/content-fields/hubtype-fields/index.js +0 -1
- package/lib/esm/content-fields/hubtype-fields/index.js.map +1 -1
- package/lib/esm/content-fields/hubtype-fields/node-types.d.ts +0 -1
- package/lib/esm/content-fields/hubtype-fields/node-types.js +0 -1
- package/lib/esm/content-fields/hubtype-fields/node-types.js.map +1 -1
- package/lib/esm/content-fields/hubtype-fields/nodes.d.ts +1 -2
- package/lib/esm/functions/conditional-country.js +1 -1
- package/lib/esm/functions/conditional-country.js.map +1 -1
- package/lib/esm/helpers.d.ts +2 -4
- package/lib/esm/helpers.js +8 -5
- package/lib/esm/helpers.js.map +1 -1
- package/lib/esm/index.d.ts +6 -7
- package/lib/esm/index.js +14 -17
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/tracking.d.ts +0 -1
- package/lib/esm/tracking.js +0 -1
- package/lib/esm/tracking.js.map +1 -1
- package/lib/esm/types.d.ts +6 -8
- package/lib/esm/types.js.map +1 -1
- package/lib/esm/user-input/index.d.ts +2 -2
- package/lib/esm/user-input/index.js +0 -8
- package/lib/esm/user-input/index.js.map +1 -1
- package/lib/esm/utils.d.ts +1 -2
- package/lib/esm/utils.js +3 -3
- package/lib/esm/utils.js.map +1 -1
- package/package.json +2 -2
- package/src/action/fallback.ts +2 -5
- package/src/action/first-interaction.ts +3 -6
- package/src/action/index.tsx +2 -3
- package/src/action/knowledge-bases.ts +8 -8
- package/src/action/payload.ts +1 -2
- package/src/api.ts +33 -43
- package/src/content-fields/flow-handoff.tsx +2 -2
- package/src/content-fields/hubtype-fields/common.ts +1 -0
- package/src/content-fields/hubtype-fields/index.ts +0 -1
- package/src/content-fields/hubtype-fields/node-types.ts +0 -1
- package/src/content-fields/hubtype-fields/nodes.ts +0 -2
- package/src/functions/conditional-country.ts +1 -1
- package/src/helpers.ts +17 -11
- package/src/index.ts +19 -20
- package/src/tracking.ts +0 -1
- package/src/types.ts +22 -10
- package/src/user-input/index.ts +1 -11
- package/src/utils.ts +3 -7
- package/lib/cjs/content-fields/hubtype-fields/intent.d.ts +0 -10
- package/lib/cjs/content-fields/hubtype-fields/intent.js +0 -3
- package/lib/cjs/content-fields/hubtype-fields/intent.js.map +0 -1
- package/lib/cjs/user-input/intent.d.ts +0 -4
- package/lib/cjs/user-input/intent.js +0 -45
- package/lib/cjs/user-input/intent.js.map +0 -1
- package/lib/esm/content-fields/hubtype-fields/intent.d.ts +0 -10
- package/lib/esm/content-fields/hubtype-fields/intent.js +0 -2
- package/lib/esm/content-fields/hubtype-fields/intent.js.map +0 -1
- package/lib/esm/user-input/intent.d.ts +0 -4
- package/lib/esm/user-input/intent.js +0 -41
- package/lib/esm/user-input/intent.js.map +0 -1
- package/src/content-fields/hubtype-fields/intent.ts +0 -12
- package/src/user-input/intent.ts +0 -59
package/src/api.ts
CHANGED
|
@@ -7,7 +7,6 @@ import {
|
|
|
7
7
|
HtFallbackNode,
|
|
8
8
|
HtFlowBuilderData,
|
|
9
9
|
HtGoToFlow,
|
|
10
|
-
HtIntentNode,
|
|
11
10
|
HtKeywordNode,
|
|
12
11
|
HtNodeComponent,
|
|
13
12
|
HtNodeLink,
|
|
@@ -125,49 +124,12 @@ export class FlowBuilderApi {
|
|
|
125
124
|
: undefined
|
|
126
125
|
}
|
|
127
126
|
|
|
128
|
-
getIntentNode(input: Input, locale: string): HtIntentNode | undefined {
|
|
129
|
-
try {
|
|
130
|
-
const intentsNodes = this.flow.nodes.filter(
|
|
131
|
-
node => node.type === HtNodeWithContentType.INTENT
|
|
132
|
-
) as HtIntentNode[]
|
|
133
|
-
const inputIntent = input.intent
|
|
134
|
-
if (inputIntent) {
|
|
135
|
-
return intentsNodes.find(
|
|
136
|
-
node =>
|
|
137
|
-
inputIntent && this.nodeContainsIntent(node, inputIntent, locale)
|
|
138
|
-
)
|
|
139
|
-
}
|
|
140
|
-
} catch (error) {
|
|
141
|
-
console.error(`Error getting node by intent '${input.intent}': `, error)
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
return undefined
|
|
145
|
-
}
|
|
146
|
-
|
|
147
127
|
getSmartIntentNodes(): HtSmartIntentNode[] {
|
|
148
128
|
return this.flow.nodes.filter(
|
|
149
129
|
node => node.type === HtNodeWithContentType.SMART_INTENT
|
|
150
130
|
) as HtSmartIntentNode[]
|
|
151
131
|
}
|
|
152
132
|
|
|
153
|
-
private nodeContainsIntent(
|
|
154
|
-
node: HtIntentNode,
|
|
155
|
-
intent: string,
|
|
156
|
-
locale: string
|
|
157
|
-
): boolean {
|
|
158
|
-
return node.content.intents.some(
|
|
159
|
-
i => i.locale === locale && i.values.includes(intent)
|
|
160
|
-
)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
hasMetConfidenceThreshold(
|
|
164
|
-
node: HtIntentNode,
|
|
165
|
-
predictedConfidence: number
|
|
166
|
-
): boolean {
|
|
167
|
-
const nodeConfidence = node.content.confidence / 100
|
|
168
|
-
return predictedConfidence >= nodeConfidence
|
|
169
|
-
}
|
|
170
|
-
|
|
171
133
|
getKeywordNodes(): HtKeywordNode[] {
|
|
172
134
|
return this.flow.nodes.filter(
|
|
173
135
|
node => node.type === HtNodeWithContentType.KEYWORD
|
|
@@ -244,17 +206,45 @@ export class FlowBuilderApi {
|
|
|
244
206
|
return this.flow.is_knowledge_base_active || false
|
|
245
207
|
}
|
|
246
208
|
|
|
247
|
-
getResolvedLocale(
|
|
209
|
+
getResolvedLocale(): string {
|
|
210
|
+
const systemLocale = this.request.getSystemLocale()
|
|
211
|
+
|
|
212
|
+
const locale = this.resolveAsLocale(systemLocale)
|
|
213
|
+
if (locale) {
|
|
214
|
+
return locale
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const language = this.resolveAsLanguage(systemLocale)
|
|
218
|
+
if (language) {
|
|
219
|
+
return language
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return this.resolveAsDefaultLocale()
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
private resolveAsLocale(locale: string): string | undefined {
|
|
248
226
|
if (this.flow.locales.find(flowLocale => flowLocale === locale)) {
|
|
249
227
|
return locale
|
|
250
228
|
}
|
|
251
|
-
|
|
252
|
-
|
|
229
|
+
return undefined
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private resolveAsLanguage(locale?: string): string | undefined {
|
|
233
|
+
const language = locale?.split('-')[0]
|
|
234
|
+
if (
|
|
235
|
+
language &&
|
|
236
|
+
this.flow.locales.find(flowLocale => flowLocale === language)
|
|
237
|
+
) {
|
|
253
238
|
console.log(`locale: ${locale} has been resolved as ${language}`)
|
|
254
239
|
return language
|
|
255
240
|
}
|
|
256
|
-
|
|
257
|
-
|
|
241
|
+
return undefined
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
private resolveAsDefaultLocale(): string {
|
|
245
|
+
console.log(
|
|
246
|
+
`Resolve locale with default locale: ${this.flow.default_locale_code}`
|
|
258
247
|
)
|
|
248
|
+
return this.flow.default_locale_code || 'en'
|
|
259
249
|
}
|
|
260
250
|
}
|
|
@@ -55,8 +55,8 @@ export class FlowHandoff extends ContentFieldsBase {
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
if (this.queue) {
|
|
58
|
-
const language = request.
|
|
59
|
-
const country = request.
|
|
58
|
+
const language = request.getSystemLocale()
|
|
59
|
+
const country = request.getUserCountry()
|
|
60
60
|
|
|
61
61
|
handOffBuilder.withQueue(this.queue.id)
|
|
62
62
|
handOffBuilder.withBotEvent({
|
|
@@ -5,7 +5,6 @@ import { HtFunctionNode } from './function'
|
|
|
5
5
|
import { HtGoToFlow } from './go-to-flow'
|
|
6
6
|
import { HtHandoffNode } from './handoff'
|
|
7
7
|
import { HtImageNode } from './image'
|
|
8
|
-
import { HtIntentNode } from './intent'
|
|
9
8
|
import { HtKeywordNode } from './keyword'
|
|
10
9
|
import { HtKnowledgeBaseNode } from './knowledge-base'
|
|
11
10
|
import { HtPayloadNode } from './payload'
|
|
@@ -23,7 +22,6 @@ export type HtNodeWithContent =
|
|
|
23
22
|
| HtCarouselNode
|
|
24
23
|
| HtHandoffNode
|
|
25
24
|
| HtKeywordNode
|
|
26
|
-
| HtIntentNode
|
|
27
25
|
| HtFunctionNode
|
|
28
26
|
| HtFallbackNode
|
|
29
27
|
| HtWhatsappButtonListNode
|
|
@@ -9,6 +9,6 @@ export function conditionalCountry({
|
|
|
9
9
|
request,
|
|
10
10
|
results,
|
|
11
11
|
}: ConditionalCountryArgs): string {
|
|
12
|
-
const country = request.
|
|
12
|
+
const country = request.getUserCountry()
|
|
13
13
|
return results.find(result => result === country) || 'default'
|
|
14
14
|
}
|
package/src/helpers.ts
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ResolvedPlugins } from '@botonic/core'
|
|
2
2
|
|
|
3
3
|
import BotonicPluginFlowBuilder from './index'
|
|
4
4
|
|
|
5
5
|
const FLOW_BUILDER_PLUGIN_NAME = 'BotonicPluginFlowBuilder'
|
|
6
6
|
|
|
7
|
-
export function getFlowBuilderPlugin(
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
export function getFlowBuilderPlugin(
|
|
8
|
+
plugins: ResolvedPlugins
|
|
9
|
+
): BotonicPluginFlowBuilder {
|
|
10
|
+
const ERROR_MESSAGE = `You must include '@botonic/plugin-flow-builder' in your plugins file.`
|
|
11
|
+
|
|
12
|
+
if (Object.values(plugins).length === 0) {
|
|
13
|
+
throw new Error(ERROR_MESSAGE)
|
|
14
|
+
}
|
|
15
|
+
|
|
10
16
|
const flowBuilderPlugin = Object.values(plugins).find(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if (!flowBuilderPlugin)
|
|
15
|
-
throw new Error(
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
plugin => plugin.constructor.name === FLOW_BUILDER_PLUGIN_NAME
|
|
18
|
+
) as unknown as BotonicPluginFlowBuilder
|
|
19
|
+
|
|
20
|
+
if (!flowBuilderPlugin) {
|
|
21
|
+
throw new Error(ERROR_MESSAGE)
|
|
22
|
+
}
|
|
23
|
+
|
|
18
24
|
return flowBuilderPlugin
|
|
19
25
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
BotContext,
|
|
3
|
+
Plugin,
|
|
4
|
+
PluginPreRequest,
|
|
5
|
+
ResolvedPlugins,
|
|
6
|
+
Session,
|
|
7
|
+
} from '@botonic/core'
|
|
2
8
|
import { ActionRequest } from '@botonic/react'
|
|
3
9
|
import { v7 as uuidv7 } from 'uuid'
|
|
4
10
|
|
|
@@ -51,7 +57,6 @@ export default class BotonicPluginFlowBuilder implements Plugin {
|
|
|
51
57
|
private functions: Record<any, any>
|
|
52
58
|
private currentRequest: PluginPreRequest
|
|
53
59
|
public getAccessToken: (session: Session) => string
|
|
54
|
-
public getLocale: (session: Session) => string
|
|
55
60
|
public trackEvent?: TrackEventFunction
|
|
56
61
|
public getKnowledgeBaseResponse?: KnowledgeBaseFunction
|
|
57
62
|
public smartIntentsConfig: SmartIntentsInferenceConfig
|
|
@@ -61,12 +66,11 @@ export default class BotonicPluginFlowBuilder implements Plugin {
|
|
|
61
66
|
public jsonVersion: FlowBuilderJSONVersion
|
|
62
67
|
public apiUrl: string
|
|
63
68
|
|
|
64
|
-
constructor(options: BotonicPluginFlowBuilderOptions) {
|
|
69
|
+
constructor(options: BotonicPluginFlowBuilderOptions<ResolvedPlugins, any>) {
|
|
65
70
|
this.apiUrl = options.apiUrl || FLOW_BUILDER_API_URL_PROD
|
|
66
71
|
this.jsonVersion = options.jsonVersion || FlowBuilderJSONVersion.LATEST
|
|
67
72
|
this.flow = options.flow
|
|
68
|
-
this.
|
|
69
|
-
this.getAccessToken = resolveGetAccessToken(options)
|
|
73
|
+
this.getAccessToken = resolveGetAccessToken(options.getAccessToken)
|
|
70
74
|
this.trackEvent = options.trackEvent
|
|
71
75
|
this.getKnowledgeBaseResponse = options.getKnowledgeBaseResponse
|
|
72
76
|
this.smartIntentsConfig = {
|
|
@@ -103,8 +107,7 @@ export default class BotonicPluginFlowBuilder implements Plugin {
|
|
|
103
107
|
inputHasTextData(request.input) && !request.input.payload
|
|
104
108
|
|
|
105
109
|
if (checkUserTextInput) {
|
|
106
|
-
const
|
|
107
|
-
const resolvedLocale = this.cmsApi.getResolvedLocale(locale)
|
|
110
|
+
const resolvedLocale = this.cmsApi.getResolvedLocale()
|
|
108
111
|
const nodeByUserInput = await getNodeByUserInput(
|
|
109
112
|
this.cmsApi,
|
|
110
113
|
resolvedLocale,
|
|
@@ -119,7 +122,7 @@ export default class BotonicPluginFlowBuilder implements Plugin {
|
|
|
119
122
|
|
|
120
123
|
private updateRequestBeforeRoutes(request: PluginPreRequest): void {
|
|
121
124
|
if (request.input.payload) {
|
|
122
|
-
request.input.payload = this.
|
|
125
|
+
request.input.payload = this.removeSourceSuffix(request.input.payload)
|
|
123
126
|
|
|
124
127
|
if (this.cmsApi.isBotAction(request.input.payload)) {
|
|
125
128
|
const cmsBotAction = this.cmsApi.getNodeById<HtBotActionNode>(
|
|
@@ -132,7 +135,7 @@ export default class BotonicPluginFlowBuilder implements Plugin {
|
|
|
132
135
|
}
|
|
133
136
|
}
|
|
134
137
|
|
|
135
|
-
private
|
|
138
|
+
private removeSourceSuffix(payload: string): string {
|
|
136
139
|
return payload.split(SOURCE_INFO_SEPARATOR)[0]
|
|
137
140
|
}
|
|
138
141
|
|
|
@@ -142,11 +145,10 @@ export default class BotonicPluginFlowBuilder implements Plugin {
|
|
|
142
145
|
|
|
143
146
|
async getContentsByContentID(
|
|
144
147
|
contentID: string,
|
|
145
|
-
locale: string,
|
|
146
148
|
prevContents?: FlowContent[]
|
|
147
149
|
): Promise<FlowContent[]> {
|
|
148
150
|
const node = this.cmsApi.getNodeByContentID(contentID) as HtNodeWithContent
|
|
149
|
-
return await this.getContentsByNode(node,
|
|
151
|
+
return await this.getContentsByNode(node, prevContents)
|
|
150
152
|
}
|
|
151
153
|
|
|
152
154
|
getUUIDByContentID(contentID: string): string {
|
|
@@ -156,31 +158,28 @@ export default class BotonicPluginFlowBuilder implements Plugin {
|
|
|
156
158
|
|
|
157
159
|
private async getContentsById(
|
|
158
160
|
id: string,
|
|
159
|
-
locale: string,
|
|
160
161
|
prevContents?: FlowContent[]
|
|
161
162
|
): Promise<FlowContent[]> {
|
|
162
163
|
const node = this.cmsApi.getNodeById(id) as HtNodeWithContent
|
|
163
|
-
return await this.getContentsByNode(node,
|
|
164
|
+
return await this.getContentsByNode(node, prevContents)
|
|
164
165
|
}
|
|
165
166
|
|
|
166
|
-
async getStartContents(
|
|
167
|
-
const resolvedLocale = this.cmsApi.getResolvedLocale(locale)
|
|
167
|
+
async getStartContents(): Promise<FlowContent[]> {
|
|
168
168
|
const startNode = this.cmsApi.getStartNode()
|
|
169
169
|
this.currentRequest.session.flow_thread_id = uuidv7()
|
|
170
|
-
return await this.getContentsByNode(startNode
|
|
170
|
+
return await this.getContentsByNode(startNode)
|
|
171
171
|
}
|
|
172
172
|
|
|
173
173
|
async getContentsByNode(
|
|
174
174
|
node: HtNodeWithContent,
|
|
175
|
-
locale: string,
|
|
176
175
|
prevContents?: FlowContent[]
|
|
177
176
|
): Promise<FlowContent[]> {
|
|
178
177
|
const contents = prevContents || []
|
|
179
|
-
const resolvedLocale = this.cmsApi.getResolvedLocale(
|
|
178
|
+
const resolvedLocale = this.cmsApi.getResolvedLocale()
|
|
180
179
|
|
|
181
180
|
if (node.type === HtNodeWithContentType.FUNCTION) {
|
|
182
181
|
const targetId = await this.callFunction(node, resolvedLocale)
|
|
183
|
-
return this.getContentsById(targetId,
|
|
182
|
+
return this.getContentsById(targetId, contents)
|
|
184
183
|
}
|
|
185
184
|
|
|
186
185
|
const content = this.getFlowContent(node, resolvedLocale)
|
|
@@ -195,7 +194,7 @@ export default class BotonicPluginFlowBuilder implements Plugin {
|
|
|
195
194
|
|
|
196
195
|
// TODO: prevent infinite recursive calls
|
|
197
196
|
if (node.follow_up) {
|
|
198
|
-
return this.getContentsById(node.follow_up.id,
|
|
197
|
+
return this.getContentsById(node.follow_up.id, contents)
|
|
199
198
|
}
|
|
200
199
|
|
|
201
200
|
return contents
|
package/src/tracking.ts
CHANGED
package/src/types.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
BotContext,
|
|
3
|
+
PluginPreRequest,
|
|
4
|
+
ResolvedPlugins,
|
|
5
|
+
Session,
|
|
6
|
+
} from '@botonic/core'
|
|
3
7
|
|
|
4
8
|
import { HtFlowBuilderData } from './content-fields/hubtype-fields'
|
|
5
9
|
|
|
@@ -9,27 +13,35 @@ export interface InShadowingConfig {
|
|
|
9
13
|
allowKnowledgeBases: boolean
|
|
10
14
|
}
|
|
11
15
|
|
|
12
|
-
export interface BotonicPluginFlowBuilderOptions
|
|
16
|
+
export interface BotonicPluginFlowBuilderOptions<
|
|
17
|
+
TPlugins extends ResolvedPlugins = ResolvedPlugins,
|
|
18
|
+
TExtraData = any,
|
|
19
|
+
> {
|
|
13
20
|
apiUrl?: string
|
|
14
21
|
jsonVersion?: FlowBuilderJSONVersion
|
|
15
22
|
flow?: HtFlowBuilderData
|
|
16
23
|
customFunctions?: Record<any, any>
|
|
17
|
-
getLocale: (session: Session) => string
|
|
18
24
|
getAccessToken: () => string
|
|
19
|
-
trackEvent?: TrackEventFunction
|
|
20
|
-
getKnowledgeBaseResponse?: KnowledgeBaseFunction
|
|
25
|
+
trackEvent?: TrackEventFunction<TPlugins, TExtraData>
|
|
26
|
+
getKnowledgeBaseResponse?: KnowledgeBaseFunction<TPlugins, TExtraData>
|
|
21
27
|
smartIntentsConfig?: { numSmartIntentsToUse: number }
|
|
22
28
|
inShadowing?: Partial<InShadowingConfig>
|
|
23
29
|
}
|
|
24
30
|
|
|
25
|
-
export type TrackEventFunction
|
|
26
|
-
|
|
31
|
+
export type TrackEventFunction<
|
|
32
|
+
TPlugins extends ResolvedPlugins = ResolvedPlugins,
|
|
33
|
+
TExtraData = any,
|
|
34
|
+
> = (
|
|
35
|
+
request: BotContext<TPlugins, TExtraData>,
|
|
27
36
|
eventAction: string,
|
|
28
37
|
args?: Record<string, any>
|
|
29
38
|
) => Promise<void>
|
|
30
39
|
|
|
31
|
-
export type KnowledgeBaseFunction
|
|
32
|
-
|
|
40
|
+
export type KnowledgeBaseFunction<
|
|
41
|
+
TPlugins extends ResolvedPlugins = ResolvedPlugins,
|
|
42
|
+
TExtraData = any,
|
|
43
|
+
> = (
|
|
44
|
+
request: BotContext<TPlugins, TExtraData>,
|
|
33
45
|
sources: string[],
|
|
34
46
|
instructions: string,
|
|
35
47
|
messageId: string,
|
package/src/user-input/index.ts
CHANGED
|
@@ -2,7 +2,6 @@ import { ActionRequest } from '@botonic/react'
|
|
|
2
2
|
|
|
3
3
|
import { FlowBuilderApi } from '../api'
|
|
4
4
|
import {
|
|
5
|
-
HtIntentNode,
|
|
6
5
|
HtKeywordNode,
|
|
7
6
|
HtSmartIntentNode,
|
|
8
7
|
} from '../content-fields/hubtype-fields'
|
|
@@ -11,7 +10,6 @@ import {
|
|
|
11
10
|
isKeywordsAllowed,
|
|
12
11
|
isSmartIntentsAllowed,
|
|
13
12
|
} from '../utils'
|
|
14
|
-
import { getIntentNodeByInput } from './intent'
|
|
15
13
|
import { KeywordMatcher } from './keyword'
|
|
16
14
|
import { SmartIntentsApi, SmartIntentsInferenceConfig } from './smart-intent'
|
|
17
15
|
|
|
@@ -20,7 +18,7 @@ export async function getNodeByUserInput(
|
|
|
20
18
|
locale: string,
|
|
21
19
|
request: ActionRequest,
|
|
22
20
|
smartIntentsConfig: SmartIntentsInferenceConfig
|
|
23
|
-
): Promise<HtSmartIntentNode |
|
|
21
|
+
): Promise<HtSmartIntentNode | HtKeywordNode | undefined> {
|
|
24
22
|
if (inputHasTextData(request.input)) {
|
|
25
23
|
if (isKeywordsAllowed(request)) {
|
|
26
24
|
const keywordMatcher = new KeywordMatcher({
|
|
@@ -47,14 +45,6 @@ export async function getNodeByUserInput(
|
|
|
47
45
|
return smartIntentNode
|
|
48
46
|
}
|
|
49
47
|
}
|
|
50
|
-
|
|
51
|
-
// TODO: Remove this because frontend no allow create intents babel
|
|
52
|
-
if (isSmartIntentsAllowed(request)) {
|
|
53
|
-
const intentNode = await getIntentNodeByInput(cmsApi, locale, request)
|
|
54
|
-
if (intentNode) {
|
|
55
|
-
return intentNode
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
48
|
}
|
|
59
49
|
|
|
60
50
|
return undefined
|
package/src/utils.ts
CHANGED
|
@@ -2,11 +2,7 @@ import { INPUT, Input, Session } from '@botonic/core'
|
|
|
2
2
|
import { ActionRequest } from '@botonic/react'
|
|
3
3
|
|
|
4
4
|
import { getFlowBuilderPlugin } from './helpers'
|
|
5
|
-
import {
|
|
6
|
-
BotonicPluginFlowBuilderOptions,
|
|
7
|
-
InShadowingConfig,
|
|
8
|
-
ProcessEnvNodeEnvs,
|
|
9
|
-
} from './types'
|
|
5
|
+
import { InShadowingConfig, ProcessEnvNodeEnvs } from './types'
|
|
10
6
|
|
|
11
7
|
function getAccessTokenFromSession(session: Session): string {
|
|
12
8
|
if (!session._access_token) {
|
|
@@ -16,13 +12,13 @@ function getAccessTokenFromSession(session: Session): string {
|
|
|
16
12
|
}
|
|
17
13
|
|
|
18
14
|
export function resolveGetAccessToken(
|
|
19
|
-
|
|
15
|
+
getAccessToken: (session: Session) => string
|
|
20
16
|
): (session: Session) => string {
|
|
21
17
|
switch (process.env.NODE_ENV) {
|
|
22
18
|
case ProcessEnvNodeEnvs.PRODUCTION:
|
|
23
19
|
return getAccessTokenFromSession
|
|
24
20
|
case ProcessEnvNodeEnvs.DEVELOPMENT:
|
|
25
|
-
return
|
|
21
|
+
return getAccessToken
|
|
26
22
|
default:
|
|
27
23
|
throw new Error('No method defined for getting access token')
|
|
28
24
|
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { HtBaseNode, HtInputLocale, HtTextLocale } from './common';
|
|
2
|
-
import { HtNodeWithContentType } from './node-types';
|
|
3
|
-
export interface HtIntentNode extends HtBaseNode {
|
|
4
|
-
type: HtNodeWithContentType.INTENT;
|
|
5
|
-
content: {
|
|
6
|
-
title: HtTextLocale[];
|
|
7
|
-
intents: HtInputLocale[];
|
|
8
|
-
confidence: number;
|
|
9
|
-
};
|
|
10
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"intent.js","sourceRoot":"","sources":["../../../../src/content-fields/hubtype-fields/intent.ts"],"names":[],"mappings":""}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { ActionRequest } from '@botonic/react';
|
|
2
|
-
import { FlowBuilderApi } from '../api';
|
|
3
|
-
import { HtIntentNode } from '../content-fields/hubtype-fields';
|
|
4
|
-
export declare function getIntentNodeByInput(cmsApi: FlowBuilderApi, locale: string, request: ActionRequest): Promise<HtIntentNode | undefined>;
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getIntentNodeByInput = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const core_1 = require("@botonic/core");
|
|
6
|
-
const tracking_1 = require("../tracking");
|
|
7
|
-
function getIntentNodeByInput(cmsApi, locale, request) {
|
|
8
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
9
|
-
const intentNode = cmsApi.getIntentNode(request.input, locale);
|
|
10
|
-
if (request.input.confidence && request.input.intent && intentNode) {
|
|
11
|
-
yield trackIntentEvent(request, intentNode);
|
|
12
|
-
if (isIntentValid(intentNode, request, cmsApi)) {
|
|
13
|
-
const targetPayload = cmsApi.getPayload(intentNode.target);
|
|
14
|
-
request.input.nluResolution = {
|
|
15
|
-
type: core_1.NluType.Intent,
|
|
16
|
-
matchedValue: request.input.intent,
|
|
17
|
-
payload: targetPayload,
|
|
18
|
-
};
|
|
19
|
-
return intentNode;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
return undefined;
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
exports.getIntentNodeByInput = getIntentNodeByInput;
|
|
26
|
-
function trackIntentEvent(request, intentNode) {
|
|
27
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
28
|
-
const eventArgs = {
|
|
29
|
-
nluIntentLabel: request.input.intent,
|
|
30
|
-
nluIntentConfidence: request.input.confidence,
|
|
31
|
-
nluIntentThreshold: intentNode === null || intentNode === void 0 ? void 0 : intentNode.content.confidence,
|
|
32
|
-
nluIntentMessageId: request.input.message_id,
|
|
33
|
-
userInput: request.input.data,
|
|
34
|
-
flowThreadId: request.session.flow_thread_id,
|
|
35
|
-
flowId: intentNode.flow_id,
|
|
36
|
-
flowNodeId: intentNode.id,
|
|
37
|
-
};
|
|
38
|
-
yield (0, tracking_1.trackEvent)(request, tracking_1.EventAction.Intent, eventArgs);
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
function isIntentValid(intentNode, request, cmsApi) {
|
|
42
|
-
return (request.input.confidence &&
|
|
43
|
-
cmsApi.hasMetConfidenceThreshold(intentNode, request.input.confidence));
|
|
44
|
-
}
|
|
45
|
-
//# sourceMappingURL=intent.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"intent.js","sourceRoot":"","sources":["../../../src/user-input/intent.ts"],"names":[],"mappings":";;;;AAAA,wCAAuC;AAKvC,0CAAqD;AAErD,SAAsB,oBAAoB,CACxC,MAAsB,EACtB,MAAc,EACd,OAAsB;;QAEtB,MAAM,UAAU,GAAG,MAAM,CAAC,aAAa,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;QAE9D,IAAI,OAAO,CAAC,KAAK,CAAC,UAAU,IAAI,OAAO,CAAC,KAAK,CAAC,MAAM,IAAI,UAAU,EAAE;YAClE,MAAM,gBAAgB,CAAC,OAAO,EAAE,UAAU,CAAC,CAAA;YAE3C,IAAI,aAAa,CAAC,UAAU,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE;gBAC9C,MAAM,aAAa,GAAG,MAAM,CAAC,UAAU,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;gBAE1D,OAAO,CAAC,KAAK,CAAC,aAAa,GAAG;oBAC5B,IAAI,EAAE,cAAO,CAAC,MAAM;oBACpB,YAAY,EAAE,OAAO,CAAC,KAAK,CAAC,MAAM;oBAClC,OAAO,EAAE,aAAa;iBACvB,CAAA;gBACD,OAAO,UAAU,CAAA;aAClB;SACF;QAED,OAAO,SAAS,CAAA;IAClB,CAAC;CAAA;AAvBD,oDAuBC;AAED,SAAe,gBAAgB,CAC7B,OAAsB,EACtB,UAAwB;;QAExB,MAAM,SAAS,GAAG;YAChB,cAAc,EAAE,OAAO,CAAC,KAAK,CAAC,MAAM;YACpC,mBAAmB,EAAE,OAAO,CAAC,KAAK,CAAC,UAAU;YAC7C,kBAAkB,EAAE,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAE,OAAO,CAAC,UAAU;YAClD,kBAAkB,EAAE,OAAO,CAAC,KAAK,CAAC,UAAU;YAC5C,SAAS,EAAE,OAAO,CAAC,KAAK,CAAC,IAAI;YAC7B,YAAY,EAAE,OAAO,CAAC,OAAO,CAAC,cAAc;YAC5C,MAAM,EAAE,UAAU,CAAC,OAAO;YAC1B,UAAU,EAAE,UAAU,CAAC,EAAE;SAC1B,CAAA;QACD,MAAM,IAAA,qBAAU,EAAC,OAAO,EAAE,sBAAW,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;IAC1D,CAAC;CAAA;AAED,SAAS,aAAa,CACpB,UAAwB,EACxB,OAAsB,EACtB,MAAsB;IAEtB,OAAO,CACL,OAAO,CAAC,KAAK,CAAC,UAAU;QACxB,MAAM,CAAC,yBAAyB,CAAC,UAAU,EAAE,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,CACvE,CAAA;AACH,CAAC"}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { HtBaseNode, HtInputLocale, HtTextLocale } from './common';
|
|
2
|
-
import { HtNodeWithContentType } from './node-types';
|
|
3
|
-
export interface HtIntentNode extends HtBaseNode {
|
|
4
|
-
type: HtNodeWithContentType.INTENT;
|
|
5
|
-
content: {
|
|
6
|
-
title: HtTextLocale[];
|
|
7
|
-
intents: HtInputLocale[];
|
|
8
|
-
confidence: number;
|
|
9
|
-
};
|
|
10
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"intent.js","sourceRoot":"","sources":["../../../../src/content-fields/hubtype-fields/intent.ts"],"names":[],"mappings":""}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { ActionRequest } from '@botonic/react';
|
|
2
|
-
import { FlowBuilderApi } from '../api';
|
|
3
|
-
import { HtIntentNode } from '../content-fields/hubtype-fields';
|
|
4
|
-
export declare function getIntentNodeByInput(cmsApi: FlowBuilderApi, locale: string, request: ActionRequest): Promise<HtIntentNode | undefined>;
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { __awaiter } from "tslib";
|
|
2
|
-
import { NluType } from '@botonic/core';
|
|
3
|
-
import { EventAction, trackEvent } from '../tracking';
|
|
4
|
-
export function getIntentNodeByInput(cmsApi, locale, request) {
|
|
5
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
6
|
-
const intentNode = cmsApi.getIntentNode(request.input, locale);
|
|
7
|
-
if (request.input.confidence && request.input.intent && intentNode) {
|
|
8
|
-
yield trackIntentEvent(request, intentNode);
|
|
9
|
-
if (isIntentValid(intentNode, request, cmsApi)) {
|
|
10
|
-
const targetPayload = cmsApi.getPayload(intentNode.target);
|
|
11
|
-
request.input.nluResolution = {
|
|
12
|
-
type: NluType.Intent,
|
|
13
|
-
matchedValue: request.input.intent,
|
|
14
|
-
payload: targetPayload,
|
|
15
|
-
};
|
|
16
|
-
return intentNode;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
return undefined;
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
function trackIntentEvent(request, intentNode) {
|
|
23
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
-
const eventArgs = {
|
|
25
|
-
nluIntentLabel: request.input.intent,
|
|
26
|
-
nluIntentConfidence: request.input.confidence,
|
|
27
|
-
nluIntentThreshold: intentNode === null || intentNode === void 0 ? void 0 : intentNode.content.confidence,
|
|
28
|
-
nluIntentMessageId: request.input.message_id,
|
|
29
|
-
userInput: request.input.data,
|
|
30
|
-
flowThreadId: request.session.flow_thread_id,
|
|
31
|
-
flowId: intentNode.flow_id,
|
|
32
|
-
flowNodeId: intentNode.id,
|
|
33
|
-
};
|
|
34
|
-
yield trackEvent(request, EventAction.Intent, eventArgs);
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
function isIntentValid(intentNode, request, cmsApi) {
|
|
38
|
-
return (request.input.confidence &&
|
|
39
|
-
cmsApi.hasMetConfidenceThreshold(intentNode, request.input.confidence));
|
|
40
|
-
}
|
|
41
|
-
//# sourceMappingURL=intent.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"intent.js","sourceRoot":"","sources":["../../../src/user-input/intent.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAKvC,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAErD,MAAM,UAAgB,oBAAoB,CACxC,MAAsB,EACtB,MAAc,EACd,OAAsB;;QAEtB,MAAM,UAAU,GAAG,MAAM,CAAC,aAAa,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;QAE9D,IAAI,OAAO,CAAC,KAAK,CAAC,UAAU,IAAI,OAAO,CAAC,KAAK,CAAC,MAAM,IAAI,UAAU,EAAE;YAClE,MAAM,gBAAgB,CAAC,OAAO,EAAE,UAAU,CAAC,CAAA;YAE3C,IAAI,aAAa,CAAC,UAAU,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE;gBAC9C,MAAM,aAAa,GAAG,MAAM,CAAC,UAAU,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;gBAE1D,OAAO,CAAC,KAAK,CAAC,aAAa,GAAG;oBAC5B,IAAI,EAAE,OAAO,CAAC,MAAM;oBACpB,YAAY,EAAE,OAAO,CAAC,KAAK,CAAC,MAAM;oBAClC,OAAO,EAAE,aAAa;iBACvB,CAAA;gBACD,OAAO,UAAU,CAAA;aAClB;SACF;QAED,OAAO,SAAS,CAAA;IAClB,CAAC;CAAA;AAED,SAAe,gBAAgB,CAC7B,OAAsB,EACtB,UAAwB;;QAExB,MAAM,SAAS,GAAG;YAChB,cAAc,EAAE,OAAO,CAAC,KAAK,CAAC,MAAM;YACpC,mBAAmB,EAAE,OAAO,CAAC,KAAK,CAAC,UAAU;YAC7C,kBAAkB,EAAE,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAE,OAAO,CAAC,UAAU;YAClD,kBAAkB,EAAE,OAAO,CAAC,KAAK,CAAC,UAAU;YAC5C,SAAS,EAAE,OAAO,CAAC,KAAK,CAAC,IAAI;YAC7B,YAAY,EAAE,OAAO,CAAC,OAAO,CAAC,cAAc;YAC5C,MAAM,EAAE,UAAU,CAAC,OAAO;YAC1B,UAAU,EAAE,UAAU,CAAC,EAAE;SAC1B,CAAA;QACD,MAAM,UAAU,CAAC,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;IAC1D,CAAC;CAAA;AAED,SAAS,aAAa,CACpB,UAAwB,EACxB,OAAsB,EACtB,MAAsB;IAEtB,OAAO,CACL,OAAO,CAAC,KAAK,CAAC,UAAU;QACxB,MAAM,CAAC,yBAAyB,CAAC,UAAU,EAAE,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,CACvE,CAAA;AACH,CAAC"}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { HtBaseNode, HtInputLocale, HtTextLocale } from './common'
|
|
2
|
-
import { HtNodeWithContentType } from './node-types'
|
|
3
|
-
|
|
4
|
-
// TODO: Remove this because frontend no allow create intents babel
|
|
5
|
-
export interface HtIntentNode extends HtBaseNode {
|
|
6
|
-
type: HtNodeWithContentType.INTENT
|
|
7
|
-
content: {
|
|
8
|
-
title: HtTextLocale[]
|
|
9
|
-
intents: HtInputLocale[]
|
|
10
|
-
confidence: number
|
|
11
|
-
}
|
|
12
|
-
}
|