@chatbotkit/react 1.6.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 +178 -0
- package/dist/cjs/actions/complete.d.ts +38 -0
- package/dist/cjs/components/ConversationManager.cjs +26 -0
- package/dist/cjs/components/ConversationManager.d.ts +5 -0
- package/dist/cjs/hooks/useConversationManager.cjs +99 -114
- package/dist/cjs/hooks/useConversationManager.d.ts +13 -52
- 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/index.cjs +4 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/utils/it.cjs +9 -0
- package/dist/cjs/utils/it.d.ts +1 -0
- package/dist/cjs/utils/stream.cjs +2 -2
- package/dist/cjs/utils/stream.d.ts +6 -7
- package/dist/esm/actions/complete.d.ts +38 -0
- package/dist/esm/actions/complete.js +173 -0
- package/dist/esm/components/AutoScroller.js +1 -1
- package/dist/esm/components/ConversationManager.d.ts +5 -0
- package/dist/esm/components/ConversationManager.js +21 -0
- package/dist/esm/hooks/useConversationManager.d.ts +13 -52
- package/dist/esm/hooks/useConversationManager.js +99 -114
- 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/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/utils/it.d.ts +1 -0
- package/dist/esm/utils/it.js +5 -0
- package/dist/esm/utils/stream.d.ts +6 -7
- package/dist/esm/utils/stream.js +2 -2
- package/dist/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +122 -2
|
@@ -1,142 +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, 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 [backstory, setBackstory] = (0, react_1.useState)(_backstory);
|
|
14
|
-
const [model, setModel] = (0, react_1.useState)(_model);
|
|
15
|
-
const [datasetId, setDatasetId] = (0, react_1.useState)(_datasetId);
|
|
16
|
-
const [skillsetId, setSkillsetId] = (0, react_1.useState)(_skillsetId);
|
|
17
|
-
const client = (0, react_1.useMemo)(() => {
|
|
18
|
-
var _a, _b;
|
|
19
|
-
if (typeof endpoint === 'function') {
|
|
20
|
-
return {
|
|
21
|
-
complete(conversationId, options) {
|
|
22
|
-
return {
|
|
23
|
-
async *stream() {
|
|
24
|
-
yield* (0, stream_js_1.consume)(endpoint(conversationId, options));
|
|
25
|
-
},
|
|
26
|
-
};
|
|
27
|
-
},
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
const options = { ...rest, secret: token || '' };
|
|
31
|
-
let thisClient = _client || new sdk_1.ConversationClient(options);
|
|
32
|
-
const extension = {};
|
|
33
|
-
if (typeof endpoint === 'string') {
|
|
34
|
-
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');
|
|
35
|
-
extension.endpoints = {
|
|
36
|
-
'/api/v1/conversation/complete': endpoint,
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
if (token) {
|
|
40
|
-
extension.secret = token;
|
|
41
|
-
}
|
|
42
|
-
if (Object.keys(extension).length === 0) {
|
|
43
|
-
return thisClient;
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
return (0, object_js_1.cloneAndExtend)(thisClient, extension);
|
|
47
|
-
}
|
|
48
|
-
}, [_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)();
|
|
49
11
|
const [text, setText] = (0, react_1.useState)((''));
|
|
50
|
-
const [messages, setMessages] = (0, react_1.useState)(([]));
|
|
51
|
-
const [thinking, setThinking] = (0, react_1.useState)(false);
|
|
52
|
-
const [typing, setTyping] = (0, react_1.useState)(false);
|
|
53
12
|
const [error, setError] = (0, react_1.useState)((null));
|
|
54
|
-
async function
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
13
|
+
async function stream(newMessages) {
|
|
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);
|
|
69
30
|
try {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
catch (e) {
|
|
84
|
-
setThinking(false);
|
|
85
|
-
setError(e);
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
const botMessage = {
|
|
89
|
-
id: (0, string_js_1.getRandomId)('message-'),
|
|
90
|
-
type: 'bot',
|
|
91
|
-
text: '',
|
|
92
|
-
};
|
|
93
|
-
let alreadyStreaming = false;
|
|
94
|
-
try {
|
|
95
|
-
for await (const event of it.stream()) {
|
|
96
|
-
if (event.type === 'token') {
|
|
97
|
-
if (!alreadyStreaming) {
|
|
98
|
-
alreadyStreaming = true;
|
|
99
|
-
newMessages = [...newMessages, botMessage];
|
|
100
|
-
setMessages(newMessages);
|
|
101
|
-
setThinking(false);
|
|
102
|
-
setTyping(true);
|
|
31
|
+
setThinking(true);
|
|
32
|
+
setError(null);
|
|
33
|
+
for await (const item of remote(allMessages)) {
|
|
34
|
+
switch (item.type) {
|
|
35
|
+
case 'token': {
|
|
36
|
+
appendText(item.data.token);
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
case 'message': {
|
|
40
|
+
appendMessage(item.data);
|
|
41
|
+
break;
|
|
103
42
|
}
|
|
104
|
-
botMessage.text += event.data.token;
|
|
105
|
-
setMessages([...newMessages]);
|
|
106
43
|
}
|
|
107
44
|
}
|
|
108
45
|
}
|
|
109
46
|
catch (e) {
|
|
110
47
|
setError(e);
|
|
48
|
+
if (typeof process !== 'undefined' &&
|
|
49
|
+
process.env.NODE_ENV === 'development') {
|
|
50
|
+
console.error(e);
|
|
51
|
+
}
|
|
111
52
|
}
|
|
112
53
|
finally {
|
|
54
|
+
setThinking(false);
|
|
113
55
|
setTyping(false);
|
|
114
56
|
}
|
|
115
57
|
}
|
|
58
|
+
async function submit(thisText) {
|
|
59
|
+
if (thinking || typing) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (!thisText) {
|
|
63
|
+
if (!text) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
thisText = text;
|
|
67
|
+
setText('');
|
|
68
|
+
}
|
|
69
|
+
const userMessage = {
|
|
70
|
+
type: 'user',
|
|
71
|
+
text: thisText,
|
|
72
|
+
};
|
|
73
|
+
appendMessage(userMessage);
|
|
74
|
+
await stream([userMessage]);
|
|
75
|
+
}
|
|
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,
|
|
88
|
+
},
|
|
89
|
+
},
|
|
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]);
|
|
112
|
+
}
|
|
116
113
|
return {
|
|
117
|
-
|
|
118
|
-
setToken,
|
|
119
|
-
conversationId,
|
|
120
|
-
setConversationId,
|
|
121
|
-
backstory,
|
|
122
|
-
setBackstory,
|
|
123
|
-
model,
|
|
124
|
-
setModel,
|
|
125
|
-
datasetId,
|
|
126
|
-
setDatasetId,
|
|
127
|
-
skillsetId,
|
|
128
|
-
setSkillsetId,
|
|
129
|
-
text,
|
|
130
|
-
setText,
|
|
114
|
+
message,
|
|
131
115
|
messages,
|
|
132
|
-
setMessages,
|
|
133
116
|
thinking,
|
|
134
|
-
setThinking,
|
|
135
117
|
typing,
|
|
136
|
-
|
|
118
|
+
text,
|
|
119
|
+
setText,
|
|
137
120
|
error,
|
|
138
121
|
setError,
|
|
139
122
|
submit,
|
|
123
|
+
trigger,
|
|
124
|
+
request,
|
|
140
125
|
};
|
|
141
126
|
}
|
|
142
127
|
exports.useConversationManager = useConversationManager;
|
|
@@ -1,57 +1,18 @@
|
|
|
1
|
-
export function useConversationManager(
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
Model?: string;
|
|
9
|
-
datasetId?: string;
|
|
10
|
-
skillsetId?: string;
|
|
11
|
-
}): {
|
|
12
|
-
token: string;
|
|
13
|
-
setToken: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
14
|
-
conversationId: string;
|
|
15
|
-
setConversationId: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
16
|
-
backstory: string;
|
|
17
|
-
setBackstory: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
18
|
-
model: any;
|
|
19
|
-
setModel: import("react").Dispatch<any>;
|
|
20
|
-
datasetId: string;
|
|
21
|
-
setDatasetId: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
22
|
-
skillsetId: string;
|
|
23
|
-
setSkillsetId: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
24
|
-
text: string;
|
|
25
|
-
setText: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
1
|
+
export function useConversationManager({ ...conversationManagerRemoteOptions }: UseConversationManagerOptions): UseConversationManagerResult;
|
|
2
|
+
export default useConversationManager;
|
|
3
|
+
export type Message = any;
|
|
4
|
+
export type UseConversationManagerRemoteOptions = import('./useConversationManagerRemote.cjs').UseConversationManagerRemoteOptions;
|
|
5
|
+
export type UseConversationManagerOptions = UseConversationManagerRemoteOptions & {};
|
|
6
|
+
export type UseConversationManagerResult = {
|
|
7
|
+
message: any;
|
|
26
8
|
messages: Message[];
|
|
27
|
-
setMessages: import("react").Dispatch<import("react").SetStateAction<Message[]>>;
|
|
28
9
|
thinking: boolean;
|
|
29
|
-
setThinking: import("react").Dispatch<import("react").SetStateAction<boolean>>;
|
|
30
10
|
typing: boolean;
|
|
31
|
-
setTyping: import("react").Dispatch<import("react").SetStateAction<boolean>>;
|
|
32
|
-
error: any;
|
|
33
|
-
setError: import("react").Dispatch<any>;
|
|
34
|
-
submit: () => Promise<void>;
|
|
35
|
-
};
|
|
36
|
-
export default useConversationManager;
|
|
37
|
-
export type ModelConfig = {
|
|
38
|
-
maxTokens?: number;
|
|
39
|
-
temperature?: number;
|
|
40
|
-
frequencyPenalty?: number;
|
|
41
|
-
presencePenalty?: number;
|
|
42
|
-
seed?: number;
|
|
43
|
-
interactionMaxMessages?: number;
|
|
44
|
-
region?: 'us' | 'eu';
|
|
45
|
-
};
|
|
46
|
-
export type Model = string | {
|
|
47
|
-
name: string;
|
|
48
|
-
config?: ModelConfig;
|
|
49
|
-
};
|
|
50
|
-
export type Message = {
|
|
51
|
-
id: string;
|
|
52
|
-
type: 'bot' | 'user';
|
|
53
11
|
text: string;
|
|
12
|
+
setText: (text: string) => void;
|
|
13
|
+
error: any;
|
|
14
|
+
setError: (error: any) => void;
|
|
15
|
+
submit: () => void;
|
|
16
|
+
trigger: (name: string) => void;
|
|
17
|
+
request: (name: string, args: any) => void;
|
|
54
18
|
};
|
|
55
|
-
export type EndpointURL = string;
|
|
56
|
-
export type EndpointFunction = (conversationId: any, request: any) => AsyncGenerator<any>;
|
|
57
|
-
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;
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useConversationManager = exports.AutoTextarea = exports.ChatMessages = exports.ChatMessage = exports.ChatInput = void 0;
|
|
3
|
+
exports.useConversationManager = exports.ConversationContext = exports.ConversationManager = exports.AutoTextarea = exports.ChatMessages = exports.ChatMessage = exports.ChatInput = void 0;
|
|
4
4
|
var ChatInput_js_1 = require("./components/ChatInput.cjs");
|
|
5
5
|
Object.defineProperty(exports, "ChatInput", { enumerable: true, get: function () { return ChatInput_js_1.ChatInput; } });
|
|
6
6
|
var ChatMessage_js_1 = require("./components/ChatMessage.cjs");
|
|
@@ -9,5 +9,8 @@ var ChatMessages_js_1 = require("./components/ChatMessages.cjs");
|
|
|
9
9
|
Object.defineProperty(exports, "ChatMessages", { enumerable: true, get: function () { return ChatMessages_js_1.ChatMessages; } });
|
|
10
10
|
var AutoTextarea_js_1 = require("./components/AutoTextarea.cjs");
|
|
11
11
|
Object.defineProperty(exports, "AutoTextarea", { enumerable: true, get: function () { return AutoTextarea_js_1.AutoTextarea; } });
|
|
12
|
+
var ConversationManager_js_1 = require("./components/ConversationManager.cjs");
|
|
13
|
+
Object.defineProperty(exports, "ConversationManager", { enumerable: true, get: function () { return ConversationManager_js_1.ConversationManager; } });
|
|
14
|
+
Object.defineProperty(exports, "ConversationContext", { enumerable: true, get: function () { return ConversationManager_js_1.ConversationContext; } });
|
|
12
15
|
var useConversationManager_js_1 = require("./hooks/useConversationManager.cjs");
|
|
13
16
|
Object.defineProperty(exports, "useConversationManager", { enumerable: true, get: function () { return useConversationManager_js_1.useConversationManager; } });
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -3,3 +3,4 @@ export { ChatMessage } from "./components/ChatMessage.js";
|
|
|
3
3
|
export { ChatMessages } from "./components/ChatMessages.js";
|
|
4
4
|
export { AutoTextarea } from "./components/AutoTextarea.js";
|
|
5
5
|
export { useConversationManager } from "./hooks/useConversationManager.js";
|
|
6
|
+
export { ConversationManager, ConversationContext } from "./components/ConversationManager.js";
|
|
@@ -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;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.consume = exports.stream = void 0;
|
|
4
|
-
|
|
4
|
+
function stream(source) {
|
|
5
5
|
let it;
|
|
6
6
|
if ('next' in source && typeof source.next === 'function') {
|
|
7
7
|
it = source.next();
|
|
@@ -10,7 +10,7 @@ async function stream(source) {
|
|
|
10
10
|
return stream(source.stream());
|
|
11
11
|
}
|
|
12
12
|
else {
|
|
13
|
-
throw new Error('
|
|
13
|
+
throw new Error('Invalid source');
|
|
14
14
|
}
|
|
15
15
|
return new Promise((resolve, reject) => {
|
|
16
16
|
it.then((res) => {
|