@magmamath/students-features 0.3.0-rc.13 → 0.3.0-rc.15
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 -86
- 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 +69 -0
- package/dist/commonjs/features/chatbot/model/translation.helpers.js.map +1 -0
- package/dist/commonjs/features/chatbot/model/translation.js +106 -135
- 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 -86
- 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 +63 -0
- package/dist/module/features/chatbot/model/translation.helpers.js.map +1 -0
- package/dist/module/features/chatbot/model/translation.js +107 -136
- 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 +2 -2
- package/dist/typescript/commonjs/features/chatbot/index.d.ts.map +1 -1
- package/dist/typescript/commonjs/features/chatbot/model/ChatBotModel.d.ts +5 -5
- 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 +10 -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 +20 -5
- 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 +2 -2
- package/dist/typescript/module/features/chatbot/index.d.ts.map +1 -1
- package/dist/typescript/module/features/chatbot/model/ChatBotModel.d.ts +5 -5
- 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 +10 -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 +20 -5
- 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 +7 -2
- package/src/features/chatbot/model/ChatBotModel.ts +27 -85
- 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 +58 -0
- package/src/features/chatbot/model/translation.ts +112 -160
- package/src/features/chatbot/types/model.types.ts +5 -0
- package/src/features/chatbot/types/translation.types.ts +21 -5
- 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
|
+
messages: 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
|
+
textList: 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,QAAQ,EAAE,mBAAmB,EAAE,CAAA;IAC/B,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,QAAQ,EAAE,IAAI,CAAA;IACd,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 {
|
|
5
|
-
|
|
4
|
+
export {
|
|
5
|
+
MessageVariant,
|
|
6
|
+
type ConversationMessage,
|
|
7
|
+
type ChatbotRenderer,
|
|
8
|
+
type ChatbotModelProps,
|
|
9
|
+
} from './types/model.types'
|
|
10
|
+
export type { PostMessageResponse, ChatAnswer } 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,
|
|
@@ -18,12 +17,12 @@ import {
|
|
|
18
17
|
SetHintMessageProps,
|
|
19
18
|
StartConversationFxData,
|
|
20
19
|
} from '../types/model.types'
|
|
21
|
-
import { GetAlternativesResponse
|
|
22
|
-
import { generateRandomId } from '../../../lib/helpers/helpers'
|
|
20
|
+
import { GetAlternativesResponse } from '../types/api.types'
|
|
23
21
|
import { ChatbotAPI } from './api'
|
|
24
22
|
import { ChatbotTranslation } from './translation'
|
|
25
23
|
import { ChatTextToSpeechModel } from './t2s'
|
|
26
24
|
import { AudioProvider } from '../types/t2s.types'
|
|
25
|
+
import { messagesHelper } from './messagesHelper'
|
|
27
26
|
|
|
28
27
|
type ReinitConversationProps = {
|
|
29
28
|
key: string
|
|
@@ -44,7 +43,6 @@ export class ChatbotModel {
|
|
|
44
43
|
readonly toggleOpen = createEvent()
|
|
45
44
|
readonly $isOpen = restore(this.setIsOpen, false).on(this.toggleOpen, (isOpen) => !isOpen)
|
|
46
45
|
|
|
47
|
-
// separate messages and alternatives class?
|
|
48
46
|
public readonly removeLastMessage = createEvent()
|
|
49
47
|
private readonly setLastMessageKey = createEvent<string>()
|
|
50
48
|
public readonly $lastMessageKey = restore(this.setLastMessageKey, null)
|
|
@@ -52,7 +50,7 @@ export class ChatbotModel {
|
|
|
52
50
|
readonly setScrollActive = createEvent<boolean>()
|
|
53
51
|
readonly $isScrollActive = restore(this.setScrollActive, false)
|
|
54
52
|
|
|
55
|
-
readonly sendHelpRequestFx
|
|
53
|
+
readonly sendHelpRequestFx
|
|
56
54
|
|
|
57
55
|
readonly defaultMessages: ChatbotModelProps['messages']
|
|
58
56
|
|
|
@@ -81,12 +79,7 @@ export class ChatbotModel {
|
|
|
81
79
|
this.api = new ChatbotAPI(api)
|
|
82
80
|
|
|
83
81
|
this.defaultMessages = messages
|
|
84
|
-
if ($key) {
|
|
85
|
-
sample({
|
|
86
|
-
source: $key,
|
|
87
|
-
target: this.$currentKey,
|
|
88
|
-
})
|
|
89
|
-
}
|
|
82
|
+
if ($key) sample({ source: $key, target: this.$currentKey })
|
|
90
83
|
|
|
91
84
|
this.$alternatives = restore(this.api.getAlternativesFx.doneData, null)
|
|
92
85
|
.on(this.setAlternatives, (_, payload) => payload)
|
|
@@ -110,7 +103,7 @@ export class ChatbotModel {
|
|
|
110
103
|
}
|
|
111
104
|
|
|
112
105
|
private createSendHelpRequestFx() {
|
|
113
|
-
|
|
106
|
+
const effect = attach({
|
|
114
107
|
source: [this.$currentKey, this.translation.$isTranslated],
|
|
115
108
|
mapParams: (params: SendHelpParams, [key, isTranslated]) => ({
|
|
116
109
|
...params,
|
|
@@ -120,8 +113,8 @@ export class ChatbotModel {
|
|
|
120
113
|
effect: createControllerEffect(
|
|
121
114
|
async ({
|
|
122
115
|
payload,
|
|
123
|
-
message
|
|
124
|
-
translationTargetLanguage,
|
|
116
|
+
message,
|
|
117
|
+
translationTargetLanguage: targetLanguage,
|
|
125
118
|
translatedMessage,
|
|
126
119
|
key,
|
|
127
120
|
isTranslated,
|
|
@@ -129,87 +122,47 @@ export class ChatbotModel {
|
|
|
129
122
|
this.setLastMessageKey(key)
|
|
130
123
|
this.messages.setField({
|
|
131
124
|
key,
|
|
132
|
-
fn: (currentMessages
|
|
125
|
+
fn: (currentMessages) => [
|
|
133
126
|
...(currentMessages || []),
|
|
134
|
-
{
|
|
135
|
-
id: generateRandomId(),
|
|
136
|
-
message: message ?? '',
|
|
137
|
-
translatedMessage,
|
|
138
|
-
isOwnMessage: true,
|
|
139
|
-
threadId: payload.threadId ?? '',
|
|
140
|
-
options: [],
|
|
141
|
-
},
|
|
127
|
+
messagesHelper.createOwn({ message, translatedMessage, threadId: payload.threadId }),
|
|
142
128
|
],
|
|
143
129
|
})
|
|
144
130
|
|
|
145
131
|
const response = await this.api.postMessageFx(payload)
|
|
132
|
+
let messages = [messagesHelper.createAnswer(response)]
|
|
133
|
+
if (response.endMessage) {
|
|
134
|
+
messages.push(messagesHelper.createLast(this.defaultMessages.endMessage))
|
|
135
|
+
}
|
|
146
136
|
if (isTranslated) {
|
|
147
|
-
|
|
148
|
-
targetLanguage: translationTargetLanguage as string,
|
|
149
|
-
message: {
|
|
150
|
-
...response,
|
|
151
|
-
id: generateRandomId(),
|
|
152
|
-
isOwnMessage: false,
|
|
153
|
-
},
|
|
154
|
-
})
|
|
137
|
+
messages = await this.translation.translateMessagesFx({ targetLanguage, messages })
|
|
155
138
|
}
|
|
156
|
-
|
|
157
|
-
|
|
139
|
+
this.messages.setField({
|
|
140
|
+
key,
|
|
141
|
+
fn: (currentMessages) => [...(currentMessages || []), ...messages],
|
|
142
|
+
})
|
|
143
|
+
this.threadIds.setField({ key, data: response.threadId })
|
|
158
144
|
},
|
|
159
145
|
),
|
|
160
146
|
})
|
|
161
|
-
}
|
|
162
147
|
|
|
163
|
-
private initChatReplyWatcher() {
|
|
164
148
|
sample({
|
|
165
|
-
clock:
|
|
166
|
-
source: [this.$currentKey] as const,
|
|
167
|
-
fn: ([key], response) => ({ key, response }),
|
|
168
|
-
}).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
|
-
this.messages.setField({
|
|
191
|
-
key,
|
|
192
|
-
fn: (messages) => [...(messages || []), ...newMessages],
|
|
193
|
-
})
|
|
194
|
-
this.threadIds.setField({ key, data: response.threadId })
|
|
195
|
-
})
|
|
196
|
-
|
|
197
|
-
sample({
|
|
198
|
-
clock: this.sendHelpRequestFx.fail,
|
|
149
|
+
clock: effect.fail,
|
|
199
150
|
source: this.$currentChatData,
|
|
200
151
|
fn: ({ key, messages }) => ({ key, data: [...messages, this.defaultMessages.error] }),
|
|
201
152
|
filter: (_, { error }) => error.message !== 'Request aborted manually',
|
|
202
153
|
target: this.messages.setField,
|
|
203
154
|
})
|
|
204
155
|
|
|
156
|
+
return effect
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private initChatReplyWatcher() {
|
|
205
160
|
sample({
|
|
206
161
|
clock: this.removeLastMessage,
|
|
207
162
|
source: [this.$lastMessageKey, this.messages.$state] as const,
|
|
208
163
|
fn: ([lastMessageKey, messages]) => {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
const updatedMessages = messages[lastMessageKey]
|
|
212
|
-
if (!updatedMessages?.length) return mockData
|
|
164
|
+
if (!lastMessageKey) return { key: '', data: [] }
|
|
165
|
+
const updatedMessages = [...messages[lastMessageKey]]
|
|
213
166
|
updatedMessages.pop()
|
|
214
167
|
return { key: lastMessageKey, data: updatedMessages }
|
|
215
168
|
},
|
|
@@ -223,7 +176,6 @@ export class ChatbotModel {
|
|
|
223
176
|
mapParams: (_: void, source) => source,
|
|
224
177
|
effect: createEffect(({ key, messages }: StartConversationFxData) => {
|
|
225
178
|
if (messages.length) return this.reinitConversation({ key })
|
|
226
|
-
|
|
227
179
|
this.messages.setField({ key, data: [this.defaultMessages.start] })
|
|
228
180
|
this.availableChats.setField({ key, data: true })
|
|
229
181
|
}),
|
|
@@ -245,18 +197,8 @@ export class ChatbotModel {
|
|
|
245
197
|
key,
|
|
246
198
|
fn: (messages) => {
|
|
247
199
|
if (!messages?.length || messages.length === 1) return
|
|
248
|
-
let messagesToRemove = 1
|
|
249
|
-
let lastMessage = messages[messages.length - 1]
|
|
250
|
-
if (lastMessage.endMessage) {
|
|
251
|
-
lastMessage = messages[messages.length - 2]
|
|
252
|
-
messagesToRemove = 2
|
|
253
|
-
}
|
|
254
|
-
if (!lastMessage) return
|
|
255
200
|
const copy = [...messages]
|
|
256
|
-
copy.
|
|
257
|
-
...lastMessage,
|
|
258
|
-
options: [{ text: 'Give me another hint', promptKey: '' }],
|
|
259
|
-
})
|
|
201
|
+
while (copy.at(-1)?.endMessage) copy.pop()
|
|
260
202
|
return copy
|
|
261
203
|
},
|
|
262
204
|
})
|
|
@@ -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,58 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AddTranslationToAlternativesProps,
|
|
3
|
+
GetOptionsProps,
|
|
4
|
+
GetOptionsResult,
|
|
5
|
+
} from '../types/translation.types'
|
|
6
|
+
import { ChatAnswer, GetAlternativesResponse } from '../types/api.types'
|
|
7
|
+
|
|
8
|
+
export function getOptionsForTranslation({
|
|
9
|
+
lastMessage,
|
|
10
|
+
alternatives,
|
|
11
|
+
}: GetOptionsProps): GetOptionsResult {
|
|
12
|
+
if (!lastMessage) return { lastMessage: null, alternatives: null }
|
|
13
|
+
if (!lastMessage.options.length) {
|
|
14
|
+
if (!alternatives) return { lastMessage: null, alternatives: null }
|
|
15
|
+
const hintAlternatives = alternatives.options.hints ?? []
|
|
16
|
+
const defaultAlternatives = alternatives.options.default ?? []
|
|
17
|
+
return {
|
|
18
|
+
lastMessage: null,
|
|
19
|
+
alternatives: {
|
|
20
|
+
hints: hintAlternatives.map(({ text }) => text),
|
|
21
|
+
default: defaultAlternatives.map(({ text }) => text),
|
|
22
|
+
},
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
lastMessage: lastMessage.options.map(({ text }) => text),
|
|
28
|
+
alternatives: null,
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function addTranslationsToAlternative({
|
|
33
|
+
alternatives,
|
|
34
|
+
translatedHints,
|
|
35
|
+
translatedDefaults,
|
|
36
|
+
}: AddTranslationToAlternativesProps): GetAlternativesResponse {
|
|
37
|
+
return {
|
|
38
|
+
...alternatives,
|
|
39
|
+
options: {
|
|
40
|
+
...alternatives.options,
|
|
41
|
+
hints: translatedHints.map((translatedText, index) => ({
|
|
42
|
+
...alternatives.options.hints[index],
|
|
43
|
+
translatedText,
|
|
44
|
+
})),
|
|
45
|
+
default: translatedDefaults.map((translatedText, index) => ({
|
|
46
|
+
...alternatives.options.default[index],
|
|
47
|
+
translatedText,
|
|
48
|
+
})),
|
|
49
|
+
},
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function addTranslationToOptions(options: ChatAnswer[], translations: string[]) {
|
|
54
|
+
return options.map((option, index) => ({
|
|
55
|
+
...option,
|
|
56
|
+
translatedText: translations[index],
|
|
57
|
+
}))
|
|
58
|
+
}
|