@botonic/plugin-flow-builder 0.54.0 → 0.55.0
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/first-interaction.js +1 -11
- package/lib/cjs/action/first-interaction.js.map +1 -1
- package/lib/cjs/action/get-contents.js +0 -5
- package/lib/cjs/action/get-contents.js.map +1 -1
- package/lib/cjs/api.d.ts +0 -6
- package/lib/cjs/api.js +0 -19
- package/lib/cjs/api.js.map +1 -1
- package/lib/cjs/constants.d.ts +0 -1
- package/lib/cjs/constants.js +1 -2
- package/lib/cjs/constants.js.map +1 -1
- package/lib/cjs/content-fields/hubtype-fields/common.d.ts +0 -1
- package/lib/cjs/content-fields/hubtype-fields/fallback.d.ts +0 -2
- 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/content-fields/index.d.ts +2 -4
- package/lib/cjs/content-fields/index.js +1 -4
- package/lib/cjs/content-fields/index.js.map +1 -1
- package/lib/cjs/flow-factory.js +0 -2
- package/lib/cjs/flow-factory.js.map +1 -1
- package/lib/cjs/index.d.ts +1 -2
- package/lib/cjs/index.js +0 -2
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/tracking.js +1 -2
- package/lib/cjs/tracking.js.map +1 -1
- package/lib/cjs/types.d.ts +1 -4
- package/lib/cjs/types.js.map +1 -1
- package/lib/cjs/utils/input.d.ts +0 -1
- package/lib/cjs/utils/input.js +0 -4
- package/lib/cjs/utils/input.js.map +1 -1
- package/lib/esm/action/first-interaction.js +1 -11
- package/lib/esm/action/first-interaction.js.map +1 -1
- package/lib/esm/action/get-contents.js +0 -5
- package/lib/esm/action/get-contents.js.map +1 -1
- package/lib/esm/api.d.ts +0 -6
- package/lib/esm/api.js +0 -19
- package/lib/esm/api.js.map +1 -1
- package/lib/esm/constants.d.ts +0 -1
- package/lib/esm/constants.js +1 -2
- package/lib/esm/constants.js.map +1 -1
- package/lib/esm/content-fields/hubtype-fields/common.d.ts +0 -1
- package/lib/esm/content-fields/hubtype-fields/fallback.d.ts +0 -2
- 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/content-fields/index.d.ts +2 -4
- package/lib/esm/content-fields/index.js +1 -4
- package/lib/esm/content-fields/index.js.map +1 -1
- package/lib/esm/flow-factory.js +0 -2
- package/lib/esm/flow-factory.js.map +1 -1
- package/lib/esm/index.d.ts +1 -2
- package/lib/esm/index.js +0 -2
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/tracking.js +1 -2
- package/lib/esm/tracking.js.map +1 -1
- package/lib/esm/types.d.ts +1 -4
- package/lib/esm/types.js.map +1 -1
- package/lib/esm/utils/input.d.ts +0 -1
- package/lib/esm/utils/input.js +0 -4
- package/lib/esm/utils/input.js.map +1 -1
- package/package.json +2 -2
- package/src/action/first-interaction.ts +1 -13
- package/src/action/get-contents.ts +0 -5
- package/src/api.ts +1 -35
- package/src/constants.ts +0 -1
- package/src/content-fields/hubtype-fields/common.ts +0 -1
- package/src/content-fields/hubtype-fields/fallback.ts +0 -2
- 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/content-fields/index.ts +0 -8
- package/src/flow-factory.ts +0 -4
- package/src/index.ts +0 -4
- package/src/tracking.ts +1 -4
- package/src/types.ts +0 -14
- package/src/utils/input.ts +0 -4
- package/lib/cjs/action/knowledge-bases.d.ts +0 -3
- package/lib/cjs/action/knowledge-bases.js +0 -85
- package/lib/cjs/action/knowledge-bases.js.map +0 -1
- package/lib/cjs/content-fields/flow-knowledge-base.d.ts +0 -21
- package/lib/cjs/content-fields/flow-knowledge-base.js +0 -44
- package/lib/cjs/content-fields/flow-knowledge-base.js.map +0 -1
- package/lib/cjs/content-fields/hubtype-fields/knowledge-base.d.ts +0 -17
- package/lib/cjs/content-fields/hubtype-fields/knowledge-base.js +0 -3
- package/lib/cjs/content-fields/hubtype-fields/knowledge-base.js.map +0 -1
- package/lib/esm/action/knowledge-bases.d.ts +0 -3
- package/lib/esm/action/knowledge-bases.js +0 -85
- package/lib/esm/action/knowledge-bases.js.map +0 -1
- package/lib/esm/content-fields/flow-knowledge-base.d.ts +0 -21
- package/lib/esm/content-fields/flow-knowledge-base.js +0 -44
- package/lib/esm/content-fields/flow-knowledge-base.js.map +0 -1
- package/lib/esm/content-fields/hubtype-fields/knowledge-base.d.ts +0 -17
- package/lib/esm/content-fields/hubtype-fields/knowledge-base.js +0 -3
- package/lib/esm/content-fields/hubtype-fields/knowledge-base.js.map +0 -1
- package/src/action/knowledge-bases.ts +0 -164
- package/src/content-fields/flow-knowledge-base.tsx +0 -58
- package/src/content-fields/hubtype-fields/knowledge-base.ts +0 -18
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { HtBaseNode } from './common';
|
|
2
|
-
import type { HtNodeWithContentType } from './node-types';
|
|
3
|
-
interface SourceData {
|
|
4
|
-
id: string;
|
|
5
|
-
name: string;
|
|
6
|
-
}
|
|
7
|
-
export interface HtKnowledgeBaseNode extends HtBaseNode {
|
|
8
|
-
type: HtNodeWithContentType.KNOWLEDGE_BASE;
|
|
9
|
-
content: {
|
|
10
|
-
feedback_enabled: boolean;
|
|
11
|
-
sources_data: SourceData[];
|
|
12
|
-
instructions: string;
|
|
13
|
-
has_memory: boolean;
|
|
14
|
-
memory_length?: number;
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"knowledge-base.js","sourceRoot":"","sources":["../../../../src/content-fields/hubtype-fields/knowledge-base.ts"],"names":[],"mappings":""}
|
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type BotContext,
|
|
3
|
-
EventAction,
|
|
4
|
-
type EventKnowledgeBase,
|
|
5
|
-
type KnowledgeBasesResponse,
|
|
6
|
-
KnowledgebaseFailReason,
|
|
7
|
-
type ResolvedPlugins,
|
|
8
|
-
} from '@botonic/core'
|
|
9
|
-
|
|
10
|
-
import {
|
|
11
|
-
DISABLED_MEMORY_LENGTH,
|
|
12
|
-
type FlowContent,
|
|
13
|
-
FlowKnowledgeBase,
|
|
14
|
-
} from '../content-fields'
|
|
15
|
-
import {
|
|
16
|
-
getCommonFlowContentEventArgsForContentId,
|
|
17
|
-
trackEvent,
|
|
18
|
-
} from '../tracking'
|
|
19
|
-
import type { KnowledgeBaseFunction } from '../types'
|
|
20
|
-
import {
|
|
21
|
-
inputHasTextOrTranscript,
|
|
22
|
-
isKnowledgeBasesAllowed,
|
|
23
|
-
} from '../utils/input'
|
|
24
|
-
import type { FlowBuilderContext } from './context'
|
|
25
|
-
|
|
26
|
-
export async function getContentsByKnowledgeBase({
|
|
27
|
-
cmsApi,
|
|
28
|
-
flowBuilderPlugin,
|
|
29
|
-
request,
|
|
30
|
-
}: FlowBuilderContext): Promise<FlowContent[]> {
|
|
31
|
-
if (isKnowledgeBasesAllowed(request)) {
|
|
32
|
-
const startNodeKnowledgeBaseFlow = cmsApi.getStartNodeKnowledgeBaseFlow()
|
|
33
|
-
const isKnowledgeBaseEnabled = cmsApi.isKnowledgeBaseEnabled()
|
|
34
|
-
|
|
35
|
-
if (!startNodeKnowledgeBaseFlow || !isKnowledgeBaseEnabled) {
|
|
36
|
-
return []
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const contents = await flowBuilderPlugin.getContentsByNode(
|
|
40
|
-
startNodeKnowledgeBaseFlow
|
|
41
|
-
)
|
|
42
|
-
|
|
43
|
-
const knowledgeBaseContent = contents.find(
|
|
44
|
-
content => content instanceof FlowKnowledgeBase
|
|
45
|
-
) as FlowKnowledgeBase
|
|
46
|
-
|
|
47
|
-
if (!knowledgeBaseContent) {
|
|
48
|
-
return contents
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const sourceIds = knowledgeBaseContent.sourcesData.map(source => source.id)
|
|
52
|
-
|
|
53
|
-
if (
|
|
54
|
-
flowBuilderPlugin.getKnowledgeBaseResponse &&
|
|
55
|
-
inputHasTextOrTranscript(request.input) &&
|
|
56
|
-
sourceIds.length > 0
|
|
57
|
-
) {
|
|
58
|
-
const contentsWithKnowledgeResponse =
|
|
59
|
-
await getContentsWithKnowledgeResponse(
|
|
60
|
-
flowBuilderPlugin.getKnowledgeBaseResponse,
|
|
61
|
-
request,
|
|
62
|
-
contents,
|
|
63
|
-
knowledgeBaseContent
|
|
64
|
-
)
|
|
65
|
-
|
|
66
|
-
if (contentsWithKnowledgeResponse) {
|
|
67
|
-
return contentsWithKnowledgeResponse
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return []
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
async function getContentsWithKnowledgeResponse<
|
|
76
|
-
T extends ResolvedPlugins = ResolvedPlugins,
|
|
77
|
-
>(
|
|
78
|
-
getKnowledgeBaseResponse: KnowledgeBaseFunction<T>,
|
|
79
|
-
request: BotContext<T>,
|
|
80
|
-
contents: FlowContent[],
|
|
81
|
-
knowledgeBaseContent: FlowKnowledgeBase
|
|
82
|
-
): Promise<FlowContent[] | undefined> {
|
|
83
|
-
const sourceIds = knowledgeBaseContent.sourcesData.map(source => source.id)
|
|
84
|
-
const instructions = knowledgeBaseContent.instructions
|
|
85
|
-
const messageId = request.input.message_id
|
|
86
|
-
const memoryLength = knowledgeBaseContent.hasMemory
|
|
87
|
-
? knowledgeBaseContent.memoryLength
|
|
88
|
-
: DISABLED_MEMORY_LENGTH
|
|
89
|
-
|
|
90
|
-
const knowledgeBaseResponse = await getKnowledgeBaseResponse(
|
|
91
|
-
request,
|
|
92
|
-
sourceIds,
|
|
93
|
-
instructions,
|
|
94
|
-
messageId,
|
|
95
|
-
memoryLength
|
|
96
|
-
)
|
|
97
|
-
await trackKnowledgeBase(knowledgeBaseResponse, request, knowledgeBaseContent)
|
|
98
|
-
|
|
99
|
-
if (
|
|
100
|
-
!knowledgeBaseResponse.hasKnowledge ||
|
|
101
|
-
!knowledgeBaseResponse.isFaithful
|
|
102
|
-
) {
|
|
103
|
-
return undefined
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return updateContentsWithResponse(contents, knowledgeBaseResponse)
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function updateContentsWithResponse(
|
|
110
|
-
contents: FlowContent[],
|
|
111
|
-
response: KnowledgeBasesResponse
|
|
112
|
-
): FlowContent[] {
|
|
113
|
-
return contents.map(content => {
|
|
114
|
-
if (content instanceof FlowKnowledgeBase) {
|
|
115
|
-
content.text = response.answer
|
|
116
|
-
content.inferenceId = response.inferenceId
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return content
|
|
120
|
-
})
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
async function trackKnowledgeBase(
|
|
124
|
-
response: KnowledgeBasesResponse,
|
|
125
|
-
request: BotContext,
|
|
126
|
-
knowledgeBaseContent: FlowKnowledgeBase
|
|
127
|
-
) {
|
|
128
|
-
const getKnowledgeFailReason = (): KnowledgebaseFailReason | undefined => {
|
|
129
|
-
let knowledgebaseFailReason: KnowledgebaseFailReason | undefined
|
|
130
|
-
|
|
131
|
-
if (!response.isFaithful) {
|
|
132
|
-
knowledgebaseFailReason = KnowledgebaseFailReason.Hallucination
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if (!response.hasKnowledge) {
|
|
136
|
-
knowledgebaseFailReason = KnowledgebaseFailReason.NoKnowledge
|
|
137
|
-
}
|
|
138
|
-
return knowledgebaseFailReason
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const { flowId, flowName, flowNodeId, flowNodeContentId } =
|
|
142
|
-
getCommonFlowContentEventArgsForContentId(request, knowledgeBaseContent.id)
|
|
143
|
-
|
|
144
|
-
const event: EventKnowledgeBase = {
|
|
145
|
-
action: EventAction.Knowledgebase,
|
|
146
|
-
knowledgebaseInferenceId: response.inferenceId,
|
|
147
|
-
knowledgebaseFailReason: getKnowledgeFailReason(),
|
|
148
|
-
knowledgebaseSourcesIds: knowledgeBaseContent.sourcesData.map(
|
|
149
|
-
source => source.id
|
|
150
|
-
),
|
|
151
|
-
knowledgebaseChunksIds: response.chunkIds,
|
|
152
|
-
knowledgebaseMessageId: request.input.message_id,
|
|
153
|
-
userInput: request.input.data as string,
|
|
154
|
-
flowThreadId: request.session.flow_thread_id as string,
|
|
155
|
-
flowId,
|
|
156
|
-
flowName,
|
|
157
|
-
flowNodeId,
|
|
158
|
-
flowNodeContentId,
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const { action, ...eventArgs } = event
|
|
162
|
-
|
|
163
|
-
await trackEvent(request, action, eventArgs)
|
|
164
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import type { BotContext } from '@botonic/core'
|
|
2
|
-
import { Text } from '@botonic/react'
|
|
3
|
-
|
|
4
|
-
import { ContentFieldsBase } from './content-fields-base'
|
|
5
|
-
import type { HtKnowledgeBaseNode } from './hubtype-fields'
|
|
6
|
-
|
|
7
|
-
export const DISABLED_MEMORY_LENGTH = 1
|
|
8
|
-
|
|
9
|
-
export class FlowKnowledgeBase extends ContentFieldsBase {
|
|
10
|
-
public feedbackEnabled: boolean = false
|
|
11
|
-
public sources: string[] = []
|
|
12
|
-
public text: string = ''
|
|
13
|
-
public inferenceId?: string
|
|
14
|
-
public sourcesData: { id: string; name: string }[] = []
|
|
15
|
-
public instructions: string
|
|
16
|
-
public hasMemory: boolean
|
|
17
|
-
public memoryLength: number
|
|
18
|
-
|
|
19
|
-
static fromHubtypeCMS(component: HtKnowledgeBaseNode): FlowKnowledgeBase {
|
|
20
|
-
const newKnowledgeBase = new FlowKnowledgeBase(component.id)
|
|
21
|
-
newKnowledgeBase.code = component.code
|
|
22
|
-
newKnowledgeBase.feedbackEnabled = component.content.feedback_enabled
|
|
23
|
-
newKnowledgeBase.sourcesData = component.content.sources_data
|
|
24
|
-
newKnowledgeBase.instructions = component.content.instructions
|
|
25
|
-
newKnowledgeBase.hasMemory = component.content.has_memory || false
|
|
26
|
-
newKnowledgeBase.memoryLength =
|
|
27
|
-
component.content.memory_length || DISABLED_MEMORY_LENGTH
|
|
28
|
-
newKnowledgeBase.followUp = component.follow_up
|
|
29
|
-
|
|
30
|
-
return newKnowledgeBase
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
34
|
-
async trackFlow(_botContext: BotContext): Promise<void> {
|
|
35
|
-
// TODO: Review how we can track here the knowledge base event here.
|
|
36
|
-
// We should store event args in an attribute of FlowKnowledgeBase
|
|
37
|
-
// when knowledge base is resolved inside the flow builder action
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async processContent(botContext: BotContext): Promise<void> {
|
|
41
|
-
await this.filterContent(botContext, this)
|
|
42
|
-
await this.trackFlow(botContext)
|
|
43
|
-
return
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
toBotonic(botContext: BotContext): JSX.Element {
|
|
47
|
-
return (
|
|
48
|
-
<Text
|
|
49
|
-
key={this.id}
|
|
50
|
-
feedbackEnabled={this.feedbackEnabled}
|
|
51
|
-
inferenceId={this.inferenceId}
|
|
52
|
-
botInteractionId={botContext.input.bot_interaction_id}
|
|
53
|
-
>
|
|
54
|
-
{this.text}
|
|
55
|
-
</Text>
|
|
56
|
-
)
|
|
57
|
-
}
|
|
58
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { HtBaseNode } from './common'
|
|
2
|
-
import type { HtNodeWithContentType } from './node-types'
|
|
3
|
-
|
|
4
|
-
interface SourceData {
|
|
5
|
-
id: string
|
|
6
|
-
name: string
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface HtKnowledgeBaseNode extends HtBaseNode {
|
|
10
|
-
type: HtNodeWithContentType.KNOWLEDGE_BASE
|
|
11
|
-
content: {
|
|
12
|
-
feedback_enabled: boolean
|
|
13
|
-
sources_data: SourceData[]
|
|
14
|
-
instructions: string
|
|
15
|
-
has_memory: boolean
|
|
16
|
-
memory_length?: number
|
|
17
|
-
}
|
|
18
|
-
}
|