@chatbotkit/react 1.7.0 → 1.8.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/README.md +198 -9
- package/dist/cjs/actions/complete.cjs +90 -24
- package/dist/cjs/actions/complete.d.ts +21 -7
- package/dist/cjs/components/ConversationManager.cjs +4 -19
- package/dist/cjs/hooks/useConversationManager.cjs +83 -147
- package/dist/cjs/hooks/useConversationManager.d.ts +9 -54
- package/dist/cjs/hooks/useConversationManagerRemote.cjs +83 -0
- package/dist/cjs/hooks/useConversationManagerRemote.d.ts +20 -0
- package/dist/cjs/hooks/useConversationManagerState.cjs +32 -0
- package/dist/cjs/hooks/useConversationManagerState.d.ts +10 -0
- package/dist/cjs/hooks/useConversationManagerStateReducer.cjs +112 -0
- package/dist/cjs/hooks/useConversationManagerStateReducer.d.ts +42 -0
- package/dist/cjs/utils/it.cjs +9 -0
- package/dist/cjs/utils/it.d.ts +1 -0
- package/dist/esm/actions/complete.d.ts +21 -7
- package/dist/esm/actions/complete.js +86 -20
- package/dist/esm/components/ConversationManager.js +4 -19
- package/dist/esm/hooks/useConversationManager.d.ts +9 -54
- package/dist/esm/hooks/useConversationManager.js +83 -147
- package/dist/esm/hooks/useConversationManagerRemote.d.ts +20 -0
- package/dist/esm/hooks/useConversationManagerRemote.js +78 -0
- package/dist/esm/hooks/useConversationManagerState.d.ts +10 -0
- package/dist/esm/hooks/useConversationManagerState.js +27 -0
- package/dist/esm/hooks/useConversationManagerStateReducer.d.ts +42 -0
- package/dist/esm/hooks/useConversationManagerStateReducer.js +105 -0
- package/dist/esm/utils/it.d.ts +1 -0
- package/dist/esm/utils/it.js +5 -0
- package/dist/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +82 -2
|
@@ -1,191 +1,127 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useConversationManager = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
4
5
|
const react_1 = require("react");
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
const { client: _client, endpoint, token: _token, conversationId: _conversationId, botId: _botId, backstory: _backstory, model: _model, datasetId: _datasetId, skillsetId: _skillsetId, ...rest } = options;
|
|
11
|
-
const [token, setToken] = (0, react_1.useState)(_token);
|
|
12
|
-
const [conversationId, setConversationId] = (0, react_1.useState)(_conversationId);
|
|
13
|
-
const [botId, setBotId] = (0, react_1.useState)(_botId);
|
|
14
|
-
const [backstory, setBackstory] = (0, react_1.useState)(_backstory);
|
|
15
|
-
const [model, setModel] = (0, react_1.useState)(_model);
|
|
16
|
-
const [datasetId, setDatasetId] = (0, react_1.useState)(_datasetId);
|
|
17
|
-
const [skillsetId, setSkillsetId] = (0, react_1.useState)(_skillsetId);
|
|
18
|
-
const client = (0, react_1.useMemo)(() => {
|
|
19
|
-
var _a, _b;
|
|
20
|
-
if (typeof endpoint === 'function') {
|
|
21
|
-
return {
|
|
22
|
-
complete(conversationId, options) {
|
|
23
|
-
return {
|
|
24
|
-
async *stream() {
|
|
25
|
-
yield* (0, stream_js_1.consume)(endpoint(conversationId, options));
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
|
-
},
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
const options = { ...rest, secret: token || '' };
|
|
32
|
-
let thisClient = _client || new sdk_1.ConversationClient(options);
|
|
33
|
-
const extension = {};
|
|
34
|
-
if (typeof endpoint === 'string') {
|
|
35
|
-
extension.url = new URL(((_b = (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.location) === null || _b === void 0 ? void 0 : _b.origin) || 'about:blank');
|
|
36
|
-
extension.endpoints = {
|
|
37
|
-
'/api/v1/conversation/complete': endpoint,
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
if (token) {
|
|
41
|
-
extension.secret = token;
|
|
42
|
-
}
|
|
43
|
-
if (Object.keys(extension).length === 0) {
|
|
44
|
-
return thisClient;
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
return (0, object_js_1.cloneAndExtend)(thisClient, extension);
|
|
48
|
-
}
|
|
49
|
-
}, [_client, endpoint, token]);
|
|
6
|
+
const useConversationManagerRemote_js_1 = tslib_1.__importDefault(require("./useConversationManagerRemote.cjs"));
|
|
7
|
+
const useConversationManagerState_js_1 = require("./useConversationManagerState.cjs");
|
|
8
|
+
function useConversationManager({ ...conversationManagerRemoteOptions }) {
|
|
9
|
+
const remote = (0, useConversationManagerRemote_js_1.default)(conversationManagerRemoteOptions);
|
|
10
|
+
const [{ thinking, typing, message, messages, }, { setThinking, setTyping, appendText, appendMessage, },] = (0, useConversationManagerState_js_1.useConversationManagerState)();
|
|
50
11
|
const [text, setText] = (0, react_1.useState)((''));
|
|
51
|
-
const [messages, setMessages] = (0, react_1.useState)(([]));
|
|
52
|
-
const [thinking, setThinking] = (0, react_1.useState)(false);
|
|
53
|
-
const [typing, setTyping] = (0, react_1.useState)(false);
|
|
54
12
|
const [error, setError] = (0, react_1.useState)((null));
|
|
55
13
|
async function stream(newMessages) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
catch (e) {
|
|
75
|
-
setThinking(false);
|
|
76
|
-
setError(e);
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
const botMessage = {
|
|
80
|
-
id: (0, string_js_1.getRandomId)('message-'),
|
|
81
|
-
type: 'bot',
|
|
82
|
-
text: '',
|
|
83
|
-
};
|
|
84
|
-
let alreadyStreaming = false;
|
|
14
|
+
const allMessages = [
|
|
15
|
+
...messages.map(({ type, text, meta }) => {
|
|
16
|
+
return {
|
|
17
|
+
type,
|
|
18
|
+
text,
|
|
19
|
+
meta,
|
|
20
|
+
};
|
|
21
|
+
}),
|
|
22
|
+
...((newMessages === null || newMessages === void 0 ? void 0 : newMessages.map(({ type, text, meta }) => {
|
|
23
|
+
return {
|
|
24
|
+
type,
|
|
25
|
+
text,
|
|
26
|
+
meta,
|
|
27
|
+
};
|
|
28
|
+
})) || []),
|
|
29
|
+
].slice(-100);
|
|
85
30
|
try {
|
|
86
|
-
|
|
87
|
-
|
|
31
|
+
setThinking(true);
|
|
32
|
+
setError(null);
|
|
33
|
+
for await (const item of remote(allMessages)) {
|
|
34
|
+
switch (item.type) {
|
|
88
35
|
case 'token': {
|
|
89
|
-
|
|
90
|
-
alreadyStreaming = true;
|
|
91
|
-
newMessages = [...newMessages, botMessage];
|
|
92
|
-
setMessages(newMessages);
|
|
93
|
-
setThinking(false);
|
|
94
|
-
setTyping(true);
|
|
95
|
-
}
|
|
96
|
-
botMessage.text += event.data.token;
|
|
97
|
-
setMessages([...newMessages]);
|
|
36
|
+
appendText(item.data.token);
|
|
98
37
|
break;
|
|
99
38
|
}
|
|
100
39
|
case 'message': {
|
|
101
|
-
|
|
102
|
-
if (botMessage.text !== message.text ||
|
|
103
|
-
message.type === 'activity' ||
|
|
104
|
-
typeof message.children !== 'undefined') {
|
|
105
|
-
const newMessage = {
|
|
106
|
-
id: (0, string_js_1.getRandomId)('message-'),
|
|
107
|
-
...event.data,
|
|
108
|
-
};
|
|
109
|
-
newMessages = [...newMessages, newMessage];
|
|
110
|
-
setMessages([...newMessages]);
|
|
111
|
-
}
|
|
40
|
+
appendMessage(item.data);
|
|
112
41
|
break;
|
|
113
42
|
}
|
|
114
|
-
case 'result': {
|
|
115
|
-
setThinking(false);
|
|
116
|
-
setTyping(false);
|
|
117
|
-
}
|
|
118
43
|
}
|
|
119
44
|
}
|
|
120
45
|
}
|
|
121
46
|
catch (e) {
|
|
122
47
|
setError(e);
|
|
48
|
+
if (typeof process !== 'undefined' &&
|
|
49
|
+
process.env.NODE_ENV === 'development') {
|
|
50
|
+
console.error(e);
|
|
51
|
+
}
|
|
123
52
|
}
|
|
124
53
|
finally {
|
|
54
|
+
setThinking(false);
|
|
125
55
|
setTyping(false);
|
|
126
56
|
}
|
|
127
57
|
}
|
|
128
|
-
async function submit() {
|
|
129
|
-
if (
|
|
58
|
+
async function submit(thisText) {
|
|
59
|
+
if (thinking || typing) {
|
|
130
60
|
return;
|
|
131
61
|
}
|
|
132
|
-
|
|
62
|
+
if (!thisText) {
|
|
63
|
+
if (!text) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
thisText = text;
|
|
67
|
+
setText('');
|
|
68
|
+
}
|
|
133
69
|
const userMessage = {
|
|
134
|
-
id: (0, string_js_1.getRandomId)('message-'),
|
|
135
70
|
type: 'user',
|
|
136
|
-
text:
|
|
71
|
+
text: thisText,
|
|
137
72
|
};
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
setMessages([...newMessages]);
|
|
141
|
-
await stream(newMessages);
|
|
73
|
+
appendMessage(userMessage);
|
|
74
|
+
await stream([userMessage]);
|
|
142
75
|
}
|
|
143
|
-
async function trigger(name
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
},
|
|
76
|
+
async function trigger(name) {
|
|
77
|
+
if (thinking || typing) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const activityMessage = {
|
|
81
|
+
type: 'activity',
|
|
82
|
+
text: '',
|
|
83
|
+
meta: {
|
|
84
|
+
activity: {
|
|
85
|
+
type: 'trigger',
|
|
86
|
+
function: {
|
|
87
|
+
name: name,
|
|
156
88
|
},
|
|
157
89
|
},
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
await stream(
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
await stream([activityMessage]);
|
|
93
|
+
}
|
|
94
|
+
async function request(name, args) {
|
|
95
|
+
if (thinking || typing) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const activityMessage = {
|
|
99
|
+
type: 'activity',
|
|
100
|
+
text: '',
|
|
101
|
+
meta: {
|
|
102
|
+
activity: {
|
|
103
|
+
type: 'request',
|
|
104
|
+
function: {
|
|
105
|
+
name: name,
|
|
106
|
+
arguments: args,
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
await stream([activityMessage]);
|
|
161
112
|
}
|
|
162
113
|
return {
|
|
163
|
-
|
|
164
|
-
setToken,
|
|
165
|
-
conversationId,
|
|
166
|
-
setConversationId,
|
|
167
|
-
botId,
|
|
168
|
-
setBotId,
|
|
169
|
-
backstory,
|
|
170
|
-
setBackstory,
|
|
171
|
-
model,
|
|
172
|
-
setModel,
|
|
173
|
-
datasetId,
|
|
174
|
-
setDatasetId,
|
|
175
|
-
skillsetId,
|
|
176
|
-
setSkillsetId,
|
|
177
|
-
text,
|
|
178
|
-
setText,
|
|
114
|
+
message,
|
|
179
115
|
messages,
|
|
180
|
-
setMessages,
|
|
181
116
|
thinking,
|
|
182
|
-
setThinking,
|
|
183
117
|
typing,
|
|
184
|
-
|
|
118
|
+
text,
|
|
119
|
+
setText,
|
|
185
120
|
error,
|
|
186
121
|
setError,
|
|
187
122
|
submit,
|
|
188
123
|
trigger,
|
|
124
|
+
request,
|
|
189
125
|
};
|
|
190
126
|
}
|
|
191
127
|
exports.useConversationManager = useConversationManager;
|
|
@@ -1,63 +1,18 @@
|
|
|
1
|
-
export function useConversationManager(
|
|
1
|
+
export function useConversationManager({ ...conversationManagerRemoteOptions }: UseConversationManagerOptions): UseConversationManagerResult;
|
|
2
2
|
export default useConversationManager;
|
|
3
|
-
export type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
frequencyPenalty?: number;
|
|
7
|
-
presencePenalty?: number;
|
|
8
|
-
seed?: number;
|
|
9
|
-
interactionMaxMessages?: number;
|
|
10
|
-
region?: 'us' | 'eu';
|
|
11
|
-
};
|
|
12
|
-
export type Model = string | {
|
|
13
|
-
name: string;
|
|
14
|
-
config?: ModelConfig;
|
|
15
|
-
};
|
|
16
|
-
export type Message = {
|
|
17
|
-
id?: string;
|
|
18
|
-
type: 'bot' | 'user' | 'context' | 'instruction' | 'backstory' | 'activity';
|
|
19
|
-
text: string;
|
|
20
|
-
meta?: Record<string, any>;
|
|
21
|
-
};
|
|
22
|
-
export type EndpointURL = string;
|
|
23
|
-
export type EndpointFunction = (conversationId: any, request: any) => AsyncGenerator<any>;
|
|
24
|
-
export type UseConversationManagerOptions = {
|
|
25
|
-
[key: string]: any;
|
|
26
|
-
client?: ConversationClient;
|
|
27
|
-
endpoint?: EndpointURL | EndpointFunction;
|
|
28
|
-
token?: string;
|
|
29
|
-
conversationId?: string;
|
|
30
|
-
backstory?: string;
|
|
31
|
-
Model?: string;
|
|
32
|
-
datasetId?: string;
|
|
33
|
-
skillsetId?: string;
|
|
34
|
-
};
|
|
3
|
+
export type Message = any;
|
|
4
|
+
export type UseConversationManagerRemoteOptions = import('./useConversationManagerRemote.cjs').UseConversationManagerRemoteOptions;
|
|
5
|
+
export type UseConversationManagerOptions = UseConversationManagerRemoteOptions & {};
|
|
35
6
|
export type UseConversationManagerResult = {
|
|
36
|
-
|
|
37
|
-
setToken: (token: string) => void;
|
|
38
|
-
conversationId?: string;
|
|
39
|
-
setConversationId: (conversationId: string) => void;
|
|
40
|
-
botId?: string;
|
|
41
|
-
setBotId: (botId: string) => void;
|
|
42
|
-
backstory?: string;
|
|
43
|
-
setBackstory: (backstory: string) => void;
|
|
44
|
-
model?: Model;
|
|
45
|
-
setModel: (model: Model) => void;
|
|
46
|
-
datasetId?: string;
|
|
47
|
-
setDatasetId: (datasetId: string) => void;
|
|
48
|
-
skillsetId?: string;
|
|
49
|
-
setSkillsetId: (skillsetId: string) => void;
|
|
50
|
-
text: string;
|
|
51
|
-
setText: (text: string) => void;
|
|
7
|
+
message: any;
|
|
52
8
|
messages: Message[];
|
|
53
|
-
setMessages: (messages: Message[]) => void;
|
|
54
9
|
thinking: boolean;
|
|
55
|
-
setThinking: (thinking: boolean) => void;
|
|
56
10
|
typing: boolean;
|
|
57
|
-
|
|
11
|
+
text: string;
|
|
12
|
+
setText: (text: string) => void;
|
|
58
13
|
error: any;
|
|
59
14
|
setError: (error: any) => void;
|
|
60
15
|
submit: () => void;
|
|
61
|
-
trigger: (name: string
|
|
16
|
+
trigger: (name: string) => void;
|
|
17
|
+
request: (name: string, args: any) => void;
|
|
62
18
|
};
|
|
63
|
-
import { ConversationClient } from '@chatbotkit/sdk';
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useConversationManagerRemote = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const object_js_1 = require("../utils/object.cjs");
|
|
6
|
+
const stream_js_1 = require("../utils/stream.cjs");
|
|
7
|
+
const sdk_1 = require("@chatbotkit/sdk");
|
|
8
|
+
function useConversationManagerRemote({ client: _client, endpoint, conversationId, token, backstory, model, datasetId, skillsetId, privacy, moderation, ...rest }) {
|
|
9
|
+
const client = (0, react_1.useMemo)(() => {
|
|
10
|
+
var _a, _b;
|
|
11
|
+
if (typeof endpoint === 'function') {
|
|
12
|
+
return {
|
|
13
|
+
complete(conversationId, options) {
|
|
14
|
+
return {
|
|
15
|
+
async *stream() {
|
|
16
|
+
yield* (0, stream_js_1.consume)(endpoint(options));
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
const options = { ...rest, secret: token || '' };
|
|
23
|
+
let thisClient = _client || new sdk_1.ConversationClient(options);
|
|
24
|
+
const extension = {};
|
|
25
|
+
if (typeof endpoint === 'string') {
|
|
26
|
+
extension.url = new URL(((_b = (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.location) === null || _b === void 0 ? void 0 : _b.origin) || 'about:blank');
|
|
27
|
+
extension.endpoints = {
|
|
28
|
+
'/api/v1/conversation/complete': endpoint,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
if (token) {
|
|
32
|
+
extension.secret = token;
|
|
33
|
+
}
|
|
34
|
+
if (Object.keys(extension).length === 0) {
|
|
35
|
+
return thisClient;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
return (0, object_js_1.cloneAndExtend)(thisClient, extension);
|
|
39
|
+
}
|
|
40
|
+
}, [_client, endpoint, token]);
|
|
41
|
+
const remote = (0, react_1.useMemo)(() => {
|
|
42
|
+
if (conversationId) {
|
|
43
|
+
return (async function* (messages) {
|
|
44
|
+
const lastUserMessage = [...messages]
|
|
45
|
+
.reverse()
|
|
46
|
+
.find((message) => message.type === 'user');
|
|
47
|
+
if (!lastUserMessage) {
|
|
48
|
+
throw new Error('No user message found');
|
|
49
|
+
}
|
|
50
|
+
yield* client
|
|
51
|
+
.complete(conversationId, { text: lastUserMessage.text })
|
|
52
|
+
.stream();
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
return (async function* (messages) {
|
|
57
|
+
yield* client
|
|
58
|
+
.complete(null, {
|
|
59
|
+
backstory: backstory,
|
|
60
|
+
model: model,
|
|
61
|
+
datasetId: datasetId,
|
|
62
|
+
skillsetId: skillsetId,
|
|
63
|
+
privacy: privacy,
|
|
64
|
+
moderation: moderation,
|
|
65
|
+
messages: messages,
|
|
66
|
+
})
|
|
67
|
+
.stream();
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}, [
|
|
71
|
+
client,
|
|
72
|
+
conversationId,
|
|
73
|
+
backstory,
|
|
74
|
+
model,
|
|
75
|
+
datasetId,
|
|
76
|
+
skillsetId,
|
|
77
|
+
privacy,
|
|
78
|
+
moderation,
|
|
79
|
+
]);
|
|
80
|
+
return remote;
|
|
81
|
+
}
|
|
82
|
+
exports.useConversationManagerRemote = useConversationManagerRemote;
|
|
83
|
+
exports.default = useConversationManagerRemote;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export function useConversationManagerRemote({ client: _client, endpoint, conversationId, token, backstory, model, datasetId, skillsetId, privacy, moderation, ...rest }: UseConversationManagerRemoteOptions): UseConversationManagerRemoteResult;
|
|
2
|
+
export default useConversationManagerRemote;
|
|
3
|
+
export type Message = any;
|
|
4
|
+
export type Model = any;
|
|
5
|
+
export type EndpointURL = string;
|
|
6
|
+
export type EndpointFunction = (options: any) => AsyncGenerator<any>;
|
|
7
|
+
export type UseConversationManagerRemoteOptions = {
|
|
8
|
+
client?: ConversationClient;
|
|
9
|
+
endpoint?: EndpointURL | EndpointFunction;
|
|
10
|
+
conversationId?: string;
|
|
11
|
+
token?: string;
|
|
12
|
+
backstory?: string;
|
|
13
|
+
model?: Model;
|
|
14
|
+
datasetId?: string;
|
|
15
|
+
skillsetId?: string;
|
|
16
|
+
privacy?: boolean;
|
|
17
|
+
moderation?: boolean;
|
|
18
|
+
};
|
|
19
|
+
export type UseConversationManagerRemoteResult = (messages: Message[]) => AsyncGenerator<any, void, any>;
|
|
20
|
+
import { ConversationClient } from '@chatbotkit/sdk';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useConversationManagerState = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const useConversationManagerStateReducer_js_1 = tslib_1.__importDefault(require("./useConversationManagerStateReducer.cjs"));
|
|
7
|
+
function useConversationManagerState(state) {
|
|
8
|
+
const [_, dispatch] = (0, useConversationManagerStateReducer_js_1.default)(state);
|
|
9
|
+
const setThinking = (0, react_1.useCallback)((thinking) => {
|
|
10
|
+
dispatch({ type: 'setThinking', data: { thinking } });
|
|
11
|
+
}, [dispatch]);
|
|
12
|
+
const setTyping = (0, react_1.useCallback)((typing) => {
|
|
13
|
+
dispatch({ type: 'setTyping', data: { typing } });
|
|
14
|
+
}, [dispatch]);
|
|
15
|
+
const appendText = (0, react_1.useCallback)((text) => {
|
|
16
|
+
dispatch({ type: 'appendText', data: { text } });
|
|
17
|
+
}, [dispatch]);
|
|
18
|
+
const appendMessage = (0, react_1.useCallback)((message) => {
|
|
19
|
+
dispatch({ type: 'appendMessage', data: { message } });
|
|
20
|
+
}, [dispatch]);
|
|
21
|
+
return [
|
|
22
|
+
_,
|
|
23
|
+
{
|
|
24
|
+
setThinking,
|
|
25
|
+
setTyping,
|
|
26
|
+
appendText,
|
|
27
|
+
appendMessage,
|
|
28
|
+
},
|
|
29
|
+
];
|
|
30
|
+
}
|
|
31
|
+
exports.useConversationManagerState = useConversationManagerState;
|
|
32
|
+
exports.default = useConversationManagerState;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function useConversationManagerState(state?: Partial<State>): [State, StateFunctions];
|
|
2
|
+
export default useConversationManagerState;
|
|
3
|
+
export type Message = any;
|
|
4
|
+
export type State = import('./useConversationManagerStateReducer.cjs').State;
|
|
5
|
+
export type StateFunctions = {
|
|
6
|
+
setThinking: (thinking: boolean) => void;
|
|
7
|
+
setTyping: (typing: boolean) => void;
|
|
8
|
+
appendText: (text: string) => void;
|
|
9
|
+
appendMessage: (message: any) => void;
|
|
10
|
+
};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useConversationManagerStateReducer = exports.conversationManagerStateInitial = exports.conversationManagerStateReducer = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const string_js_1 = require("../utils/string.cjs");
|
|
6
|
+
function conversationManagerStateReducer(state, action) {
|
|
7
|
+
var _a;
|
|
8
|
+
switch (action.type) {
|
|
9
|
+
case 'setThinking': {
|
|
10
|
+
const { thinking } = action.data;
|
|
11
|
+
const extra = {};
|
|
12
|
+
if (thinking) {
|
|
13
|
+
extra.typing = false;
|
|
14
|
+
extra.message = null;
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
...state,
|
|
18
|
+
...extra,
|
|
19
|
+
thinking,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
case 'setTyping': {
|
|
23
|
+
const { typing } = action.data;
|
|
24
|
+
const extra = {};
|
|
25
|
+
if (typing) {
|
|
26
|
+
extra.thinking = false;
|
|
27
|
+
extra.message = null;
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
...state,
|
|
31
|
+
...extra,
|
|
32
|
+
typing,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
case 'appendText': {
|
|
36
|
+
const { text } = action.data;
|
|
37
|
+
const extra = {
|
|
38
|
+
thinking: false,
|
|
39
|
+
typing: true,
|
|
40
|
+
};
|
|
41
|
+
const message = state.message
|
|
42
|
+
? ({ ...state.message })
|
|
43
|
+
: ({
|
|
44
|
+
id: (0, string_js_1.getRandomId)('tmp-'),
|
|
45
|
+
type: 'bot',
|
|
46
|
+
text: '',
|
|
47
|
+
});
|
|
48
|
+
message.text += text;
|
|
49
|
+
return {
|
|
50
|
+
...state,
|
|
51
|
+
...extra,
|
|
52
|
+
message,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
case 'appendMessage': {
|
|
56
|
+
const { message } = action.data;
|
|
57
|
+
const extra = {};
|
|
58
|
+
if (message.type === 'bot') {
|
|
59
|
+
if (message.text === ((_a = state.message) === null || _a === void 0 ? void 0 : _a.text)) {
|
|
60
|
+
extra.typing = false;
|
|
61
|
+
extra.message = null;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const messages = state.messages.slice(0);
|
|
65
|
+
if (message.id) {
|
|
66
|
+
const index = messages.findIndex((m) => m.id === message.id);
|
|
67
|
+
if (index !== -1) {
|
|
68
|
+
messages[index] = {
|
|
69
|
+
...messages[index],
|
|
70
|
+
...message,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
messages.push({
|
|
75
|
+
...message,
|
|
76
|
+
id: message.id || (0, string_js_1.getRandomId)('tmp-'),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
messages.push({
|
|
82
|
+
...message,
|
|
83
|
+
id: (0, string_js_1.getRandomId)('tmp-'),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
...state,
|
|
88
|
+
...extra,
|
|
89
|
+
messages,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
default: {
|
|
93
|
+
throw new Error(`Unhandled action`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
exports.conversationManagerStateReducer = conversationManagerStateReducer;
|
|
98
|
+
function conversationManagerStateInitial(state) {
|
|
99
|
+
return {
|
|
100
|
+
message: null,
|
|
101
|
+
messages: [],
|
|
102
|
+
thinking: false,
|
|
103
|
+
typing: false,
|
|
104
|
+
...state,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
exports.conversationManagerStateInitial = conversationManagerStateInitial;
|
|
108
|
+
function useConversationManagerStateReducer(state) {
|
|
109
|
+
return (0, react_1.useReducer)(conversationManagerStateReducer, state, conversationManagerStateInitial);
|
|
110
|
+
}
|
|
111
|
+
exports.useConversationManagerStateReducer = useConversationManagerStateReducer;
|
|
112
|
+
exports.default = useConversationManagerStateReducer;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export function conversationManagerStateReducer(state: State, action: Action): State;
|
|
2
|
+
export function conversationManagerStateInitial(state?: Partial<State>): State;
|
|
3
|
+
export function useConversationManagerStateReducer(state?: Partial<State>): [State, import('react').Dispatch<Action>];
|
|
4
|
+
export default useConversationManagerStateReducer;
|
|
5
|
+
export type Message = any;
|
|
6
|
+
export type MessageWithId = {
|
|
7
|
+
id: string;
|
|
8
|
+
} & Message;
|
|
9
|
+
export type MessageWithOptionalId = {
|
|
10
|
+
id?: string;
|
|
11
|
+
} & Message;
|
|
12
|
+
export type State = {
|
|
13
|
+
thinking: boolean;
|
|
14
|
+
typing: boolean;
|
|
15
|
+
message: MessageWithId | null;
|
|
16
|
+
messages: MessageWithId[];
|
|
17
|
+
};
|
|
18
|
+
export type SetThinkingAction = {
|
|
19
|
+
type: 'setThinking';
|
|
20
|
+
data: {
|
|
21
|
+
thinking: boolean;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
export type SetTypingAction = {
|
|
25
|
+
type: 'setTyping';
|
|
26
|
+
data: {
|
|
27
|
+
typing: boolean;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
export type AppendTextAction = {
|
|
31
|
+
type: 'appendText';
|
|
32
|
+
data: {
|
|
33
|
+
text: string;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
export type AppendMessageAction = {
|
|
37
|
+
type: 'appendMessage';
|
|
38
|
+
data: {
|
|
39
|
+
message: MessageWithOptionalId;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
export type Action = SetThinkingAction | SetTypingAction | AppendTextAction | AppendMessageAction;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isAsyncGenerator = void 0;
|
|
4
|
+
function isAsyncGenerator(value) {
|
|
5
|
+
return (typeof value === 'object' &&
|
|
6
|
+
value !== null &&
|
|
7
|
+
typeof value[Symbol.asyncIterator] === 'function');
|
|
8
|
+
}
|
|
9
|
+
exports.isAsyncGenerator = isAsyncGenerator;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function isAsyncGenerator(value: any): boolean;
|