@magmamath/students-features 0.3.0-rc.18 → 0.3.0-rc.2
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/dist/commonjs/features/chatbot/components/Alternatives/Alternatives.js +9 -6
- package/dist/commonjs/features/chatbot/components/Alternatives/Alternatives.js.map +1 -1
- package/dist/commonjs/features/chatbot/components/ChatControls/ChatControls.js +5 -13
- package/dist/commonjs/features/chatbot/components/ChatControls/ChatControls.js.map +1 -1
- package/dist/commonjs/features/chatbot/components/ChatMessage/MessageLoader.js +3 -4
- package/dist/commonjs/features/chatbot/components/ChatMessage/MessageLoader.js.map +1 -1
- package/dist/commonjs/features/chatbot/components/ChatMessage/ReceivedMessage.js +4 -5
- package/dist/commonjs/features/chatbot/components/ChatMessage/ReceivedMessage.js.map +1 -1
- package/dist/commonjs/features/chatbot/components/ChatMessages/ChatbotMessages.js +9 -6
- package/dist/commonjs/features/chatbot/components/ChatMessages/ChatbotMessages.js.map +1 -1
- package/dist/commonjs/features/chatbot/components/Chatbot.js +9 -8
- package/dist/commonjs/features/chatbot/components/Chatbot.js.map +1 -1
- package/dist/commonjs/features/chatbot/model/ChatBotModel.js +108 -107
- package/dist/commonjs/features/chatbot/model/ChatBotModel.js.map +1 -1
- package/dist/commonjs/features/chatbot/model/api.js.map +1 -1
- package/dist/commonjs/features/chatbot/model/t2s.js +2 -1
- package/dist/commonjs/features/chatbot/model/t2s.js.map +1 -1
- package/dist/commonjs/features/chatbot/model/translation.js +140 -121
- package/dist/commonjs/features/chatbot/model/translation.js.map +1 -1
- package/dist/commonjs/features/chatbot/types/model.types.js.map +1 -1
- package/dist/commonjs/index.js +1 -9
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/lib/effector/createEntityFieldStore.js +13 -14
- package/dist/commonjs/lib/effector/createEntityFieldStore.js.map +1 -1
- package/dist/commonjs/shared/icons/ChatbotIcon.js +106 -613
- package/dist/commonjs/shared/icons/ChatbotIcon.js.map +1 -1
- package/dist/module/features/chatbot/components/Alternatives/Alternatives.js +9 -6
- package/dist/module/features/chatbot/components/Alternatives/Alternatives.js.map +1 -1
- package/dist/module/features/chatbot/components/ChatControls/ChatControls.js +5 -13
- package/dist/module/features/chatbot/components/ChatControls/ChatControls.js.map +1 -1
- package/dist/module/features/chatbot/components/ChatMessage/MessageLoader.js +3 -4
- package/dist/module/features/chatbot/components/ChatMessage/MessageLoader.js.map +1 -1
- package/dist/module/features/chatbot/components/ChatMessage/ReceivedMessage.js +4 -5
- package/dist/module/features/chatbot/components/ChatMessage/ReceivedMessage.js.map +1 -1
- package/dist/module/features/chatbot/components/ChatMessages/ChatbotMessages.js +9 -6
- package/dist/module/features/chatbot/components/ChatMessages/ChatbotMessages.js.map +1 -1
- package/dist/module/features/chatbot/components/Chatbot.js +9 -8
- package/dist/module/features/chatbot/components/Chatbot.js.map +1 -1
- package/dist/module/features/chatbot/index.js.map +1 -1
- package/dist/module/features/chatbot/model/ChatBotModel.js +109 -108
- package/dist/module/features/chatbot/model/ChatBotModel.js.map +1 -1
- package/dist/module/features/chatbot/model/api.js.map +1 -1
- package/dist/module/features/chatbot/model/t2s.js +2 -1
- package/dist/module/features/chatbot/model/t2s.js.map +1 -1
- package/dist/module/features/chatbot/model/translation.js +141 -122
- package/dist/module/features/chatbot/model/translation.js.map +1 -1
- package/dist/module/features/chatbot/types/model.types.js.map +1 -1
- package/dist/module/index.js +0 -1
- package/dist/module/index.js.map +1 -1
- package/dist/module/lib/effector/createEntityFieldStore.js +13 -12
- package/dist/module/lib/effector/createEntityFieldStore.js.map +1 -1
- package/dist/module/shared/icons/ChatbotIcon.js +107 -614
- package/dist/module/shared/icons/ChatbotIcon.js.map +1 -1
- package/dist/typescript/commonjs/features/chatbot/components/Alternatives/Alternatives.d.ts.map +1 -1
- package/dist/typescript/commonjs/features/chatbot/components/ChatControls/ChatControls.d.ts +1 -5
- package/dist/typescript/commonjs/features/chatbot/components/ChatControls/ChatControls.d.ts.map +1 -1
- package/dist/typescript/commonjs/features/chatbot/components/ChatMessages/ChatbotMessages.d.ts.map +1 -1
- package/dist/typescript/commonjs/features/chatbot/components/Chatbot.d.ts +1 -5
- package/dist/typescript/commonjs/features/chatbot/components/Chatbot.d.ts.map +1 -1
- package/dist/typescript/commonjs/features/chatbot/index.d.ts +1 -2
- package/dist/typescript/commonjs/features/chatbot/index.d.ts.map +1 -1
- package/dist/typescript/commonjs/features/chatbot/model/ChatBotModel.d.ts +49 -20
- package/dist/typescript/commonjs/features/chatbot/model/ChatBotModel.d.ts.map +1 -1
- package/dist/typescript/commonjs/features/chatbot/model/api.d.ts +7 -5
- package/dist/typescript/commonjs/features/chatbot/model/api.d.ts.map +1 -1
- package/dist/typescript/commonjs/features/chatbot/model/t2s.d.ts +1 -1
- package/dist/typescript/commonjs/features/chatbot/model/t2s.d.ts.map +1 -1
- package/dist/typescript/commonjs/features/chatbot/model/translation.d.ts +14 -30
- package/dist/typescript/commonjs/features/chatbot/model/translation.d.ts.map +1 -1
- package/dist/typescript/commonjs/features/chatbot/types/api.types.d.ts +0 -1
- package/dist/typescript/commonjs/features/chatbot/types/api.types.d.ts.map +1 -1
- package/dist/typescript/commonjs/features/chatbot/types/model.types.d.ts +7 -10
- package/dist/typescript/commonjs/features/chatbot/types/model.types.d.ts.map +1 -1
- package/dist/typescript/commonjs/features/chatbot/types/translation.types.d.ts +5 -20
- package/dist/typescript/commonjs/features/chatbot/types/translation.types.d.ts.map +1 -1
- package/dist/typescript/commonjs/index.d.ts +0 -1
- package/dist/typescript/commonjs/index.d.ts.map +1 -1
- package/dist/typescript/commonjs/lib/effector/createEntityFieldStore.d.ts +5 -6
- package/dist/typescript/commonjs/lib/effector/createEntityFieldStore.d.ts.map +1 -1
- package/dist/typescript/commonjs/shared/icons/ChatbotIcon.d.ts.map +1 -1
- package/dist/typescript/module/features/chatbot/components/Alternatives/Alternatives.d.ts.map +1 -1
- package/dist/typescript/module/features/chatbot/components/ChatControls/ChatControls.d.ts +1 -5
- package/dist/typescript/module/features/chatbot/components/ChatControls/ChatControls.d.ts.map +1 -1
- package/dist/typescript/module/features/chatbot/components/ChatMessages/ChatbotMessages.d.ts.map +1 -1
- package/dist/typescript/module/features/chatbot/components/Chatbot.d.ts +1 -5
- package/dist/typescript/module/features/chatbot/components/Chatbot.d.ts.map +1 -1
- package/dist/typescript/module/features/chatbot/index.d.ts +1 -2
- package/dist/typescript/module/features/chatbot/index.d.ts.map +1 -1
- package/dist/typescript/module/features/chatbot/model/ChatBotModel.d.ts +49 -20
- package/dist/typescript/module/features/chatbot/model/ChatBotModel.d.ts.map +1 -1
- package/dist/typescript/module/features/chatbot/model/api.d.ts +7 -5
- package/dist/typescript/module/features/chatbot/model/api.d.ts.map +1 -1
- package/dist/typescript/module/features/chatbot/model/t2s.d.ts +1 -1
- package/dist/typescript/module/features/chatbot/model/t2s.d.ts.map +1 -1
- package/dist/typescript/module/features/chatbot/model/translation.d.ts +14 -30
- package/dist/typescript/module/features/chatbot/model/translation.d.ts.map +1 -1
- package/dist/typescript/module/features/chatbot/types/api.types.d.ts +0 -1
- package/dist/typescript/module/features/chatbot/types/api.types.d.ts.map +1 -1
- package/dist/typescript/module/features/chatbot/types/model.types.d.ts +7 -10
- package/dist/typescript/module/features/chatbot/types/model.types.d.ts.map +1 -1
- package/dist/typescript/module/features/chatbot/types/translation.types.d.ts +5 -20
- package/dist/typescript/module/features/chatbot/types/translation.types.d.ts.map +1 -1
- package/dist/typescript/module/index.d.ts +0 -1
- package/dist/typescript/module/index.d.ts.map +1 -1
- package/dist/typescript/module/lib/effector/createEntityFieldStore.d.ts +5 -6
- package/dist/typescript/module/lib/effector/createEntityFieldStore.d.ts.map +1 -1
- package/dist/typescript/module/shared/icons/ChatbotIcon.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/features/chatbot/components/Alternatives/Alternatives.tsx +9 -4
- package/src/features/chatbot/components/ChatControls/ChatControls.tsx +3 -6
- package/src/features/chatbot/components/ChatMessage/MessageLoader.tsx +2 -3
- package/src/features/chatbot/components/ChatMessage/ReceivedMessage.tsx +3 -4
- package/src/features/chatbot/components/ChatMessages/ChatbotMessages.tsx +9 -4
- package/src/features/chatbot/components/Chatbot.tsx +8 -5
- package/src/features/chatbot/index.ts +1 -7
- package/src/features/chatbot/model/ChatBotModel.ts +113 -102
- package/src/features/chatbot/model/api.ts +33 -24
- package/src/features/chatbot/model/t2s.ts +2 -1
- package/src/features/chatbot/model/translation.ts +161 -147
- package/src/features/chatbot/types/api.types.ts +0 -1
- package/src/features/chatbot/types/model.types.ts +7 -12
- package/src/features/chatbot/types/translation.types.ts +5 -21
- package/src/index.ts +0 -1
- package/src/lib/effector/createEntityFieldStore.ts +9 -11
- package/src/shared/icons/ChatbotIcon.tsx +85 -500
- package/dist/commonjs/features/chatbot/model/messagesHelper.js +0 -46
- package/dist/commonjs/features/chatbot/model/messagesHelper.js.map +0 -1
- package/dist/commonjs/features/chatbot/model/translation.helpers.js +0 -69
- package/dist/commonjs/features/chatbot/model/translation.helpers.js.map +0 -1
- package/dist/commonjs/lib/helpers/types.js +0 -2
- package/dist/commonjs/lib/helpers/types.js.map +0 -1
- package/dist/module/features/chatbot/model/messagesHelper.js +0 -41
- package/dist/module/features/chatbot/model/messagesHelper.js.map +0 -1
- package/dist/module/features/chatbot/model/translation.helpers.js +0 -63
- package/dist/module/features/chatbot/model/translation.helpers.js.map +0 -1
- package/dist/module/lib/helpers/types.js +0 -2
- package/dist/module/lib/helpers/types.js.map +0 -1
- package/dist/typescript/commonjs/features/chatbot/model/messagesHelper.d.ts +0 -21
- package/dist/typescript/commonjs/features/chatbot/model/messagesHelper.d.ts.map +0 -1
- package/dist/typescript/commonjs/features/chatbot/model/translation.helpers.d.ts +0 -10
- package/dist/typescript/commonjs/features/chatbot/model/translation.helpers.d.ts.map +0 -1
- package/dist/typescript/commonjs/lib/helpers/types.d.ts +0 -2
- package/dist/typescript/commonjs/lib/helpers/types.d.ts.map +0 -1
- package/dist/typescript/module/features/chatbot/model/messagesHelper.d.ts +0 -21
- package/dist/typescript/module/features/chatbot/model/messagesHelper.d.ts.map +0 -1
- package/dist/typescript/module/features/chatbot/model/translation.helpers.d.ts +0 -10
- package/dist/typescript/module/features/chatbot/model/translation.helpers.d.ts.map +0 -1
- package/dist/typescript/module/lib/helpers/types.d.ts +0 -2
- package/dist/typescript/module/lib/helpers/types.d.ts.map +0 -1
- package/src/features/chatbot/model/messagesHelper.ts +0 -48
- package/src/features/chatbot/model/translation.helpers.ts +0 -58
- package/src/lib/helpers/types.ts +0 -1
|
@@ -1,66 +1,35 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createEvent, restore, attach, createEffect, type Effect, sample } from 'effector'
|
|
2
|
+
import { ChatbotModel } from './ChatBotModel'
|
|
3
|
+
|
|
2
4
|
import {
|
|
3
|
-
|
|
5
|
+
TranslateAlternativesFxProps,
|
|
4
6
|
TranslateBatchFxProps,
|
|
5
7
|
TranslateMessageFxProps,
|
|
6
|
-
TranslateRequestProps,
|
|
7
8
|
} from '../types/translation.types'
|
|
8
|
-
import {
|
|
9
|
+
import { ConversationMessage } from '../types/model.types'
|
|
9
10
|
import { GetAlternativesResponse } from '../types/api.types'
|
|
10
11
|
import { decodeHTMLEntities } from '../helpers'
|
|
11
|
-
import { ChatbotAPI } from './api'
|
|
12
|
-
import { EntityFieldStore } from '../../../lib/effector/createEntityFieldStore'
|
|
13
|
-
import {
|
|
14
|
-
addTranslationsToAlternative,
|
|
15
|
-
addTranslationToOptions,
|
|
16
|
-
getOptionsForTranslation,
|
|
17
|
-
} from './translation.helpers'
|
|
18
|
-
|
|
19
|
-
type ChatbotTranslationProps = {
|
|
20
|
-
api: ChatbotAPI
|
|
21
|
-
$key: Store<string>
|
|
22
|
-
defaultMessages: ChatbotModelProps['messages']
|
|
23
|
-
messagesModel: EntityFieldStore<ConversationMessage[]>
|
|
24
|
-
alternativesModel: {
|
|
25
|
-
set: EventCallable<GetAlternativesResponse | null>
|
|
26
|
-
$store: Store<GetAlternativesResponse | null>
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
12
|
|
|
30
13
|
export class ChatbotTranslation {
|
|
31
|
-
private readonly
|
|
32
|
-
private readonly $key
|
|
33
|
-
private readonly defaultMessages
|
|
34
|
-
private readonly messagesModel
|
|
35
|
-
private readonly alternativesModel
|
|
14
|
+
private readonly model: ChatbotModel
|
|
36
15
|
|
|
37
16
|
readonly setIsTranslated = createEvent<boolean>()
|
|
38
17
|
readonly $isTranslated = restore(this.setIsTranslated, false)
|
|
39
18
|
|
|
40
|
-
readonly
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
this
|
|
51
|
-
this.
|
|
52
|
-
this.
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
this.translateAllMessagesFx = attach({
|
|
56
|
-
source: [this.$key, this.messagesModel.$state, this.alternativesModel.$store],
|
|
57
|
-
mapParams: (params: TranslateBatchFxProps['params'], [key, messages, alternatives]) => ({
|
|
58
|
-
params,
|
|
59
|
-
messages: messages[key] ?? [],
|
|
60
|
-
alternatives,
|
|
61
|
-
}),
|
|
62
|
-
effect: this.translateBatchFx,
|
|
63
|
-
})
|
|
19
|
+
readonly translateMessageFx: Effect<TranslateMessageFxProps, ConversationMessage>
|
|
20
|
+
readonly translateBatchFx: Effect<TranslateBatchFxProps['params'], ConversationMessage[]>
|
|
21
|
+
readonly translateAlternativesFx: Effect<
|
|
22
|
+
TranslateAlternativesFxProps['params'],
|
|
23
|
+
GetAlternativesResponse | null
|
|
24
|
+
>
|
|
25
|
+
|
|
26
|
+
constructor(chatbotModel: ChatbotModel) {
|
|
27
|
+
this.model = chatbotModel
|
|
28
|
+
|
|
29
|
+
this.translateAlternativesFx = this.createTranslateAlternativesEffect()
|
|
30
|
+
this.translateMessageFx = this.createTranslateMessageEffect()
|
|
31
|
+
this.translateBatchFx = this.createTranslateBatchEffect()
|
|
32
|
+
|
|
64
33
|
this.initTranslationSamples()
|
|
65
34
|
}
|
|
66
35
|
|
|
@@ -70,7 +39,7 @@ export class ChatbotTranslation {
|
|
|
70
39
|
return true
|
|
71
40
|
}
|
|
72
41
|
|
|
73
|
-
const allTranslated = messages.every(
|
|
42
|
+
const allTranslated = messages.every(message => message.translatedMessage)
|
|
74
43
|
if (allTranslated) {
|
|
75
44
|
this.setIsTranslated(true)
|
|
76
45
|
return true
|
|
@@ -81,115 +50,160 @@ export class ChatbotTranslation {
|
|
|
81
50
|
|
|
82
51
|
private initTranslationSamples() {
|
|
83
52
|
sample({
|
|
84
|
-
clock: this.
|
|
85
|
-
source: [this.$
|
|
53
|
+
clock: this.translateBatchFx.doneData,
|
|
54
|
+
source: [this.model.$currentKey] as const,
|
|
86
55
|
fn: ([key], translatedMessages) => ({ key, data: translatedMessages }),
|
|
87
|
-
target: this.
|
|
56
|
+
target: this.model.messages.setField,
|
|
88
57
|
})
|
|
89
58
|
}
|
|
90
59
|
|
|
91
|
-
private
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
60
|
+
private createTranslateAlternativesEffect() {
|
|
61
|
+
return attach({
|
|
62
|
+
source: [this.model.$alternatives],
|
|
63
|
+
mapParams: (params: TranslateAlternativesFxProps['params'], [alternatives]) => ({
|
|
64
|
+
params,
|
|
65
|
+
alternatives,
|
|
66
|
+
}),
|
|
67
|
+
effect: createEffect(async ({ alternatives, params }: TranslateAlternativesFxProps) => {
|
|
68
|
+
const hintAlternatives = alternatives?.options.hints ?? []
|
|
69
|
+
const defaultAlternatives = alternatives?.options.default ?? []
|
|
70
|
+
|
|
71
|
+
const translatedAlternatives = { ...alternatives }
|
|
72
|
+
|
|
73
|
+
if (hintAlternatives.length) {
|
|
74
|
+
const hintText = hintAlternatives.map(({ text }) => text)
|
|
75
|
+
const response = await this.model.api.translateTextFx({
|
|
76
|
+
text: hintText,
|
|
77
|
+
targetLanguage: params.targetLanguage,
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
if (translatedAlternatives.options) {
|
|
81
|
+
translatedAlternatives.options.hints = hintAlternatives.map((alternative, index) => ({
|
|
82
|
+
...alternative,
|
|
83
|
+
translatedMessage: decodeHTMLEntities(
|
|
84
|
+
response.data.translations[index].translatedText,
|
|
85
|
+
),
|
|
86
|
+
}))
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (defaultAlternatives.length) {
|
|
91
|
+
const defaultText = defaultAlternatives.map(({ text }) => text)
|
|
92
|
+
const response = await this.model.api.translateTextFx({
|
|
93
|
+
text: defaultText,
|
|
94
|
+
targetLanguage: params.targetLanguage,
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
if (translatedAlternatives.options) {
|
|
98
|
+
translatedAlternatives.options.default = defaultAlternatives.map(
|
|
99
|
+
(alternative, index) => ({
|
|
100
|
+
...alternative,
|
|
101
|
+
translatedText: decodeHTMLEntities(
|
|
102
|
+
response.data.translations[index].translatedText,
|
|
103
|
+
),
|
|
104
|
+
}),
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
109
108
|
|
|
110
|
-
|
|
111
|
-
|
|
109
|
+
return translatedAlternatives as GetAlternativesResponse | null
|
|
110
|
+
}),
|
|
111
|
+
})
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
-
private
|
|
115
|
-
async ({
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
const defaultMessages = [this.defaultMessages.error, this.defaultMessages.endMessage]
|
|
121
|
-
const defaultOptions = [this.defaultMessages.anotherHintOption]
|
|
122
|
-
const options = getOptionsForTranslation({ lastMessage: messages.at(-1), alternatives })
|
|
123
|
-
|
|
124
|
-
const translatedTextsList = await this.translateRequest({
|
|
125
|
-
textList: {
|
|
126
|
-
messages: messages.map(({ message }) => message),
|
|
127
|
-
defaultMessages: defaultMessages.map(({ message }) => message),
|
|
128
|
-
defaultOptions: defaultOptions.map(({ text }) => text),
|
|
129
|
-
lastMessage: options.lastMessage || [],
|
|
130
|
-
alternativesHints: options.alternatives?.hints || [],
|
|
131
|
-
alternativesDefaults: options.alternatives?.default || [],
|
|
132
|
-
},
|
|
133
|
-
targetLanguage: params.targetLanguage,
|
|
114
|
+
private createTranslateMessageEffect() {
|
|
115
|
+
return createEffect(async ({ message, targetLanguage }: TranslateMessageFxProps) => {
|
|
116
|
+
const optionsText = message.options.map(({ text }) => text)
|
|
117
|
+
const response = await this.model.api.translateTextFx({
|
|
118
|
+
text: [message.message, ...optionsText],
|
|
119
|
+
targetLanguage,
|
|
134
120
|
})
|
|
135
121
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
})
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
122
|
+
const translatedOptions = response.data.translations.slice(1)
|
|
123
|
+
const optionsCopy = [...message.options]
|
|
124
|
+
const newOptions = optionsCopy.map((option, index) => ({
|
|
125
|
+
...option,
|
|
126
|
+
translatedText: decodeHTMLEntities(translatedOptions[index].translatedText),
|
|
127
|
+
}))
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
...message,
|
|
131
|
+
translatedMessage: decodeHTMLEntities(response.data.translations[0].translatedText),
|
|
132
|
+
options: newOptions,
|
|
133
|
+
} as ConversationMessage
|
|
134
|
+
})
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private createTranslateBatchEffect() {
|
|
138
|
+
return attach({
|
|
139
|
+
source: [this.model.$currentKey, this.model.messages.$state],
|
|
140
|
+
mapParams: (params: TranslateBatchFxProps['params'], [key, messages]) => ({
|
|
141
|
+
params,
|
|
142
|
+
messages: messages[key] ?? [],
|
|
143
|
+
}),
|
|
144
|
+
effect: createEffect(async ({ messages, params }: TranslateBatchFxProps) => {
|
|
145
|
+
if (this.shouldSkipTranslation(messages)) {
|
|
146
|
+
return messages
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const messagesText = messages.map(({ message }) => message)
|
|
150
|
+
const response = await this.model.api.translateTextFx({
|
|
151
|
+
text: messagesText,
|
|
152
|
+
targetLanguage: params.targetLanguage,
|
|
147
153
|
})
|
|
148
|
-
}
|
|
149
154
|
|
|
150
|
-
|
|
151
|
-
|
|
155
|
+
const lastMessage = messages.at(-1)
|
|
156
|
+
const lastMessageTranslatedOptions = lastMessage
|
|
157
|
+
? await this.translateMessageOptions(lastMessage, params.targetLanguage)
|
|
158
|
+
: undefined
|
|
152
159
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
options: isLastMessage
|
|
158
|
-
? addTranslationToOptions(message.options, translatedTextsList.lastMessage)
|
|
159
|
-
: message.options,
|
|
160
|
-
translatedMessage: translatedTextsList.messages[index],
|
|
161
|
-
}
|
|
162
|
-
})
|
|
163
|
-
},
|
|
164
|
-
)
|
|
160
|
+
const errorMessageTranslationResponse = await this.model.api.translateTextFx({
|
|
161
|
+
text: [this.model.defaultMessages.error.message],
|
|
162
|
+
targetLanguage: params.targetLanguage,
|
|
163
|
+
})
|
|
165
164
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
165
|
+
this.model.defaultMessages.error.translatedMessage = decodeHTMLEntities(
|
|
166
|
+
errorMessageTranslationResponse.data.translations[0].translatedText,
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
// TODO: move this AFTER state update in sample and remove initTranslationSamples it is not needed
|
|
170
|
+
// TODO: maybe just one request to translate everything
|
|
171
|
+
this.setIsTranslated(true)
|
|
172
|
+
|
|
173
|
+
const translatedMessages = response.data.translations.map(
|
|
174
|
+
({ translatedText }) => translatedText,
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
return messages.map((message, index) => {
|
|
178
|
+
const isLastMessage = index === messages.length - 1
|
|
179
|
+
return {
|
|
180
|
+
...message,
|
|
181
|
+
options: isLastMessage ? lastMessageTranslatedOptions : message.options,
|
|
182
|
+
translatedMessage: decodeHTMLEntities(translatedMessages[index]),
|
|
183
|
+
}
|
|
184
|
+
}) as ConversationMessage[]
|
|
185
|
+
}),
|
|
186
|
+
})
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
private async translateMessageOptions(message: ConversationMessage, targetLanguage: string) {
|
|
190
|
+
if (!message?.options?.length) {
|
|
191
|
+
const translatedAlternatives = await this.translateAlternativesFx({ targetLanguage })
|
|
192
|
+
this.model.setAlternatives(translatedAlternatives)
|
|
193
|
+
return
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const optionsText = message.options.map(({ text }) => text)
|
|
197
|
+
const response = await this.model.api.translateTextFx({
|
|
198
|
+
text: optionsText,
|
|
169
199
|
targetLanguage,
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const response = await this.api.translateTextFx({ text, targetLanguage })
|
|
178
|
-
const translatedTexts = response.data.translations.map(({ translatedText }) =>
|
|
179
|
-
decodeHTMLEntities(translatedText),
|
|
180
|
-
)
|
|
181
|
-
|
|
182
|
-
return messages.map((message): ConversationMessage => {
|
|
183
|
-
const options = message.options
|
|
184
|
-
const translatedMessage = translatedTexts.shift()
|
|
185
|
-
return {
|
|
186
|
-
...message,
|
|
187
|
-
translatedMessage,
|
|
188
|
-
options: addTranslationToOptions(options, translatedTexts.splice(0, options.length)),
|
|
189
|
-
}
|
|
190
|
-
})
|
|
191
|
-
},
|
|
192
|
-
)
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
return message.options.map((option, index) => ({
|
|
203
|
+
...option,
|
|
204
|
+
translatedText: decodeHTMLEntities(response.data.translations[index].translatedText),
|
|
205
|
+
}))
|
|
206
|
+
}
|
|
193
207
|
|
|
194
208
|
public reset() {
|
|
195
209
|
this.setIsTranslated(false)
|
|
@@ -10,7 +10,6 @@ import {
|
|
|
10
10
|
ChatAnswer,
|
|
11
11
|
GetAlternativesPayload,
|
|
12
12
|
} from './api.types'
|
|
13
|
-
import { AccomplishTo } from '../../../lib/helpers/types'
|
|
14
13
|
|
|
15
14
|
export type ConversationMessage = {
|
|
16
15
|
id: string
|
|
@@ -19,11 +18,14 @@ export type ConversationMessage = {
|
|
|
19
18
|
options: ChatAnswer[]
|
|
20
19
|
isOwnMessage: boolean
|
|
21
20
|
translatedMessage?: string
|
|
22
|
-
endMessage?: boolean
|
|
23
21
|
}
|
|
24
22
|
|
|
25
23
|
export type ChatbotModelProps = {
|
|
26
24
|
$key?: Store<string>
|
|
25
|
+
credits?: {
|
|
26
|
+
$assignmentId: Store<string>
|
|
27
|
+
$exerciseId: Store<string>
|
|
28
|
+
}
|
|
27
29
|
api: {
|
|
28
30
|
getAlternatives: (props: GetAlternativesPayload) => Promise<GetAlternativesResponse>
|
|
29
31
|
postMessage: (props: PostMessagePayload) => Promise<PostMessageResponse>
|
|
@@ -33,8 +35,6 @@ export type ChatbotModelProps = {
|
|
|
33
35
|
messages: {
|
|
34
36
|
error: ConversationMessage
|
|
35
37
|
start: ConversationMessage
|
|
36
|
-
endMessage: ConversationMessage
|
|
37
|
-
anotherHintOption: ChatAnswer
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
|
|
@@ -47,18 +47,15 @@ export type SendHelpParams = {
|
|
|
47
47
|
|
|
48
48
|
export type SendHelpProps = SendHelpParams & {
|
|
49
49
|
key: string
|
|
50
|
+
messages: Record<string, ConversationMessage[]>
|
|
50
51
|
isTranslated: boolean
|
|
51
52
|
}
|
|
52
53
|
|
|
53
54
|
export type SetHintMessageProps = {
|
|
54
|
-
|
|
55
|
+
entityId: string
|
|
56
|
+
fieldId: string
|
|
55
57
|
} & ConversationMessage
|
|
56
58
|
|
|
57
|
-
export type StartConversationFxData = {
|
|
58
|
-
key: string
|
|
59
|
-
messages: ConversationMessage[]
|
|
60
|
-
}
|
|
61
|
-
|
|
62
59
|
export enum MessageVariant {
|
|
63
60
|
SENT = 'sent',
|
|
64
61
|
RECEIVED = 'received',
|
|
@@ -69,5 +66,3 @@ export type ChatbotRenderer = {
|
|
|
69
66
|
variant: MessageVariant
|
|
70
67
|
withTextToSpeech: boolean
|
|
71
68
|
}
|
|
72
|
-
|
|
73
|
-
export type CreateMessageResponse = AccomplishTo<PostMessageResponse, ConversationMessage>
|
|
@@ -6,32 +6,16 @@ export type TranslateBatchFxProps = {
|
|
|
6
6
|
params: {
|
|
7
7
|
targetLanguage: string
|
|
8
8
|
}
|
|
9
|
-
alternatives: GetAlternativesResponse | null
|
|
10
9
|
}
|
|
11
10
|
|
|
12
11
|
export type TranslateMessageFxProps = {
|
|
13
|
-
|
|
12
|
+
message: ConversationMessage
|
|
14
13
|
targetLanguage: string
|
|
15
14
|
}
|
|
16
15
|
|
|
17
|
-
export type
|
|
18
|
-
alternatives: GetAlternativesResponse
|
|
19
|
-
translatedHints: string[]
|
|
20
|
-
translatedDefaults: string[]
|
|
21
|
-
}
|
|
22
|
-
export type GetOptionsProps = {
|
|
16
|
+
export type TranslateAlternativesFxProps = {
|
|
23
17
|
alternatives: GetAlternativesResponse | null
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
lastMessage: string[] | null
|
|
28
|
-
alternatives: {
|
|
29
|
-
hints: string[]
|
|
30
|
-
default: string[]
|
|
31
|
-
} | null
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export type TranslateRequestProps<List extends Record<string, string[]>> = {
|
|
35
|
-
textList: List
|
|
36
|
-
targetLanguage: string
|
|
18
|
+
params: {
|
|
19
|
+
targetLanguage: string
|
|
20
|
+
}
|
|
37
21
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
export { useFeaturesLocalization, setFeaturesLocalization, Locale } from './shared/translation'
|
|
2
2
|
export type { LocalizationConfig } from './shared/translation'
|
|
3
|
-
export { generateRandomId } from './lib/helpers/helpers'
|
|
4
3
|
export * from './features/chatbot'
|
|
5
4
|
export * from './features/feedback'
|
|
@@ -4,23 +4,21 @@ type UpdateField<T> = {
|
|
|
4
4
|
key: string
|
|
5
5
|
} & ({ data: T; fn?: never } | { data?: never; fn: (props: T | void) => T | void })
|
|
6
6
|
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
export function createEntityFieldStore<T>() {
|
|
8
|
+
const setField = createEvent<UpdateField<T>>()
|
|
9
|
+
const reset = createEvent()
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
.on(
|
|
13
|
-
if (
|
|
11
|
+
const $state = createStore<Record<string, T>>({})
|
|
12
|
+
.on(setField, (state, { key, data }) => {
|
|
13
|
+
if (typeof data === 'function') {
|
|
14
14
|
const currentData = state[key]
|
|
15
|
-
const newData =
|
|
15
|
+
const newData = data(currentData)
|
|
16
16
|
if (newData === undefined) return state
|
|
17
17
|
return { ...state, [key]: newData }
|
|
18
18
|
}
|
|
19
19
|
return { ...state, [key]: data }
|
|
20
20
|
})
|
|
21
|
-
.reset(
|
|
22
|
-
}
|
|
21
|
+
.reset(reset)
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
return new EntityFieldStore<T>()
|
|
23
|
+
return { $state, setField, reset }
|
|
26
24
|
}
|