@chatbotkit/react 1.14.3 → 1.15.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 +0 -1
- package/dist/cjs/components/AutoTextarea.cjs +45 -29
- package/dist/cjs/components/AutoTextarea.d.ts +6 -2
- package/dist/cjs/hooks/useConversationManager.cjs +2 -2
- package/dist/cjs/hooks/useConversationManager.d.ts +6 -2
- package/dist/cjs/hooks/useConversationManagerRemote.cjs +12 -4
- package/dist/cjs/hooks/useConversationManagerRemote.d.ts +4 -2
- package/dist/cjs/hooks/useConversationManagerState.d.ts +4 -5
- package/dist/cjs/hooks/useConversationManagerStateReducer.cjs +3 -0
- package/dist/cjs/hooks/useConversationManagerStateReducer.d.ts +9 -9
- package/dist/esm/components/AutoTextarea.d.ts +6 -2
- package/dist/esm/components/AutoTextarea.js +46 -30
- package/dist/esm/hooks/useConversationManager.d.ts +6 -2
- package/dist/esm/hooks/useConversationManager.js +2 -2
- package/dist/esm/hooks/useConversationManagerRemote.d.ts +4 -2
- package/dist/esm/hooks/useConversationManagerRemote.js +12 -4
- package/dist/esm/hooks/useConversationManagerState.d.ts +4 -5
- package/dist/esm/hooks/useConversationManagerStateReducer.d.ts +9 -9
- package/dist/esm/hooks/useConversationManagerStateReducer.js +3 -0
- package/dist/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
[](https://www.npmjs.com/package/@chatbotkit/react)
|
|
5
5
|
[](mailto:support@chatbotkit.com)
|
|
6
6
|
[](https://go.cbk.ai/discord)
|
|
7
|
-
[](https://go.cbk.ai/slack)
|
|
8
7
|
|
|
9
8
|
# ChatBotKit React SDK
|
|
10
9
|
|
|
@@ -5,36 +5,52 @@ exports.AutoTextarea = void 0;
|
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
6
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
7
7
|
const react_1 = tslib_1.__importStar(require("react"));
|
|
8
|
-
function AutoTextarea(props) {
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
.join(' + ') || '0px'})`;
|
|
14
|
-
textarea.style.height = 'auto';
|
|
15
|
-
textarea.style.height = `calc(${textarea.scrollHeight}px - ${adjustment})`;
|
|
16
|
-
}
|
|
8
|
+
function AutoTextarea(props, forwardedRef) {
|
|
9
|
+
const localRef = (0, react_1.useRef)(null);
|
|
10
|
+
(0, react_1.useImperativeHandle)(forwardedRef, () => {
|
|
11
|
+
return (localRef.current);
|
|
12
|
+
});
|
|
17
13
|
(0, react_1.useEffect)(() => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
});
|
|
29
|
-
observer.observe(textarea, {
|
|
30
|
-
childList: true,
|
|
31
|
-
subtree: true,
|
|
32
|
-
characterData: true,
|
|
33
|
-
});
|
|
34
|
-
return () => observer.disconnect();
|
|
14
|
+
if (!localRef.current) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const textarea = (localRef.current);
|
|
18
|
+
function recalibrate() {
|
|
19
|
+
const adjustment = `calc(${[textarea.style.paddingTop, textarea.style.paddingBottom]
|
|
20
|
+
.filter((f) => f)
|
|
21
|
+
.join(' + ') || '0px'})`;
|
|
22
|
+
textarea.style.height = 'auto';
|
|
23
|
+
textarea.style.height = `calc(${textarea.scrollHeight}px - ${adjustment})`;
|
|
35
24
|
}
|
|
36
|
-
|
|
37
|
-
|
|
25
|
+
recalibrate();
|
|
26
|
+
const mutationObserver = new MutationObserver((mutationsList) => {
|
|
27
|
+
for (const mutation of mutationsList) {
|
|
28
|
+
if (mutation.type === 'childList' ||
|
|
29
|
+
mutation.type === 'subtree' ||
|
|
30
|
+
mutation.type === 'characterData') {
|
|
31
|
+
recalibrate();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
mutationObserver.observe(textarea, {
|
|
36
|
+
childList: true,
|
|
37
|
+
subtree: true,
|
|
38
|
+
characterData: true,
|
|
39
|
+
});
|
|
40
|
+
const resizeObserver = new ResizeObserver(recalibrate);
|
|
41
|
+
resizeObserver.observe(textarea);
|
|
42
|
+
textarea.addEventListener('input', recalibrate);
|
|
43
|
+
textarea.addEventListener('focus', recalibrate);
|
|
44
|
+
window.addEventListener('resize', recalibrate);
|
|
45
|
+
return () => {
|
|
46
|
+
mutationObserver.disconnect();
|
|
47
|
+
resizeObserver.disconnect();
|
|
48
|
+
textarea.removeEventListener('input', recalibrate);
|
|
49
|
+
textarea.removeEventListener('focus', recalibrate);
|
|
50
|
+
window.removeEventListener('resize', recalibrate);
|
|
51
|
+
};
|
|
52
|
+
}, [localRef]);
|
|
53
|
+
return (0, jsx_runtime_1.jsx)("textarea", { ref: localRef, rows: 1, ...props });
|
|
38
54
|
}
|
|
39
55
|
exports.AutoTextarea = AutoTextarea;
|
|
40
|
-
exports.default = AutoTextarea;
|
|
56
|
+
exports.default = (0, react_1.forwardRef)(AutoTextarea);
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
export function AutoTextarea(props?: {
|
|
2
2
|
[name: string]: any;
|
|
3
|
-
}): import("react/jsx-runtime").JSX.Element;
|
|
4
|
-
|
|
3
|
+
}, forwardedRef?: React.Ref<HTMLTextAreaElement>): import("react/jsx-runtime").JSX.Element;
|
|
4
|
+
declare const _default: React.ForwardRefExoticComponent<Omit<{
|
|
5
|
+
[name: string]: any;
|
|
6
|
+
}, "ref"> & React.RefAttributes<HTMLTextAreaElement>>;
|
|
7
|
+
export default _default;
|
|
8
|
+
import React from 'react';
|
|
@@ -6,7 +6,7 @@ const tslib_1 = require("tslib");
|
|
|
6
6
|
const react_1 = require("react");
|
|
7
7
|
const useConversationManagerRemote_js_1 = tslib_1.__importDefault(require("./useConversationManagerRemote.cjs"));
|
|
8
8
|
const useConversationManagerState_js_1 = require("./useConversationManagerState.cjs");
|
|
9
|
-
function useConversationManager({ ...conversationManagerRemoteOptions }) {
|
|
9
|
+
function useConversationManager({ interactionMaxMessages = 100, ...conversationManagerRemoteOptions }) {
|
|
10
10
|
const remote = (0, useConversationManagerRemote_js_1.default)(conversationManagerRemoteOptions);
|
|
11
11
|
const [{ thinking, typing, message, messages, }, { setThinking, setTyping, appendText, appendMessage, },] = (0, useConversationManagerState_js_1.useConversationManagerState)();
|
|
12
12
|
const [text, setText] = (0, react_1.useState)((''));
|
|
@@ -27,7 +27,7 @@ function useConversationManager({ ...conversationManagerRemoteOptions }) {
|
|
|
27
27
|
meta,
|
|
28
28
|
};
|
|
29
29
|
})) || []),
|
|
30
|
-
].slice(-
|
|
30
|
+
].slice(-Math.abs(interactionMaxMessages));
|
|
31
31
|
try {
|
|
32
32
|
setThinking(true);
|
|
33
33
|
setError(null);
|
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
export function useConversationManager({ ...conversationManagerRemoteOptions }: UseConversationManagerOptions): UseConversationManagerResult;
|
|
1
|
+
export function useConversationManager({ interactionMaxMessages, ...conversationManagerRemoteOptions }: UseConversationManagerOptions): UseConversationManagerResult;
|
|
2
2
|
export default useConversationManager;
|
|
3
3
|
export type Message = any;
|
|
4
4
|
export type SimpleMessage = Message & {
|
|
5
5
|
id: string;
|
|
6
|
+
createdAt: Date;
|
|
6
7
|
};
|
|
7
8
|
export type ComplexMessage = Message & {
|
|
8
9
|
id: string;
|
|
10
|
+
createdAt: Date;
|
|
9
11
|
children?: import('react').ReactNode;
|
|
10
12
|
};
|
|
11
13
|
export type UseConversationManagerRemoteOptions = import('./useConversationManagerRemote.cjs').UseConversationManagerRemoteOptions;
|
|
12
|
-
export type UseConversationManagerOptions = UseConversationManagerRemoteOptions & {
|
|
14
|
+
export type UseConversationManagerOptions = UseConversationManagerRemoteOptions & {
|
|
15
|
+
interactionMaxMessages?: number;
|
|
16
|
+
};
|
|
13
17
|
export type UseConversationManagerResult = {
|
|
14
18
|
message: SimpleMessage | null;
|
|
15
19
|
messages: ComplexMessage[];
|
|
@@ -6,7 +6,7 @@ const react_1 = require("react");
|
|
|
6
6
|
const object_js_1 = require("../utils/object.cjs");
|
|
7
7
|
const stream_js_1 = require("../utils/stream.cjs");
|
|
8
8
|
const sdk_1 = require("@chatbotkit/sdk");
|
|
9
|
-
function useConversationManagerRemote({ client: _client, endpoint, conversationId,
|
|
9
|
+
function useConversationManagerRemote({ client: _client, endpoint, token, conversationId, stateful = conversationId ? true : false, botId, backstory, model, datasetId, skillsetId, privacy, moderation, ...rest }) {
|
|
10
10
|
const client = (0, react_1.useMemo)(() => {
|
|
11
11
|
var _a, _b;
|
|
12
12
|
if (typeof endpoint === 'function') {
|
|
@@ -27,6 +27,7 @@ function useConversationManagerRemote({ client: _client, endpoint, conversationI
|
|
|
27
27
|
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');
|
|
28
28
|
extension.endpoints = {
|
|
29
29
|
'/api/v1/conversation/complete': endpoint,
|
|
30
|
+
'/api/v1/conversation/{conversationId}/complete': endpoint,
|
|
30
31
|
};
|
|
31
32
|
}
|
|
32
33
|
if (token) {
|
|
@@ -40,7 +41,7 @@ function useConversationManagerRemote({ client: _client, endpoint, conversationI
|
|
|
40
41
|
}
|
|
41
42
|
}, [_client, endpoint, token]);
|
|
42
43
|
const remote = (0, react_1.useMemo)(() => {
|
|
43
|
-
if (
|
|
44
|
+
if (stateful) {
|
|
44
45
|
return (async function* (messages) {
|
|
45
46
|
const lastUserMessage = [...messages]
|
|
46
47
|
.reverse()
|
|
@@ -49,7 +50,9 @@ function useConversationManagerRemote({ client: _client, endpoint, conversationI
|
|
|
49
50
|
throw new Error('No user message found');
|
|
50
51
|
}
|
|
51
52
|
yield* client
|
|
52
|
-
.complete(conversationId, {
|
|
53
|
+
.complete((conversationId), {
|
|
54
|
+
text: lastUserMessage.text,
|
|
55
|
+
})
|
|
53
56
|
.stream();
|
|
54
57
|
});
|
|
55
58
|
}
|
|
@@ -57,13 +60,18 @@ function useConversationManagerRemote({ client: _client, endpoint, conversationI
|
|
|
57
60
|
return (async function* (messages) {
|
|
58
61
|
yield* client
|
|
59
62
|
.complete(null, {
|
|
63
|
+
botId: botId,
|
|
60
64
|
backstory: backstory,
|
|
61
65
|
model: model,
|
|
62
66
|
datasetId: datasetId,
|
|
63
67
|
skillsetId: skillsetId,
|
|
64
68
|
privacy: privacy,
|
|
65
69
|
moderation: moderation,
|
|
66
|
-
messages: messages,
|
|
70
|
+
messages: messages.map(({ type, text, meta }) => ({
|
|
71
|
+
type,
|
|
72
|
+
text,
|
|
73
|
+
meta,
|
|
74
|
+
})),
|
|
67
75
|
})
|
|
68
76
|
.stream();
|
|
69
77
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export function useConversationManagerRemote({ client: _client, endpoint, conversationId,
|
|
1
|
+
export function useConversationManagerRemote({ client: _client, endpoint, token, conversationId, stateful, botId, backstory, model, datasetId, skillsetId, privacy, moderation, ...rest }: UseConversationManagerRemoteOptions): UseConversationManagerRemoteResult;
|
|
2
2
|
export default useConversationManagerRemote;
|
|
3
3
|
export type Message = any;
|
|
4
4
|
export type Model = any;
|
|
@@ -7,8 +7,10 @@ export type EndpointFunction = (options: any) => AsyncGenerator<any>;
|
|
|
7
7
|
export type UseConversationManagerRemoteOptions = {
|
|
8
8
|
client?: ConversationClient;
|
|
9
9
|
endpoint?: EndpointURL | EndpointFunction;
|
|
10
|
-
conversationId?: string;
|
|
11
10
|
token?: string;
|
|
11
|
+
conversationId?: string;
|
|
12
|
+
stateful?: boolean;
|
|
13
|
+
botId?: string;
|
|
12
14
|
backstory?: string;
|
|
13
15
|
model?: Model;
|
|
14
16
|
datasetId?: string;
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
export function useConversationManagerState(state?: Partial<State>): [State, StateFunctions];
|
|
2
2
|
export default useConversationManagerState;
|
|
3
|
-
export type Message = any;
|
|
4
3
|
export type State = import('./useConversationManagerStateReducer.cjs').State;
|
|
5
4
|
export type StateFunctions = {
|
|
6
|
-
setThinking: (thinking:
|
|
7
|
-
setTyping: (typing:
|
|
8
|
-
appendText: (text:
|
|
9
|
-
appendMessage: (message:
|
|
5
|
+
setThinking: (thinking: import('./useConversationManagerStateReducer.cjs').SetThinkingAction['data']['thinking']) => void;
|
|
6
|
+
setTyping: (typing: import('./useConversationManagerStateReducer.cjs').SetTypingAction['data']['typing']) => void;
|
|
7
|
+
appendText: (text: import('./useConversationManagerStateReducer.cjs').AppendTextAction['data']['text']) => void;
|
|
8
|
+
appendMessage: (message: import('./useConversationManagerStateReducer.cjs').AppendMessageAction['data']['message']) => void;
|
|
10
9
|
};
|
|
@@ -45,6 +45,7 @@ function conversationManagerStateReducer(state, action) {
|
|
|
45
45
|
id: (0, string_js_1.getRandomId)('tmp-'),
|
|
46
46
|
type: 'bot',
|
|
47
47
|
text: '',
|
|
48
|
+
createdAt: new Date(),
|
|
48
49
|
});
|
|
49
50
|
message.text += text;
|
|
50
51
|
return {
|
|
@@ -75,6 +76,7 @@ function conversationManagerStateReducer(state, action) {
|
|
|
75
76
|
messages.push({
|
|
76
77
|
...message,
|
|
77
78
|
id: message.id || (0, string_js_1.getRandomId)('tmp-'),
|
|
79
|
+
createdAt: message.createdAt || new Date(),
|
|
78
80
|
});
|
|
79
81
|
}
|
|
80
82
|
}
|
|
@@ -82,6 +84,7 @@ function conversationManagerStateReducer(state, action) {
|
|
|
82
84
|
messages.push({
|
|
83
85
|
...message,
|
|
84
86
|
id: (0, string_js_1.getRandomId)('tmp-'),
|
|
87
|
+
createdAt: new Date(),
|
|
85
88
|
});
|
|
86
89
|
}
|
|
87
90
|
return {
|
|
@@ -2,18 +2,15 @@ export function conversationManagerStateReducer(state: State, action: Action): S
|
|
|
2
2
|
export function conversationManagerStateInitial(state?: Partial<State>): State;
|
|
3
3
|
export function useConversationManagerStateReducer(state?: Partial<State>): [State, import('react').Dispatch<Action>];
|
|
4
4
|
export default useConversationManagerStateReducer;
|
|
5
|
-
export type Message = any
|
|
6
|
-
export type MessageWithId = {
|
|
5
|
+
export type Message = any & {
|
|
7
6
|
id: string;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
id?: string;
|
|
11
|
-
} & Message;
|
|
7
|
+
createdAt: Date;
|
|
8
|
+
};
|
|
12
9
|
export type State = {
|
|
13
10
|
thinking: boolean;
|
|
14
11
|
typing: boolean;
|
|
15
|
-
message:
|
|
16
|
-
messages:
|
|
12
|
+
message: Message | null;
|
|
13
|
+
messages: Message[];
|
|
17
14
|
};
|
|
18
15
|
export type SetThinkingAction = {
|
|
19
16
|
type: 'setThinking';
|
|
@@ -36,7 +33,10 @@ export type AppendTextAction = {
|
|
|
36
33
|
export type AppendMessageAction = {
|
|
37
34
|
type: 'appendMessage';
|
|
38
35
|
data: {
|
|
39
|
-
message:
|
|
36
|
+
message: Omit<Message, 'id' | 'createdAt'> & {
|
|
37
|
+
id?: string;
|
|
38
|
+
createdAt?: Date;
|
|
39
|
+
};
|
|
40
40
|
};
|
|
41
41
|
};
|
|
42
42
|
export type Action = SetThinkingAction | SetTypingAction | AppendTextAction | AppendMessageAction;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
export function AutoTextarea(props?: {
|
|
2
2
|
[name: string]: any;
|
|
3
|
-
} | undefined): import("react/jsx-runtime").JSX.Element;
|
|
4
|
-
|
|
3
|
+
} | undefined, forwardedRef?: React.Ref<HTMLTextAreaElement> | undefined): import("react/jsx-runtime").JSX.Element;
|
|
4
|
+
declare const _default: React.ForwardRefExoticComponent<Omit<{
|
|
5
|
+
[name: string]: any;
|
|
6
|
+
}, "ref"> & React.RefAttributes<HTMLTextAreaElement>>;
|
|
7
|
+
export default _default;
|
|
8
|
+
import React from 'react';
|
|
@@ -1,35 +1,51 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
-
import React, { useEffect, useRef } from 'react';
|
|
4
|
-
export function AutoTextarea(props) {
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
.join(' + ') || '0px'})`;
|
|
10
|
-
textarea.style.height = 'auto';
|
|
11
|
-
textarea.style.height = `calc(${textarea.scrollHeight}px - ${adjustment})`;
|
|
12
|
-
}
|
|
3
|
+
import React, { forwardRef, useEffect, useImperativeHandle, useRef, } from 'react';
|
|
4
|
+
export function AutoTextarea(props, forwardedRef) {
|
|
5
|
+
const localRef = useRef(null);
|
|
6
|
+
useImperativeHandle(forwardedRef, () => {
|
|
7
|
+
return (localRef.current);
|
|
8
|
+
});
|
|
13
9
|
useEffect(() => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
});
|
|
25
|
-
observer.observe(textarea, {
|
|
26
|
-
childList: true,
|
|
27
|
-
subtree: true,
|
|
28
|
-
characterData: true,
|
|
29
|
-
});
|
|
30
|
-
return () => observer.disconnect();
|
|
10
|
+
if (!localRef.current) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const textarea = (localRef.current);
|
|
14
|
+
function recalibrate() {
|
|
15
|
+
const adjustment = `calc(${[textarea.style.paddingTop, textarea.style.paddingBottom]
|
|
16
|
+
.filter((f) => f)
|
|
17
|
+
.join(' + ') || '0px'})`;
|
|
18
|
+
textarea.style.height = 'auto';
|
|
19
|
+
textarea.style.height = `calc(${textarea.scrollHeight}px - ${adjustment})`;
|
|
31
20
|
}
|
|
32
|
-
|
|
33
|
-
|
|
21
|
+
recalibrate();
|
|
22
|
+
const mutationObserver = new MutationObserver((mutationsList) => {
|
|
23
|
+
for (const mutation of mutationsList) {
|
|
24
|
+
if (mutation.type === 'childList' ||
|
|
25
|
+
mutation.type === 'subtree' ||
|
|
26
|
+
mutation.type === 'characterData') {
|
|
27
|
+
recalibrate();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
mutationObserver.observe(textarea, {
|
|
32
|
+
childList: true,
|
|
33
|
+
subtree: true,
|
|
34
|
+
characterData: true,
|
|
35
|
+
});
|
|
36
|
+
const resizeObserver = new ResizeObserver(recalibrate);
|
|
37
|
+
resizeObserver.observe(textarea);
|
|
38
|
+
textarea.addEventListener('input', recalibrate);
|
|
39
|
+
textarea.addEventListener('focus', recalibrate);
|
|
40
|
+
window.addEventListener('resize', recalibrate);
|
|
41
|
+
return () => {
|
|
42
|
+
mutationObserver.disconnect();
|
|
43
|
+
resizeObserver.disconnect();
|
|
44
|
+
textarea.removeEventListener('input', recalibrate);
|
|
45
|
+
textarea.removeEventListener('focus', recalibrate);
|
|
46
|
+
window.removeEventListener('resize', recalibrate);
|
|
47
|
+
};
|
|
48
|
+
}, [localRef]);
|
|
49
|
+
return _jsx("textarea", { ref: localRef, rows: 1, ...props });
|
|
34
50
|
}
|
|
35
|
-
export default AutoTextarea;
|
|
51
|
+
export default forwardRef(AutoTextarea);
|
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
export function useConversationManager({ ...conversationManagerRemoteOptions }: UseConversationManagerOptions): UseConversationManagerResult;
|
|
1
|
+
export function useConversationManager({ interactionMaxMessages, ...conversationManagerRemoteOptions }: UseConversationManagerOptions): UseConversationManagerResult;
|
|
2
2
|
export default useConversationManager;
|
|
3
3
|
export type Message = import('@chatbotkit/sdk/conversation/v1').Message;
|
|
4
4
|
export type SimpleMessage = Message & {
|
|
5
5
|
id: string;
|
|
6
|
+
createdAt: Date;
|
|
6
7
|
};
|
|
7
8
|
export type ComplexMessage = Message & {
|
|
8
9
|
id: string;
|
|
10
|
+
createdAt: Date;
|
|
9
11
|
children?: import('react').ReactNode;
|
|
10
12
|
};
|
|
11
13
|
export type UseConversationManagerRemoteOptions = import('./useConversationManagerRemote.js').UseConversationManagerRemoteOptions;
|
|
12
|
-
export type UseConversationManagerOptions = UseConversationManagerRemoteOptions & {
|
|
14
|
+
export type UseConversationManagerOptions = UseConversationManagerRemoteOptions & {
|
|
15
|
+
interactionMaxMessages?: number;
|
|
16
|
+
};
|
|
13
17
|
export type UseConversationManagerResult = {
|
|
14
18
|
message: SimpleMessage | null;
|
|
15
19
|
messages: ComplexMessage[];
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { useState } from 'react';
|
|
3
3
|
import useConversationManagerRemote from './useConversationManagerRemote.js';
|
|
4
4
|
import { useConversationManagerState } from './useConversationManagerState.js';
|
|
5
|
-
export function useConversationManager({ ...conversationManagerRemoteOptions }) {
|
|
5
|
+
export function useConversationManager({ interactionMaxMessages = 100, ...conversationManagerRemoteOptions }) {
|
|
6
6
|
const remote = useConversationManagerRemote(conversationManagerRemoteOptions);
|
|
7
7
|
const [{ thinking, typing, message, messages, }, { setThinking, setTyping, appendText, appendMessage, },] = useConversationManagerState();
|
|
8
8
|
const [text, setText] = useState((''));
|
|
@@ -23,7 +23,7 @@ export function useConversationManager({ ...conversationManagerRemoteOptions })
|
|
|
23
23
|
meta,
|
|
24
24
|
};
|
|
25
25
|
}) || []),
|
|
26
|
-
].slice(-
|
|
26
|
+
].slice(-Math.abs(interactionMaxMessages));
|
|
27
27
|
try {
|
|
28
28
|
setThinking(true);
|
|
29
29
|
setError(null);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export function useConversationManagerRemote({ client: _client, endpoint, conversationId,
|
|
1
|
+
export function useConversationManagerRemote({ client: _client, endpoint, token, conversationId, stateful, botId, backstory, model, datasetId, skillsetId, privacy, moderation, ...rest }: UseConversationManagerRemoteOptions): UseConversationManagerRemoteResult;
|
|
2
2
|
export default useConversationManagerRemote;
|
|
3
3
|
export type Message = import('@chatbotkit/sdk/conversation/v1').Message;
|
|
4
4
|
export type Model = import('@chatbotkit/sdk/model/v1').Model;
|
|
@@ -7,8 +7,10 @@ export type EndpointFunction = (options: any) => AsyncGenerator<any>;
|
|
|
7
7
|
export type UseConversationManagerRemoteOptions = {
|
|
8
8
|
client?: ConversationClient;
|
|
9
9
|
endpoint?: EndpointURL | EndpointFunction;
|
|
10
|
-
conversationId?: string;
|
|
11
10
|
token?: string;
|
|
11
|
+
conversationId?: string;
|
|
12
|
+
stateful?: boolean;
|
|
13
|
+
botId?: string;
|
|
12
14
|
backstory?: string;
|
|
13
15
|
model?: Model;
|
|
14
16
|
datasetId?: string;
|
|
@@ -3,7 +3,7 @@ import { useMemo } from 'react';
|
|
|
3
3
|
import { cloneAndExtend } from '../utils/object.js';
|
|
4
4
|
import { consume } from '../utils/stream.js';
|
|
5
5
|
import { ConversationClient } from '@chatbotkit/sdk';
|
|
6
|
-
export function useConversationManagerRemote({ client: _client, endpoint, conversationId,
|
|
6
|
+
export function useConversationManagerRemote({ client: _client, endpoint, token, conversationId, stateful = conversationId ? true : false, botId, backstory, model, datasetId, skillsetId, privacy, moderation, ...rest }) {
|
|
7
7
|
const client = useMemo(() => {
|
|
8
8
|
if (typeof endpoint === 'function') {
|
|
9
9
|
return {
|
|
@@ -23,6 +23,7 @@ export function useConversationManagerRemote({ client: _client, endpoint, conver
|
|
|
23
23
|
extension.url = new URL(globalThis.window?.location?.origin || 'about:blank');
|
|
24
24
|
extension.endpoints = {
|
|
25
25
|
'/api/v1/conversation/complete': endpoint,
|
|
26
|
+
'/api/v1/conversation/{conversationId}/complete': endpoint,
|
|
26
27
|
};
|
|
27
28
|
}
|
|
28
29
|
if (token) {
|
|
@@ -36,7 +37,7 @@ export function useConversationManagerRemote({ client: _client, endpoint, conver
|
|
|
36
37
|
}
|
|
37
38
|
}, [_client, endpoint, token]);
|
|
38
39
|
const remote = useMemo(() => {
|
|
39
|
-
if (
|
|
40
|
+
if (stateful) {
|
|
40
41
|
return (async function* (messages) {
|
|
41
42
|
const lastUserMessage = [...messages]
|
|
42
43
|
.reverse()
|
|
@@ -45,7 +46,9 @@ export function useConversationManagerRemote({ client: _client, endpoint, conver
|
|
|
45
46
|
throw new Error('No user message found');
|
|
46
47
|
}
|
|
47
48
|
yield* client
|
|
48
|
-
.complete(conversationId, {
|
|
49
|
+
.complete((conversationId), {
|
|
50
|
+
text: lastUserMessage.text,
|
|
51
|
+
})
|
|
49
52
|
.stream();
|
|
50
53
|
});
|
|
51
54
|
}
|
|
@@ -53,13 +56,18 @@ export function useConversationManagerRemote({ client: _client, endpoint, conver
|
|
|
53
56
|
return (async function* (messages) {
|
|
54
57
|
yield* client
|
|
55
58
|
.complete(null, {
|
|
59
|
+
botId: botId,
|
|
56
60
|
backstory: backstory,
|
|
57
61
|
model: model,
|
|
58
62
|
datasetId: datasetId,
|
|
59
63
|
skillsetId: skillsetId,
|
|
60
64
|
privacy: privacy,
|
|
61
65
|
moderation: moderation,
|
|
62
|
-
messages: messages,
|
|
66
|
+
messages: messages.map(({ type, text, meta }) => ({
|
|
67
|
+
type,
|
|
68
|
+
text,
|
|
69
|
+
meta,
|
|
70
|
+
})),
|
|
63
71
|
})
|
|
64
72
|
.stream();
|
|
65
73
|
});
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
export function useConversationManagerState(state?: Partial<import("./useConversationManagerStateReducer.js").State> | undefined): [State, StateFunctions];
|
|
2
2
|
export default useConversationManagerState;
|
|
3
|
-
export type Message = import('@chatbotkit/sdk/conversation/v1').Message;
|
|
4
3
|
export type State = import('./useConversationManagerStateReducer.js').State;
|
|
5
4
|
export type StateFunctions = {
|
|
6
|
-
setThinking: (thinking:
|
|
7
|
-
setTyping: (typing:
|
|
8
|
-
appendText: (text:
|
|
9
|
-
appendMessage: (message:
|
|
5
|
+
setThinking: (thinking: import('./useConversationManagerStateReducer.js').SetThinkingAction['data']['thinking']) => void;
|
|
6
|
+
setTyping: (typing: import('./useConversationManagerStateReducer.js').SetTypingAction['data']['typing']) => void;
|
|
7
|
+
appendText: (text: import('./useConversationManagerStateReducer.js').AppendTextAction['data']['text']) => void;
|
|
8
|
+
appendMessage: (message: import('./useConversationManagerStateReducer.js').AppendMessageAction['data']['message']) => void;
|
|
10
9
|
};
|
|
@@ -2,18 +2,15 @@ export function conversationManagerStateReducer(state: State, action: Action): S
|
|
|
2
2
|
export function conversationManagerStateInitial(state?: Partial<State> | undefined): State;
|
|
3
3
|
export function useConversationManagerStateReducer(state?: Partial<State> | undefined): [State, import('react').Dispatch<Action>];
|
|
4
4
|
export default useConversationManagerStateReducer;
|
|
5
|
-
export type Message = import('@chatbotkit/sdk/conversation/v1').Message
|
|
6
|
-
export type MessageWithId = {
|
|
5
|
+
export type Message = import('@chatbotkit/sdk/conversation/v1').Message & {
|
|
7
6
|
id: string;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
id?: string;
|
|
11
|
-
} & Message;
|
|
7
|
+
createdAt: Date;
|
|
8
|
+
};
|
|
12
9
|
export type State = {
|
|
13
10
|
thinking: boolean;
|
|
14
11
|
typing: boolean;
|
|
15
|
-
message:
|
|
16
|
-
messages:
|
|
12
|
+
message: Message | null;
|
|
13
|
+
messages: Message[];
|
|
17
14
|
};
|
|
18
15
|
export type SetThinkingAction = {
|
|
19
16
|
type: 'setThinking';
|
|
@@ -36,7 +33,10 @@ export type AppendTextAction = {
|
|
|
36
33
|
export type AppendMessageAction = {
|
|
37
34
|
type: 'appendMessage';
|
|
38
35
|
data: {
|
|
39
|
-
message:
|
|
36
|
+
message: Omit<Message, 'id' | 'createdAt'> & {
|
|
37
|
+
id?: string;
|
|
38
|
+
createdAt?: Date;
|
|
39
|
+
};
|
|
40
40
|
};
|
|
41
41
|
};
|
|
42
42
|
export type Action = SetThinkingAction | SetTypingAction | AppendTextAction | AppendMessageAction;
|
|
@@ -41,6 +41,7 @@ export function conversationManagerStateReducer(state, action) {
|
|
|
41
41
|
id: getRandomId('tmp-'),
|
|
42
42
|
type: 'bot',
|
|
43
43
|
text: '',
|
|
44
|
+
createdAt: new Date(),
|
|
44
45
|
});
|
|
45
46
|
message.text += text;
|
|
46
47
|
return {
|
|
@@ -71,6 +72,7 @@ export function conversationManagerStateReducer(state, action) {
|
|
|
71
72
|
messages.push({
|
|
72
73
|
...message,
|
|
73
74
|
id: message.id || getRandomId('tmp-'),
|
|
75
|
+
createdAt: message.createdAt || new Date(),
|
|
74
76
|
});
|
|
75
77
|
}
|
|
76
78
|
}
|
|
@@ -78,6 +80,7 @@ export function conversationManagerStateReducer(state, action) {
|
|
|
78
80
|
messages.push({
|
|
79
81
|
...message,
|
|
80
82
|
id: getRandomId('tmp-'),
|
|
83
|
+
createdAt: new Date(),
|
|
81
84
|
});
|
|
82
85
|
}
|
|
83
86
|
return {
|