@chatbotkit/react 1.5.3 → 1.7.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/dist/cjs/actions/complete.cjs +112 -0
- package/dist/cjs/actions/complete.d.ts +24 -0
- package/dist/cjs/components/AutoTextarea.cjs +26 -22
- package/dist/cjs/components/AutoTextarea.d.ts +0 -7
- package/dist/cjs/components/ConversationManager.cjs +41 -0
- package/dist/cjs/components/ConversationManager.d.ts +5 -0
- package/dist/cjs/hooks/useConversationManager.cjs +90 -27
- package/dist/cjs/hooks/useConversationManager.d.ts +45 -37
- package/dist/cjs/index.cjs +4 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/utils/stream.cjs +47 -0
- package/dist/cjs/utils/stream.d.ts +6 -0
- package/dist/esm/actions/complete.d.ts +24 -0
- package/dist/esm/actions/complete.js +107 -0
- package/dist/esm/components/AutoScroller.js +1 -1
- package/dist/esm/components/AutoTextarea.d.ts +0 -7
- package/dist/esm/components/AutoTextarea.js +26 -22
- package/dist/esm/components/ConversationManager.d.ts +5 -0
- package/dist/esm/components/ConversationManager.js +36 -0
- package/dist/esm/hooks/useConversationManager.d.ts +45 -37
- package/dist/esm/hooks/useConversationManager.js +90 -27
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/utils/stream.d.ts +6 -0
- package/dist/esm/utils/stream.js +42 -0
- package/dist/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +62 -2
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.streamComplete = void 0;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const stream_js_1 = require("../utils/stream.cjs");
|
|
7
|
+
async function* complete({ client, messages, functions, maxRecusion = 3, ...options }) {
|
|
8
|
+
var _a, _b, _c, _d;
|
|
9
|
+
if (maxRecusion <= 0) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
messages = messages.slice(0);
|
|
13
|
+
const it = client
|
|
14
|
+
.complete(null, {
|
|
15
|
+
...options,
|
|
16
|
+
messages: messages.map(({ type, text, meta }) => {
|
|
17
|
+
return {
|
|
18
|
+
type,
|
|
19
|
+
text,
|
|
20
|
+
meta,
|
|
21
|
+
};
|
|
22
|
+
}),
|
|
23
|
+
functions: functions === null || functions === void 0 ? void 0 : functions.map(({ name, description, parameters }) => {
|
|
24
|
+
return {
|
|
25
|
+
name,
|
|
26
|
+
description,
|
|
27
|
+
parameters,
|
|
28
|
+
};
|
|
29
|
+
}),
|
|
30
|
+
})
|
|
31
|
+
.stream();
|
|
32
|
+
for await (const item of it) {
|
|
33
|
+
yield item;
|
|
34
|
+
const { type, data } = item;
|
|
35
|
+
if (type === 'message') {
|
|
36
|
+
const message = data;
|
|
37
|
+
messages.push(message);
|
|
38
|
+
if (((_b = (_a = message.meta) === null || _a === void 0 ? void 0 : _a.activity) === null || _b === void 0 ? void 0 : _b.type) === 'request') {
|
|
39
|
+
const name = (_c = message.meta.activity.function) === null || _c === void 0 ? void 0 : _c.name;
|
|
40
|
+
const args = (_d = message.meta.activity.function) === null || _d === void 0 ? void 0 : _d.arguments;
|
|
41
|
+
const fn = functions === null || functions === void 0 ? void 0 : functions.find((fn) => fn.name === name);
|
|
42
|
+
if (fn && typeof fn.handler === 'function') {
|
|
43
|
+
const output = await fn.handler(args);
|
|
44
|
+
let text;
|
|
45
|
+
let children;
|
|
46
|
+
let result;
|
|
47
|
+
if (typeof output === 'string') {
|
|
48
|
+
text = undefined;
|
|
49
|
+
children = undefined;
|
|
50
|
+
result = output;
|
|
51
|
+
}
|
|
52
|
+
else if ((0, react_1.isValidElement)(output)) {
|
|
53
|
+
text = '';
|
|
54
|
+
children = output;
|
|
55
|
+
result = undefined;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
if (typeof (output === null || output === void 0 ? void 0 : output.text) === 'string') {
|
|
59
|
+
text = output.text;
|
|
60
|
+
}
|
|
61
|
+
if ((0, react_1.isValidElement)(output === null || output === void 0 ? void 0 : output.children)) {
|
|
62
|
+
children = output.children;
|
|
63
|
+
}
|
|
64
|
+
if (output === null || output === void 0 ? void 0 : output.result) {
|
|
65
|
+
result = output.result;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (text || children) {
|
|
69
|
+
yield {
|
|
70
|
+
type: 'message',
|
|
71
|
+
data: {
|
|
72
|
+
type: 'bot',
|
|
73
|
+
text: text ? text : '',
|
|
74
|
+
children: children ? (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: children }) : undefined,
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
if (result) {
|
|
79
|
+
const activityMessage = {
|
|
80
|
+
type: 'activity',
|
|
81
|
+
text: '',
|
|
82
|
+
meta: {
|
|
83
|
+
activity: {
|
|
84
|
+
type: 'response',
|
|
85
|
+
function: {
|
|
86
|
+
name,
|
|
87
|
+
arguments: args,
|
|
88
|
+
result: JSON.stringify(result),
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
yield { type: 'message', data: activityMessage };
|
|
94
|
+
messages.push(activityMessage);
|
|
95
|
+
yield* complete({
|
|
96
|
+
...options,
|
|
97
|
+
client,
|
|
98
|
+
messages,
|
|
99
|
+
functions,
|
|
100
|
+
maxRecusion: maxRecusion - 1,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function streamComplete(options) {
|
|
109
|
+
return (0, stream_js_1.stream)(complete(options));
|
|
110
|
+
}
|
|
111
|
+
exports.streamComplete = streamComplete;
|
|
112
|
+
exports.default = complete;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export function streamComplete(options: Options): import('../utils/stream.cjs').StreamResult;
|
|
2
|
+
export default complete;
|
|
3
|
+
export type InputMessage = {
|
|
4
|
+
type: 'bot' | 'user' | 'context' | 'instruction' | 'backstory' | 'activity';
|
|
5
|
+
text: string;
|
|
6
|
+
meta?: Record<string, any>;
|
|
7
|
+
};
|
|
8
|
+
export type InputFunction = {
|
|
9
|
+
name: string;
|
|
10
|
+
description: string;
|
|
11
|
+
parameters: Record<string, any>;
|
|
12
|
+
handler?: (args: any) => Promise<string | import('react').ReactElement | {
|
|
13
|
+
text?: string;
|
|
14
|
+
children?: import('react').ReactElement;
|
|
15
|
+
result?: any;
|
|
16
|
+
}>;
|
|
17
|
+
};
|
|
18
|
+
export type Options = Omit<any, 'messages' | 'functions'> & {
|
|
19
|
+
client: import('@chatbotkit/sdk').ConversationClient;
|
|
20
|
+
messages: InputMessage[];
|
|
21
|
+
functions?: InputFunction[];
|
|
22
|
+
maxRecusion?: number;
|
|
23
|
+
};
|
|
24
|
+
declare function complete({ client, messages, functions, maxRecusion, ...options }: Options): any;
|
|
@@ -3,33 +3,37 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.AutoTextarea = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
-
const react_1 = tslib_1.
|
|
6
|
+
const react_1 = tslib_1.__importStar(require("react"));
|
|
7
7
|
function AutoTextarea(props) {
|
|
8
|
-
const
|
|
9
|
-
function
|
|
10
|
-
const adjustment = `calc(${[
|
|
8
|
+
const ref = (0, react_1.useRef)(null);
|
|
9
|
+
function recalibrate(textarea) {
|
|
10
|
+
const adjustment = `calc(${[textarea.style.paddingTop, textarea.style.paddingBottom]
|
|
11
11
|
.filter((f) => f)
|
|
12
12
|
.join(' + ') || '0px'})`;
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
textarea.style.height = 'auto';
|
|
14
|
+
textarea.style.height = `calc(${textarea.scrollHeight}px - ${adjustment})`;
|
|
15
15
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
(0, react_1.useEffect)(() => {
|
|
17
|
+
const textarea = ref.current;
|
|
18
|
+
if (textarea) {
|
|
19
|
+
recalibrate(textarea);
|
|
20
|
+
const observer = new MutationObserver((mutationsList) => {
|
|
21
|
+
for (const mutation of mutationsList) {
|
|
22
|
+
if (mutation.type === 'childList' ||
|
|
23
|
+
mutation.type === 'characterData') {
|
|
24
|
+
recalibrate(textarea);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
observer.observe(textarea, {
|
|
29
|
+
childList: true,
|
|
30
|
+
subtree: true,
|
|
31
|
+
characterData: true,
|
|
32
|
+
});
|
|
33
|
+
return () => observer.disconnect();
|
|
19
34
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
function handleOnFocus(event) {
|
|
25
|
-
if (adjustOnFocus) {
|
|
26
|
-
handleEvent(event);
|
|
27
|
-
}
|
|
28
|
-
if (onFocus) {
|
|
29
|
-
onFocus(event);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return ((0, jsx_runtime_1.jsx)("textarea", { ...rest, rows: 1, onInput: handleOnInput, onFocus: handleOnFocus }));
|
|
35
|
+
}, []);
|
|
36
|
+
return (0, jsx_runtime_1.jsx)("textarea", { ref: ref, rows: 1, ...props });
|
|
33
37
|
}
|
|
34
38
|
exports.AutoTextarea = AutoTextarea;
|
|
35
39
|
exports.default = AutoTextarea;
|
|
@@ -1,11 +1,4 @@
|
|
|
1
1
|
export function AutoTextarea(props?: {
|
|
2
2
|
[name: string]: any;
|
|
3
|
-
onInput?: onInputFn | null;
|
|
4
|
-
onFocus?: onFocusFn | null;
|
|
5
|
-
adjustOnInput?: boolean | null;
|
|
6
|
-
adjustOnFocus?: boolean | null;
|
|
7
3
|
}): import("react/jsx-runtime").JSX.Element;
|
|
8
4
|
export default AutoTextarea;
|
|
9
|
-
export type onInputFn = (event: React.ChangeEvent<HTMLTextAreaElement>) => any;
|
|
10
|
-
export type onFocusFn = (event: React.ChangeEvent<HTMLTextAreaElement>) => any;
|
|
11
|
-
import React from 'react';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConversationManager = exports.ConversationContext = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
+
const react_1 = require("react");
|
|
7
|
+
const useConversationManager_js_1 = tslib_1.__importDefault(require("../hooks/useConversationManager.cjs"));
|
|
8
|
+
exports.ConversationContext = (0, react_1.createContext)(({
|
|
9
|
+
token: undefined,
|
|
10
|
+
setToken: () => { },
|
|
11
|
+
conversationId: undefined,
|
|
12
|
+
setConversationId: () => { },
|
|
13
|
+
botId: undefined,
|
|
14
|
+
setBotId: () => { },
|
|
15
|
+
backstory: undefined,
|
|
16
|
+
setBackstory: () => { },
|
|
17
|
+
model: undefined,
|
|
18
|
+
setModel: () => { },
|
|
19
|
+
datasetId: undefined,
|
|
20
|
+
setDatasetId: () => { },
|
|
21
|
+
skillsetId: undefined,
|
|
22
|
+
setSkillsetId: () => { },
|
|
23
|
+
text: '',
|
|
24
|
+
setText: () => { },
|
|
25
|
+
messages: [],
|
|
26
|
+
setMessages: () => { },
|
|
27
|
+
thinking: false,
|
|
28
|
+
setThinking: () => { },
|
|
29
|
+
typing: false,
|
|
30
|
+
setTyping: () => { },
|
|
31
|
+
error: null,
|
|
32
|
+
setError: () => { },
|
|
33
|
+
submit: () => { },
|
|
34
|
+
trigger: () => { },
|
|
35
|
+
}));
|
|
36
|
+
function ConversationManager({ children, ...options }) {
|
|
37
|
+
const manager = (0, useConversationManager_js_1.default)(options);
|
|
38
|
+
return ((0, jsx_runtime_1.jsx)(exports.ConversationContext.Provider, { value: manager, children: children }));
|
|
39
|
+
}
|
|
40
|
+
exports.ConversationManager = ConversationManager;
|
|
41
|
+
exports.default = ConversationManager;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export function ConversationManager({ children, ...options }: import('../hooks/useConversationManager.cjs').UseConversationManagerOptions & {
|
|
2
|
+
children: import('react').ReactNode;
|
|
3
|
+
}): import('react').ReactElement;
|
|
4
|
+
export const ConversationContext: import("react").Context<import("../hooks/useConversationManager.js").UseConversationManagerResult>;
|
|
5
|
+
export default ConversationManager;
|
|
@@ -2,23 +2,36 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useConversationManager = void 0;
|
|
4
4
|
const react_1 = require("react");
|
|
5
|
-
const sdk_1 = require("@chatbotkit/sdk");
|
|
6
|
-
const string_js_1 = require("../utils/string.cjs");
|
|
7
5
|
const object_js_1 = require("../utils/object.cjs");
|
|
6
|
+
const stream_js_1 = require("../utils/stream.cjs");
|
|
7
|
+
const string_js_1 = require("../utils/string.cjs");
|
|
8
|
+
const sdk_1 = require("@chatbotkit/sdk");
|
|
8
9
|
function useConversationManager(options) {
|
|
9
|
-
const { client: _client, endpoint, token: _token, conversationId: _conversationId, backstory: _backstory, model: _model, datasetId: _datasetId, skillsetId: _skillsetId, ...rest } = options;
|
|
10
|
+
const { client: _client, endpoint, token: _token, conversationId: _conversationId, botId: _botId, backstory: _backstory, model: _model, datasetId: _datasetId, skillsetId: _skillsetId, ...rest } = options;
|
|
10
11
|
const [token, setToken] = (0, react_1.useState)(_token);
|
|
11
12
|
const [conversationId, setConversationId] = (0, react_1.useState)(_conversationId);
|
|
13
|
+
const [botId, setBotId] = (0, react_1.useState)(_botId);
|
|
12
14
|
const [backstory, setBackstory] = (0, react_1.useState)(_backstory);
|
|
13
15
|
const [model, setModel] = (0, react_1.useState)(_model);
|
|
14
16
|
const [datasetId, setDatasetId] = (0, react_1.useState)(_datasetId);
|
|
15
17
|
const [skillsetId, setSkillsetId] = (0, react_1.useState)(_skillsetId);
|
|
16
18
|
const client = (0, react_1.useMemo)(() => {
|
|
17
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
|
+
}
|
|
18
31
|
const options = { ...rest, secret: token || '' };
|
|
19
32
|
let thisClient = _client || new sdk_1.ConversationClient(options);
|
|
20
33
|
const extension = {};
|
|
21
|
-
if (endpoint) {
|
|
34
|
+
if (typeof endpoint === 'string') {
|
|
22
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');
|
|
23
36
|
extension.endpoints = {
|
|
24
37
|
'/api/v1/conversation/complete': endpoint,
|
|
@@ -30,26 +43,16 @@ function useConversationManager(options) {
|
|
|
30
43
|
if (Object.keys(extension).length === 0) {
|
|
31
44
|
return thisClient;
|
|
32
45
|
}
|
|
33
|
-
|
|
46
|
+
else {
|
|
47
|
+
return (0, object_js_1.cloneAndExtend)(thisClient, extension);
|
|
48
|
+
}
|
|
34
49
|
}, [_client, endpoint, token]);
|
|
35
50
|
const [text, setText] = (0, react_1.useState)((''));
|
|
36
51
|
const [messages, setMessages] = (0, react_1.useState)(([]));
|
|
37
52
|
const [thinking, setThinking] = (0, react_1.useState)(false);
|
|
38
53
|
const [typing, setTyping] = (0, react_1.useState)(false);
|
|
39
54
|
const [error, setError] = (0, react_1.useState)((null));
|
|
40
|
-
async function
|
|
41
|
-
if (!text) {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
setText('');
|
|
45
|
-
const userMessage = {
|
|
46
|
-
id: (0, string_js_1.getRandomId)('message-'),
|
|
47
|
-
type: 'user',
|
|
48
|
-
text: text,
|
|
49
|
-
};
|
|
50
|
-
let newMessages = messages.slice(0);
|
|
51
|
-
newMessages = [...newMessages, userMessage];
|
|
52
|
-
setMessages([...newMessages]);
|
|
55
|
+
async function stream(newMessages) {
|
|
53
56
|
setThinking(true);
|
|
54
57
|
let it;
|
|
55
58
|
try {
|
|
@@ -62,7 +65,9 @@ function useConversationManager(options) {
|
|
|
62
65
|
model: model,
|
|
63
66
|
datasetId: datasetId,
|
|
64
67
|
skillsetId: skillsetId,
|
|
65
|
-
messages: newMessages
|
|
68
|
+
messages: newMessages
|
|
69
|
+
.slice(-100)
|
|
70
|
+
.map(({ type, text, meta }) => ({ type, text, meta })),
|
|
66
71
|
});
|
|
67
72
|
}
|
|
68
73
|
}
|
|
@@ -79,16 +84,37 @@ function useConversationManager(options) {
|
|
|
79
84
|
let alreadyStreaming = false;
|
|
80
85
|
try {
|
|
81
86
|
for await (const event of it.stream()) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
alreadyStreaming
|
|
85
|
-
|
|
86
|
-
|
|
87
|
+
switch (event.type) {
|
|
88
|
+
case 'token': {
|
|
89
|
+
if (!alreadyStreaming) {
|
|
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]);
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
case 'message': {
|
|
101
|
+
const message = event.data;
|
|
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
|
+
}
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case 'result': {
|
|
87
115
|
setThinking(false);
|
|
88
|
-
setTyping(
|
|
116
|
+
setTyping(false);
|
|
89
117
|
}
|
|
90
|
-
botMessage.text += event.data.token;
|
|
91
|
-
setMessages([...newMessages]);
|
|
92
118
|
}
|
|
93
119
|
}
|
|
94
120
|
}
|
|
@@ -99,11 +125,47 @@ function useConversationManager(options) {
|
|
|
99
125
|
setTyping(false);
|
|
100
126
|
}
|
|
101
127
|
}
|
|
128
|
+
async function submit() {
|
|
129
|
+
if (!text) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
setText('');
|
|
133
|
+
const userMessage = {
|
|
134
|
+
id: (0, string_js_1.getRandomId)('message-'),
|
|
135
|
+
type: 'user',
|
|
136
|
+
text: text,
|
|
137
|
+
};
|
|
138
|
+
let newMessages = messages.slice(0);
|
|
139
|
+
newMessages = [...newMessages, userMessage];
|
|
140
|
+
setMessages([...newMessages]);
|
|
141
|
+
await stream(newMessages);
|
|
142
|
+
}
|
|
143
|
+
async function trigger(name, ...args) {
|
|
144
|
+
const newMessages = [
|
|
145
|
+
...messages,
|
|
146
|
+
({
|
|
147
|
+
type: 'activity',
|
|
148
|
+
text: '',
|
|
149
|
+
meta: {
|
|
150
|
+
activity: {
|
|
151
|
+
type: 'trigger',
|
|
152
|
+
function: {
|
|
153
|
+
name: name,
|
|
154
|
+
arguments: args,
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
}),
|
|
159
|
+
];
|
|
160
|
+
await stream(newMessages);
|
|
161
|
+
}
|
|
102
162
|
return {
|
|
103
163
|
token,
|
|
104
164
|
setToken,
|
|
105
165
|
conversationId,
|
|
106
166
|
setConversationId,
|
|
167
|
+
botId,
|
|
168
|
+
setBotId,
|
|
107
169
|
backstory,
|
|
108
170
|
setBackstory,
|
|
109
171
|
model,
|
|
@@ -123,6 +185,7 @@ function useConversationManager(options) {
|
|
|
123
185
|
error,
|
|
124
186
|
setError,
|
|
125
187
|
submit,
|
|
188
|
+
trigger,
|
|
126
189
|
};
|
|
127
190
|
}
|
|
128
191
|
exports.useConversationManager = useConversationManager;
|
|
@@ -1,38 +1,4 @@
|
|
|
1
|
-
export function useConversationManager(options:
|
|
2
|
-
[key: string]: any;
|
|
3
|
-
client?: ConversationClient;
|
|
4
|
-
endpoint?: string;
|
|
5
|
-
token?: string;
|
|
6
|
-
conversationId?: string;
|
|
7
|
-
backstory?: string;
|
|
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>>;
|
|
26
|
-
messages: Message[];
|
|
27
|
-
setMessages: import("react").Dispatch<import("react").SetStateAction<Message[]>>;
|
|
28
|
-
thinking: boolean;
|
|
29
|
-
setThinking: import("react").Dispatch<import("react").SetStateAction<boolean>>;
|
|
30
|
-
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
|
-
};
|
|
1
|
+
export function useConversationManager(options: UseConversationManagerOptions): UseConversationManagerResult;
|
|
36
2
|
export default useConversationManager;
|
|
37
3
|
export type ModelConfig = {
|
|
38
4
|
maxTokens?: number;
|
|
@@ -48,8 +14,50 @@ export type Model = string | {
|
|
|
48
14
|
config?: ModelConfig;
|
|
49
15
|
};
|
|
50
16
|
export type Message = {
|
|
51
|
-
id
|
|
52
|
-
type: 'bot' | 'user';
|
|
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
|
+
};
|
|
35
|
+
export type UseConversationManagerResult = {
|
|
36
|
+
token?: string;
|
|
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;
|
|
53
50
|
text: string;
|
|
51
|
+
setText: (text: string) => void;
|
|
52
|
+
messages: Message[];
|
|
53
|
+
setMessages: (messages: Message[]) => void;
|
|
54
|
+
thinking: boolean;
|
|
55
|
+
setThinking: (thinking: boolean) => void;
|
|
56
|
+
typing: boolean;
|
|
57
|
+
setTyping: (typing: boolean) => void;
|
|
58
|
+
error: any;
|
|
59
|
+
setError: (error: any) => void;
|
|
60
|
+
submit: () => void;
|
|
61
|
+
trigger: (name: string, ...args: any) => void;
|
|
54
62
|
};
|
|
55
63
|
import { ConversationClient } from '@chatbotkit/sdk';
|
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,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.consume = exports.stream = void 0;
|
|
4
|
+
function stream(source) {
|
|
5
|
+
let it;
|
|
6
|
+
if ('next' in source && typeof source.next === 'function') {
|
|
7
|
+
it = source.next();
|
|
8
|
+
}
|
|
9
|
+
else if ('stream' in source && typeof source.stream === 'function') {
|
|
10
|
+
return stream(source.stream());
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
throw new Error('Invalid source');
|
|
14
|
+
}
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
it.then((res) => {
|
|
17
|
+
if (res.done) {
|
|
18
|
+
resolve({ iteratorResult: res });
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
resolve({ iteratorResult: res, next: stream(source) });
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
it.catch((error) => reject(error));
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
exports.stream = stream;
|
|
28
|
+
function consume(source) {
|
|
29
|
+
return {
|
|
30
|
+
[Symbol.asyncIterator]: function () {
|
|
31
|
+
return {
|
|
32
|
+
current: source,
|
|
33
|
+
async next() {
|
|
34
|
+
const { iteratorResult, next } = await this.current;
|
|
35
|
+
if (next) {
|
|
36
|
+
this.current = next;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
iteratorResult.done = true;
|
|
40
|
+
}
|
|
41
|
+
return iteratorResult;
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
exports.consume = consume;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export function streamComplete(options: Options): import('../utils/stream.js').StreamResult;
|
|
2
|
+
export default complete;
|
|
3
|
+
export type InputMessage = {
|
|
4
|
+
type: 'bot' | 'user' | 'context' | 'instruction' | 'backstory' | 'activity';
|
|
5
|
+
text: string;
|
|
6
|
+
meta?: Record<string, any>;
|
|
7
|
+
};
|
|
8
|
+
export type InputFunction = {
|
|
9
|
+
name: string;
|
|
10
|
+
description: string;
|
|
11
|
+
parameters: Record<string, any>;
|
|
12
|
+
handler?: ((args: any) => Promise<string | import('react').ReactElement | {
|
|
13
|
+
text?: string;
|
|
14
|
+
children?: import('react').ReactElement;
|
|
15
|
+
result?: any;
|
|
16
|
+
}>) | undefined;
|
|
17
|
+
};
|
|
18
|
+
export type Options = Omit<import('@chatbotkit/sdk/conversation/v1.js').ConversationCompleteRequest, 'messages' | 'functions'> & {
|
|
19
|
+
client: import('@chatbotkit/sdk').ConversationClient;
|
|
20
|
+
messages: InputMessage[];
|
|
21
|
+
functions?: InputFunction[];
|
|
22
|
+
maxRecusion?: number;
|
|
23
|
+
};
|
|
24
|
+
declare function complete({ client, messages, functions, maxRecusion, ...options }: Options): any;
|