@magmamath/students-features 0.3.0-rc.13 → 0.3.0-rc.14
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 +1 -1
- package/dist/commonjs/features/chatbot/components/Alternatives/Alternatives.js.map +1 -1
- package/dist/commonjs/features/chatbot/components/ChatControls/ChatControls.js +1 -1
- package/dist/commonjs/features/chatbot/components/ChatControls/ChatControls.js.map +1 -1
- package/dist/commonjs/features/chatbot/components/Chatbot.js +1 -1
- package/dist/commonjs/features/chatbot/components/Chatbot.js.map +1 -1
- package/dist/commonjs/features/chatbot/model/ChatBotModel.js +34 -60
- 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/messagesHelper.js +46 -0
- package/dist/commonjs/features/chatbot/model/messagesHelper.js.map +1 -0
- package/dist/commonjs/features/chatbot/model/translation.helpers.js +62 -0
- package/dist/commonjs/features/chatbot/model/translation.helpers.js.map +1 -0
- package/dist/commonjs/features/chatbot/model/translation.js +94 -130
- 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 +9 -1
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/lib/helpers/types.js +2 -0
- package/dist/commonjs/lib/helpers/types.js.map +1 -0
- package/dist/module/features/chatbot/components/Alternatives/Alternatives.js +1 -1
- package/dist/module/features/chatbot/components/Alternatives/Alternatives.js.map +1 -1
- package/dist/module/features/chatbot/components/ChatControls/ChatControls.js +1 -1
- package/dist/module/features/chatbot/components/ChatControls/ChatControls.js.map +1 -1
- package/dist/module/features/chatbot/components/Chatbot.js +1 -1
- 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 +34 -60
- 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/messagesHelper.js +41 -0
- package/dist/module/features/chatbot/model/messagesHelper.js.map +1 -0
- package/dist/module/features/chatbot/model/translation.helpers.js +57 -0
- package/dist/module/features/chatbot/model/translation.helpers.js.map +1 -0
- package/dist/module/features/chatbot/model/translation.js +95 -131
- 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 +1 -0
- package/dist/module/index.js.map +1 -1
- package/dist/module/lib/helpers/types.js +2 -0
- package/dist/module/lib/helpers/types.js.map +1 -0
- package/dist/typescript/commonjs/features/chatbot/index.d.ts +1 -1
- package/dist/typescript/commonjs/features/chatbot/index.d.ts.map +1 -1
- package/dist/typescript/commonjs/features/chatbot/model/ChatBotModel.d.ts +6 -6
- package/dist/typescript/commonjs/features/chatbot/model/ChatBotModel.d.ts.map +1 -1
- package/dist/typescript/commonjs/features/chatbot/model/api.d.ts +5 -7
- package/dist/typescript/commonjs/features/chatbot/model/api.d.ts.map +1 -1
- package/dist/typescript/commonjs/features/chatbot/model/messagesHelper.d.ts +21 -0
- package/dist/typescript/commonjs/features/chatbot/model/messagesHelper.d.ts.map +1 -0
- package/dist/typescript/commonjs/features/chatbot/model/translation.d.ts +9 -9
- package/dist/typescript/commonjs/features/chatbot/model/translation.d.ts.map +1 -1
- package/dist/typescript/commonjs/features/chatbot/model/translation.helpers.d.ts +20 -0
- package/dist/typescript/commonjs/features/chatbot/model/translation.helpers.d.ts.map +1 -0
- package/dist/typescript/commonjs/features/chatbot/types/model.types.d.ts +4 -0
- 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 +19 -4
- package/dist/typescript/commonjs/features/chatbot/types/translation.types.d.ts.map +1 -1
- package/dist/typescript/commonjs/index.d.ts +1 -0
- package/dist/typescript/commonjs/index.d.ts.map +1 -1
- package/dist/typescript/commonjs/lib/helpers/types.d.ts +2 -0
- package/dist/typescript/commonjs/lib/helpers/types.d.ts.map +1 -0
- package/dist/typescript/module/features/chatbot/index.d.ts +1 -1
- package/dist/typescript/module/features/chatbot/index.d.ts.map +1 -1
- package/dist/typescript/module/features/chatbot/model/ChatBotModel.d.ts +6 -6
- package/dist/typescript/module/features/chatbot/model/ChatBotModel.d.ts.map +1 -1
- package/dist/typescript/module/features/chatbot/model/api.d.ts +5 -7
- package/dist/typescript/module/features/chatbot/model/api.d.ts.map +1 -1
- package/dist/typescript/module/features/chatbot/model/messagesHelper.d.ts +21 -0
- package/dist/typescript/module/features/chatbot/model/messagesHelper.d.ts.map +1 -0
- package/dist/typescript/module/features/chatbot/model/translation.d.ts +9 -9
- package/dist/typescript/module/features/chatbot/model/translation.d.ts.map +1 -1
- package/dist/typescript/module/features/chatbot/model/translation.helpers.d.ts +20 -0
- package/dist/typescript/module/features/chatbot/model/translation.helpers.d.ts.map +1 -0
- package/dist/typescript/module/features/chatbot/types/model.types.d.ts +4 -0
- 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 +19 -4
- package/dist/typescript/module/features/chatbot/types/translation.types.d.ts.map +1 -1
- package/dist/typescript/module/index.d.ts +1 -0
- package/dist/typescript/module/index.d.ts.map +1 -1
- package/dist/typescript/module/lib/helpers/types.d.ts +2 -0
- package/dist/typescript/module/lib/helpers/types.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/features/chatbot/components/Alternatives/Alternatives.tsx +1 -1
- package/src/features/chatbot/components/ChatControls/ChatControls.tsx +1 -1
- package/src/features/chatbot/components/Chatbot.tsx +1 -1
- package/src/features/chatbot/index.ts +6 -1
- package/src/features/chatbot/model/ChatBotModel.ts +36 -67
- package/src/features/chatbot/model/api.ts +24 -33
- package/src/features/chatbot/model/messagesHelper.ts +48 -0
- package/src/features/chatbot/model/translation.helpers.ts +50 -0
- package/src/features/chatbot/model/translation.ts +91 -147
- package/src/features/chatbot/types/model.types.ts +5 -0
- package/src/features/chatbot/types/translation.types.ts +20 -4
- package/src/index.ts +1 -0
- package/src/lib/helpers/types.ts +1 -0
|
@@ -5,15 +5,30 @@ export type TranslateBatchFxProps = {
|
|
|
5
5
|
params: {
|
|
6
6
|
targetLanguage: string;
|
|
7
7
|
};
|
|
8
|
+
alternatives: GetAlternativesResponse | null;
|
|
8
9
|
};
|
|
9
10
|
export type TranslateMessageFxProps = {
|
|
10
11
|
message: ConversationMessage;
|
|
11
12
|
targetLanguage: string;
|
|
12
13
|
};
|
|
13
|
-
export type
|
|
14
|
+
export type AddTranslationToAlternativesProps = {
|
|
15
|
+
alternatives: GetAlternativesResponse;
|
|
16
|
+
translatedHints: string[];
|
|
17
|
+
translatedDefaults: string[];
|
|
18
|
+
};
|
|
19
|
+
export type GetOptionsProps = {
|
|
14
20
|
alternatives: GetAlternativesResponse | null;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
21
|
+
lastMessage: ConversationMessage | undefined;
|
|
22
|
+
};
|
|
23
|
+
export type GetOptionsResult = {
|
|
24
|
+
lastMessage: string[] | null;
|
|
25
|
+
alternatives: {
|
|
26
|
+
hints: string[];
|
|
27
|
+
default: string[];
|
|
28
|
+
} | null;
|
|
29
|
+
};
|
|
30
|
+
export type TranslateRequestProps<List extends Record<string, string[]>> = {
|
|
31
|
+
text: List;
|
|
32
|
+
targetLanguage: string;
|
|
18
33
|
};
|
|
19
34
|
//# sourceMappingURL=translation.types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"translation.types.d.ts","sourceRoot":"","sources":["../../../../../../src/features/chatbot/types/translation.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAA;AACnD,OAAO,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AAErD,MAAM,MAAM,qBAAqB,GAAG;IAClC,QAAQ,EAAE,mBAAmB,EAAE,CAAA;IAC/B,MAAM,EAAE;QACN,cAAc,EAAE,MAAM,CAAA;KACvB,CAAA;
|
|
1
|
+
{"version":3,"file":"translation.types.d.ts","sourceRoot":"","sources":["../../../../../../src/features/chatbot/types/translation.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAA;AACnD,OAAO,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AAErD,MAAM,MAAM,qBAAqB,GAAG;IAClC,QAAQ,EAAE,mBAAmB,EAAE,CAAA;IAC/B,MAAM,EAAE;QACN,cAAc,EAAE,MAAM,CAAA;KACvB,CAAA;IACD,YAAY,EAAE,uBAAuB,GAAG,IAAI,CAAA;CAC7C,CAAA;AAED,MAAM,MAAM,uBAAuB,GAAG;IACpC,OAAO,EAAE,mBAAmB,CAAA;IAC5B,cAAc,EAAE,MAAM,CAAA;CACvB,CAAA;AAED,MAAM,MAAM,iCAAiC,GAAG;IAC9C,YAAY,EAAE,uBAAuB,CAAA;IACrC,eAAe,EAAE,MAAM,EAAE,CAAA;IACzB,kBAAkB,EAAE,MAAM,EAAE,CAAA;CAC7B,CAAA;AACD,MAAM,MAAM,eAAe,GAAG;IAC5B,YAAY,EAAE,uBAAuB,GAAG,IAAI,CAAA;IAC5C,WAAW,EAAE,mBAAmB,GAAG,SAAS,CAAA;CAC7C,CAAA;AACD,MAAM,MAAM,gBAAgB,GAAG;IAC7B,WAAW,EAAE,MAAM,EAAE,GAAG,IAAI,CAAA;IAC5B,YAAY,EAAE;QACZ,KAAK,EAAE,MAAM,EAAE,CAAA;QACf,OAAO,EAAE,MAAM,EAAE,CAAA;KAClB,GAAG,IAAI,CAAA;CACT,CAAA;AAED,MAAM,MAAM,qBAAqB,CAAC,IAAI,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,IAAI;IACzE,IAAI,EAAE,IAAI,CAAA;IACV,cAAc,EAAE,MAAM,CAAA;CACvB,CAAA"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { useFeaturesLocalization, setFeaturesLocalization, Locale } from './shared/translation';
|
|
2
2
|
export type { LocalizationConfig } from './shared/translation';
|
|
3
|
+
export { generateRandomId } from './lib/helpers/helpers';
|
|
3
4
|
export * from './features/chatbot';
|
|
4
5
|
export * from './features/feedback';
|
|
5
6
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,uBAAuB,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;AAC/F,YAAY,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AAC9D,cAAc,oBAAoB,CAAA;AAClC,cAAc,qBAAqB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,uBAAuB,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;AAC/F,YAAY,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACxD,cAAc,oBAAoB,CAAA;AAClC,cAAc,qBAAqB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../../src/lib/helpers/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,CAAC,CAAC,EAAE,EAAE,IAAI,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -20,7 +20,7 @@ export const Alternatives = ({
|
|
|
20
20
|
}: AlternativesProps) => {
|
|
21
21
|
const isTranslated = useUnit(model.translation.$isTranslated)
|
|
22
22
|
const alternatives = useUnit(model.$alternatives)
|
|
23
|
-
const isTranslationPending = useUnit(model.translation.
|
|
23
|
+
const isTranslationPending = useUnit(model.translation.translateAllMessagesFx.pending)
|
|
24
24
|
const lastMessageOptions = useStoreMap(model.$currentChatData, ({ messages }) => {
|
|
25
25
|
const lastMessage = messages.at(-1)
|
|
26
26
|
return lastMessage?.options ?? []
|
|
@@ -34,7 +34,7 @@ export const ChatControls = ({
|
|
|
34
34
|
}: ChatControlsProps) => {
|
|
35
35
|
const isOpen = useUnit(model.$isOpen)
|
|
36
36
|
const isTranslated = useUnit(model.translation.$isTranslated)
|
|
37
|
-
const isTranslationPending = useUnit(model.translation.
|
|
37
|
+
const isTranslationPending = useUnit(model.translation.translateAllMessagesFx.pending)
|
|
38
38
|
const isHelpRequestPending = useUnit(model.sendHelpRequestFx.pending)
|
|
39
39
|
|
|
40
40
|
if (isOpen) {
|
|
@@ -40,7 +40,7 @@ export const Chatbot = ({
|
|
|
40
40
|
const isAvailable = useStoreMap(model.$currentChatData, ({ isAvailable }) => isAvailable)
|
|
41
41
|
|
|
42
42
|
const translateMessages = () => {
|
|
43
|
-
void model.translation.
|
|
43
|
+
void model.translation.translateAllMessagesFx({
|
|
44
44
|
targetLanguage: translationTargetLanguage,
|
|
45
45
|
})
|
|
46
46
|
}
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
export { Chatbot } from './components/Chatbot'
|
|
2
2
|
export { ChatbotModel } from './model/ChatBotModel'
|
|
3
3
|
export { AudioStatus, type AudioProvider } from './types/t2s.types'
|
|
4
|
-
export {
|
|
4
|
+
export {
|
|
5
|
+
MessageVariant,
|
|
6
|
+
type ConversationMessage,
|
|
7
|
+
type ChatbotRenderer,
|
|
8
|
+
type ChatbotModelProps,
|
|
9
|
+
} from './types/model.types'
|
|
5
10
|
export type { PostMessageResponse } from './types/api.types'
|
|
@@ -4,7 +4,6 @@ import {
|
|
|
4
4
|
restore,
|
|
5
5
|
sample,
|
|
6
6
|
type Store,
|
|
7
|
-
type Effect,
|
|
8
7
|
createStore,
|
|
9
8
|
createEffect,
|
|
10
9
|
combine,
|
|
@@ -13,17 +12,18 @@ import { createControllerEffect, createEntityFieldStore } from '../../../lib/eff
|
|
|
13
12
|
import {
|
|
14
13
|
ChatbotModelProps,
|
|
15
14
|
ConversationMessage,
|
|
15
|
+
CreateMessageResponse,
|
|
16
16
|
SendHelpParams,
|
|
17
17
|
SendHelpProps,
|
|
18
18
|
SetHintMessageProps,
|
|
19
19
|
StartConversationFxData,
|
|
20
20
|
} from '../types/model.types'
|
|
21
|
-
import { GetAlternativesResponse
|
|
22
|
-
import { generateRandomId } from '../../../lib/helpers/helpers'
|
|
21
|
+
import { GetAlternativesResponse } from '../types/api.types'
|
|
23
22
|
import { ChatbotAPI } from './api'
|
|
24
23
|
import { ChatbotTranslation } from './translation'
|
|
25
24
|
import { ChatTextToSpeechModel } from './t2s'
|
|
26
25
|
import { AudioProvider } from '../types/t2s.types'
|
|
26
|
+
import { messagesHelper } from './messagesHelper'
|
|
27
27
|
|
|
28
28
|
type ReinitConversationProps = {
|
|
29
29
|
key: string
|
|
@@ -52,7 +52,7 @@ export class ChatbotModel {
|
|
|
52
52
|
readonly setScrollActive = createEvent<boolean>()
|
|
53
53
|
readonly $isScrollActive = restore(this.setScrollActive, false)
|
|
54
54
|
|
|
55
|
-
readonly sendHelpRequestFx
|
|
55
|
+
readonly sendHelpRequestFx
|
|
56
56
|
|
|
57
57
|
readonly defaultMessages: ChatbotModelProps['messages']
|
|
58
58
|
|
|
@@ -81,12 +81,7 @@ export class ChatbotModel {
|
|
|
81
81
|
this.api = new ChatbotAPI(api)
|
|
82
82
|
|
|
83
83
|
this.defaultMessages = messages
|
|
84
|
-
if ($key) {
|
|
85
|
-
sample({
|
|
86
|
-
source: $key,
|
|
87
|
-
target: this.$currentKey,
|
|
88
|
-
})
|
|
89
|
-
}
|
|
84
|
+
if ($key) sample({ source: $key, target: this.$currentKey })
|
|
90
85
|
|
|
91
86
|
this.$alternatives = restore(this.api.getAlternativesFx.doneData, null)
|
|
92
87
|
.on(this.setAlternatives, (_, payload) => payload)
|
|
@@ -110,7 +105,7 @@ export class ChatbotModel {
|
|
|
110
105
|
}
|
|
111
106
|
|
|
112
107
|
private createSendHelpRequestFx() {
|
|
113
|
-
|
|
108
|
+
const effect = attach({
|
|
114
109
|
source: [this.$currentKey, this.translation.$isTranslated],
|
|
115
110
|
mapParams: (params: SendHelpParams, [key, isTranslated]) => ({
|
|
116
111
|
...params,
|
|
@@ -120,37 +115,26 @@ export class ChatbotModel {
|
|
|
120
115
|
effect: createControllerEffect(
|
|
121
116
|
async ({
|
|
122
117
|
payload,
|
|
123
|
-
message
|
|
118
|
+
message,
|
|
124
119
|
translationTargetLanguage,
|
|
125
120
|
translatedMessage,
|
|
126
121
|
key,
|
|
127
122
|
isTranslated,
|
|
128
|
-
}: SendHelpProps) => {
|
|
123
|
+
}: SendHelpProps): Promise<CreateMessageResponse> => {
|
|
129
124
|
this.setLastMessageKey(key)
|
|
130
125
|
this.messages.setField({
|
|
131
126
|
key,
|
|
132
127
|
fn: (currentMessages = []) => [
|
|
133
128
|
...(currentMessages || []),
|
|
134
|
-
{
|
|
135
|
-
id: generateRandomId(),
|
|
136
|
-
message: message ?? '',
|
|
137
|
-
translatedMessage,
|
|
138
|
-
isOwnMessage: true,
|
|
139
|
-
threadId: payload.threadId ?? '',
|
|
140
|
-
options: [],
|
|
141
|
-
},
|
|
129
|
+
messagesHelper.createOwn({ message, translatedMessage, threadId: payload.threadId }),
|
|
142
130
|
],
|
|
143
131
|
})
|
|
144
132
|
|
|
145
133
|
const response = await this.api.postMessageFx(payload)
|
|
146
134
|
if (isTranslated) {
|
|
147
|
-
return
|
|
148
|
-
targetLanguage: translationTargetLanguage
|
|
149
|
-
message:
|
|
150
|
-
...response,
|
|
151
|
-
id: generateRandomId(),
|
|
152
|
-
isOwnMessage: false,
|
|
153
|
-
},
|
|
135
|
+
return this.translation.translateMessageFx({
|
|
136
|
+
targetLanguage: translationTargetLanguage,
|
|
137
|
+
message: messagesHelper.createAnswer(response),
|
|
154
138
|
})
|
|
155
139
|
}
|
|
156
140
|
|
|
@@ -158,58 +142,44 @@ export class ChatbotModel {
|
|
|
158
142
|
},
|
|
159
143
|
),
|
|
160
144
|
})
|
|
161
|
-
}
|
|
162
145
|
|
|
163
|
-
private initChatReplyWatcher() {
|
|
164
146
|
sample({
|
|
165
|
-
clock:
|
|
147
|
+
clock: effect.doneData,
|
|
166
148
|
source: [this.$currentKey] as const,
|
|
167
149
|
fn: ([key], response) => ({ key, response }),
|
|
168
150
|
}).watch(({ key, response }) => {
|
|
169
|
-
const newMessages: ConversationMessage[] = [
|
|
170
|
-
{
|
|
171
|
-
id: generateRandomId(),
|
|
172
|
-
message: response.message,
|
|
173
|
-
isOwnMessage: false,
|
|
174
|
-
threadId: response.threadId,
|
|
175
|
-
options: response.options,
|
|
176
|
-
translatedMessage:
|
|
177
|
-
'translatedMessage' in response ? response.translatedMessage : undefined,
|
|
178
|
-
},
|
|
179
|
-
]
|
|
180
|
-
if (response.endMessage) {
|
|
181
|
-
newMessages.push({
|
|
182
|
-
id: generateRandomId(),
|
|
183
|
-
message: 'Answer again to get another hint!',
|
|
184
|
-
isOwnMessage: false,
|
|
185
|
-
options: [],
|
|
186
|
-
threadId: '',
|
|
187
|
-
endMessage: true,
|
|
188
|
-
})
|
|
189
|
-
}
|
|
190
151
|
this.messages.setField({
|
|
191
152
|
key,
|
|
192
|
-
fn: (messages) =>
|
|
153
|
+
fn: (messages) => {
|
|
154
|
+
const copy = [...(messages || [])]
|
|
155
|
+
copy.push(messagesHelper.createAnswer(response))
|
|
156
|
+
if (response.endMessage) {
|
|
157
|
+
copy.push(messagesHelper.createLast(this.defaultMessages.endMessage))
|
|
158
|
+
}
|
|
159
|
+
return copy
|
|
160
|
+
},
|
|
193
161
|
})
|
|
194
162
|
this.threadIds.setField({ key, data: response.threadId })
|
|
195
163
|
})
|
|
196
164
|
|
|
197
165
|
sample({
|
|
198
|
-
clock:
|
|
166
|
+
clock: effect.fail,
|
|
199
167
|
source: this.$currentChatData,
|
|
200
168
|
fn: ({ key, messages }) => ({ key, data: [...messages, this.defaultMessages.error] }),
|
|
201
169
|
filter: (_, { error }) => error.message !== 'Request aborted manually',
|
|
202
170
|
target: this.messages.setField,
|
|
203
171
|
})
|
|
204
172
|
|
|
173
|
+
return effect
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private initChatReplyWatcher() {
|
|
205
177
|
sample({
|
|
206
178
|
clock: this.removeLastMessage,
|
|
207
179
|
source: [this.$lastMessageKey, this.messages.$state] as const,
|
|
208
180
|
fn: ([lastMessageKey, messages]) => {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
const updatedMessages = messages[lastMessageKey]
|
|
212
|
-
if (!updatedMessages?.length) return mockData
|
|
181
|
+
if (!lastMessageKey) return { key: '', data: [] }
|
|
182
|
+
const updatedMessages = [...messages[lastMessageKey]]
|
|
213
183
|
updatedMessages.pop()
|
|
214
184
|
return { key: lastMessageKey, data: updatedMessages }
|
|
215
185
|
},
|
|
@@ -223,7 +193,6 @@ export class ChatbotModel {
|
|
|
223
193
|
mapParams: (_: void, source) => source,
|
|
224
194
|
effect: createEffect(({ key, messages }: StartConversationFxData) => {
|
|
225
195
|
if (messages.length) return this.reinitConversation({ key })
|
|
226
|
-
|
|
227
196
|
this.messages.setField({ key, data: [this.defaultMessages.start] })
|
|
228
197
|
this.availableChats.setField({ key, data: true })
|
|
229
198
|
}),
|
|
@@ -245,17 +214,17 @@ export class ChatbotModel {
|
|
|
245
214
|
key,
|
|
246
215
|
fn: (messages) => {
|
|
247
216
|
if (!messages?.length || messages.length === 1) return
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
lastMessage = messages[messages.length - 2]
|
|
252
|
-
messagesToRemove = 2
|
|
217
|
+
const copy = [...messages]
|
|
218
|
+
while (copy.at(-1)?.endMessage) {
|
|
219
|
+
copy.pop()
|
|
253
220
|
}
|
|
221
|
+
|
|
222
|
+
const lastMessage = copy.at(-1)
|
|
254
223
|
if (!lastMessage) return
|
|
255
|
-
|
|
256
|
-
copy.splice(
|
|
224
|
+
|
|
225
|
+
copy.splice(copy.length - 1, 1, {
|
|
257
226
|
...lastMessage,
|
|
258
|
-
options: [
|
|
227
|
+
options: [this.defaultMessages.anotherHintOption],
|
|
259
228
|
})
|
|
260
229
|
return copy
|
|
261
230
|
},
|
|
@@ -1,46 +1,37 @@
|
|
|
1
|
-
import { createEffect
|
|
2
|
-
|
|
1
|
+
import { createEffect } from 'effector'
|
|
3
2
|
import { createControllerEffect } from '../../../lib/effector'
|
|
4
|
-
import { ControlledEffect } from '../../../lib/effector/createControllerEffect'
|
|
5
3
|
import { ChatbotModelProps } from '../types/model.types'
|
|
6
|
-
import {
|
|
7
|
-
GetAlternativesResponse,
|
|
8
|
-
PostMessagePayload,
|
|
9
|
-
PostMessageResponse,
|
|
10
|
-
GoogleTranslatePayload,
|
|
11
|
-
GoogleTranslateResponse,
|
|
12
|
-
GoogleText2SpeechPayload,
|
|
13
|
-
GoogleText2SpeechResponse,
|
|
14
|
-
GetAlternativesPayload,
|
|
15
|
-
} from '../types/api.types'
|
|
4
|
+
import { GoogleTranslatePayload, GoogleTranslateResponse } from '../types/api.types'
|
|
16
5
|
import { replaceKatexSyntax } from '../helpers'
|
|
17
6
|
|
|
18
7
|
export class ChatbotAPI {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
8
|
+
public readonly postMessageFx
|
|
9
|
+
public readonly getAlternativesFx
|
|
10
|
+
public readonly translateTextFx
|
|
11
|
+
public readonly textToSpeechTextFx
|
|
23
12
|
|
|
24
13
|
constructor(api: ChatbotModelProps['api']) {
|
|
25
14
|
this.postMessageFx = createControllerEffect(api.postMessage)
|
|
26
15
|
this.getAlternativesFx = createEffect(api.getAlternatives)
|
|
27
|
-
this.translateTextFx = createEffect(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
16
|
+
this.translateTextFx = createEffect(
|
|
17
|
+
async (payload: GoogleTranslatePayload): Promise<GoogleTranslateResponse> => {
|
|
18
|
+
const response = await api.postTranslateText({
|
|
19
|
+
...payload,
|
|
20
|
+
text: replaceKatexSyntax(payload.text),
|
|
21
|
+
})
|
|
32
22
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
23
|
+
return {
|
|
24
|
+
...response,
|
|
25
|
+
data: {
|
|
26
|
+
...response.data,
|
|
27
|
+
translations: response.data.translations.map((translation) => ({
|
|
28
|
+
...translation,
|
|
29
|
+
translatedText: replaceKatexSyntax([translation.translatedText], true)[0],
|
|
30
|
+
})),
|
|
31
|
+
},
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
)
|
|
44
35
|
this.textToSpeechTextFx = createEffect(api.postTextToSpeech)
|
|
45
36
|
}
|
|
46
37
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { generateRandomId } from '../../../lib/helpers/helpers'
|
|
2
|
+
import { ConversationMessage, CreateMessageResponse } from '../types/model.types'
|
|
3
|
+
|
|
4
|
+
type OwnMessageProps = {
|
|
5
|
+
message?: string
|
|
6
|
+
translatedMessage?: string
|
|
7
|
+
threadId?: string | null
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class messagesHelper {
|
|
11
|
+
public static createAnswer(data: CreateMessageResponse): ConversationMessage {
|
|
12
|
+
return {
|
|
13
|
+
id: generateRandomId(),
|
|
14
|
+
isOwnMessage: false,
|
|
15
|
+
message: data.message,
|
|
16
|
+
threadId: data.threadId,
|
|
17
|
+
options: data.options,
|
|
18
|
+
translatedMessage: data.translatedMessage,
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public static createLast(data: CreateMessageResponse) {
|
|
23
|
+
return {
|
|
24
|
+
id: generateRandomId(),
|
|
25
|
+
message: data.message,
|
|
26
|
+
translatedMessage: data.translatedMessage,
|
|
27
|
+
isOwnMessage: false,
|
|
28
|
+
options: [],
|
|
29
|
+
threadId: '',
|
|
30
|
+
endMessage: true,
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public static createOwn({
|
|
35
|
+
message = '',
|
|
36
|
+
translatedMessage,
|
|
37
|
+
threadId,
|
|
38
|
+
}: OwnMessageProps): ConversationMessage {
|
|
39
|
+
return {
|
|
40
|
+
id: generateRandomId(),
|
|
41
|
+
message,
|
|
42
|
+
translatedMessage,
|
|
43
|
+
threadId: threadId || '',
|
|
44
|
+
isOwnMessage: true,
|
|
45
|
+
options: [],
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AddTranslationToAlternativesProps,
|
|
3
|
+
GetOptionsProps,
|
|
4
|
+
GetOptionsResult,
|
|
5
|
+
} from '../types/translation.types'
|
|
6
|
+
|
|
7
|
+
export function getOptionsForTranslation({
|
|
8
|
+
lastMessage,
|
|
9
|
+
alternatives,
|
|
10
|
+
}: GetOptionsProps): GetOptionsResult {
|
|
11
|
+
if (!lastMessage) return { lastMessage: null, alternatives: null }
|
|
12
|
+
if (!lastMessage.options.length) {
|
|
13
|
+
if (!alternatives) return { lastMessage: null, alternatives: null }
|
|
14
|
+
const hintAlternatives = alternatives.options.hints ?? []
|
|
15
|
+
const defaultAlternatives = alternatives.options.default ?? []
|
|
16
|
+
return {
|
|
17
|
+
lastMessage: null,
|
|
18
|
+
alternatives: {
|
|
19
|
+
hints: hintAlternatives.map(({ text }) => text),
|
|
20
|
+
default: defaultAlternatives.map(({ text }) => text),
|
|
21
|
+
},
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
lastMessage: lastMessage.options.map(({ text }) => text),
|
|
27
|
+
alternatives: null,
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function addTranslationsToAlternative({
|
|
32
|
+
alternatives,
|
|
33
|
+
translatedHints,
|
|
34
|
+
translatedDefaults,
|
|
35
|
+
}: AddTranslationToAlternativesProps) {
|
|
36
|
+
return {
|
|
37
|
+
...alternatives,
|
|
38
|
+
options: {
|
|
39
|
+
...alternatives.options,
|
|
40
|
+
hints: translatedHints.map((translatedMessage, index) => ({
|
|
41
|
+
...alternatives.options.hints[index],
|
|
42
|
+
translatedMessage,
|
|
43
|
+
})),
|
|
44
|
+
default: translatedDefaults.map((translatedMessage, index) => ({
|
|
45
|
+
...alternatives.options.default[index],
|
|
46
|
+
translatedMessage,
|
|
47
|
+
})),
|
|
48
|
+
},
|
|
49
|
+
}
|
|
50
|
+
}
|