@chatbotkit/react 1.6.0 → 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/ConversationManager.cjs +41 -0
- package/dist/cjs/components/ConversationManager.d.ts +5 -0
- package/dist/cjs/hooks/useConversationManager.cjs +74 -25
- package/dist/cjs/hooks/useConversationManager.d.ts +43 -37
- package/dist/cjs/index.cjs +4 -1
- package/dist/cjs/index.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 +24 -0
- package/dist/esm/actions/complete.js +107 -0
- package/dist/esm/components/AutoScroller.js +1 -1
- package/dist/esm/components/ConversationManager.d.ts +5 -0
- package/dist/esm/components/ConversationManager.js +36 -0
- package/dist/esm/hooks/useConversationManager.d.ts +43 -37
- package/dist/esm/hooks/useConversationManager.js +74 -25
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -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 +42 -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;
|
|
@@ -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,14 +2,15 @@
|
|
|
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");
|
|
8
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");
|
|
9
9
|
function useConversationManager(options) {
|
|
10
|
-
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;
|
|
11
11
|
const [token, setToken] = (0, react_1.useState)(_token);
|
|
12
12
|
const [conversationId, setConversationId] = (0, react_1.useState)(_conversationId);
|
|
13
|
+
const [botId, setBotId] = (0, react_1.useState)(_botId);
|
|
13
14
|
const [backstory, setBackstory] = (0, react_1.useState)(_backstory);
|
|
14
15
|
const [model, setModel] = (0, react_1.useState)(_model);
|
|
15
16
|
const [datasetId, setDatasetId] = (0, react_1.useState)(_datasetId);
|
|
@@ -51,19 +52,7 @@ function useConversationManager(options) {
|
|
|
51
52
|
const [thinking, setThinking] = (0, react_1.useState)(false);
|
|
52
53
|
const [typing, setTyping] = (0, react_1.useState)(false);
|
|
53
54
|
const [error, setError] = (0, react_1.useState)((null));
|
|
54
|
-
async function
|
|
55
|
-
if (!text) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
setText('');
|
|
59
|
-
const userMessage = {
|
|
60
|
-
id: (0, string_js_1.getRandomId)('message-'),
|
|
61
|
-
type: 'user',
|
|
62
|
-
text: text,
|
|
63
|
-
};
|
|
64
|
-
let newMessages = messages.slice(0);
|
|
65
|
-
newMessages = [...newMessages, userMessage];
|
|
66
|
-
setMessages([...newMessages]);
|
|
55
|
+
async function stream(newMessages) {
|
|
67
56
|
setThinking(true);
|
|
68
57
|
let it;
|
|
69
58
|
try {
|
|
@@ -76,7 +65,9 @@ function useConversationManager(options) {
|
|
|
76
65
|
model: model,
|
|
77
66
|
datasetId: datasetId,
|
|
78
67
|
skillsetId: skillsetId,
|
|
79
|
-
messages: newMessages
|
|
68
|
+
messages: newMessages
|
|
69
|
+
.slice(-100)
|
|
70
|
+
.map(({ type, text, meta }) => ({ type, text, meta })),
|
|
80
71
|
});
|
|
81
72
|
}
|
|
82
73
|
}
|
|
@@ -93,16 +84,37 @@ function useConversationManager(options) {
|
|
|
93
84
|
let alreadyStreaming = false;
|
|
94
85
|
try {
|
|
95
86
|
for await (const event of it.stream()) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
alreadyStreaming
|
|
99
|
-
|
|
100
|
-
|
|
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': {
|
|
101
115
|
setThinking(false);
|
|
102
|
-
setTyping(
|
|
116
|
+
setTyping(false);
|
|
103
117
|
}
|
|
104
|
-
botMessage.text += event.data.token;
|
|
105
|
-
setMessages([...newMessages]);
|
|
106
118
|
}
|
|
107
119
|
}
|
|
108
120
|
}
|
|
@@ -113,11 +125,47 @@ function useConversationManager(options) {
|
|
|
113
125
|
setTyping(false);
|
|
114
126
|
}
|
|
115
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
|
+
}
|
|
116
162
|
return {
|
|
117
163
|
token,
|
|
118
164
|
setToken,
|
|
119
165
|
conversationId,
|
|
120
166
|
setConversationId,
|
|
167
|
+
botId,
|
|
168
|
+
setBotId,
|
|
121
169
|
backstory,
|
|
122
170
|
setBackstory,
|
|
123
171
|
model,
|
|
@@ -137,6 +185,7 @@ function useConversationManager(options) {
|
|
|
137
185
|
error,
|
|
138
186
|
setError,
|
|
139
187
|
submit,
|
|
188
|
+
trigger,
|
|
140
189
|
};
|
|
141
190
|
}
|
|
142
191
|
exports.useConversationManager = useConversationManager;
|
|
@@ -1,38 +1,4 @@
|
|
|
1
|
-
export function useConversationManager(options:
|
|
2
|
-
[key: string]: any;
|
|
3
|
-
client?: ConversationClient;
|
|
4
|
-
endpoint?: EndpointURL | EndpointFunction;
|
|
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,10 +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';
|
|
53
19
|
text: string;
|
|
20
|
+
meta?: Record<string, any>;
|
|
54
21
|
};
|
|
55
22
|
export type EndpointURL = string;
|
|
56
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;
|
|
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;
|
|
62
|
+
};
|
|
57
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";
|
|
@@ -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) => {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
export function stream(source:
|
|
2
|
-
export function consume(source:
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
};
|
|
1
|
+
export function stream(source: StreamSource): StreamResult;
|
|
2
|
+
export function consume(source: ConsumeSource): ConsumeResult;
|
|
3
|
+
export type StreamSource = any;
|
|
4
|
+
export type StreamResult = any;
|
|
5
|
+
export type ConsumeSource = any;
|
|
6
|
+
export type ConsumeResult = any;
|
|
@@ -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;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { isValidElement } from 'react';
|
|
3
|
+
import { stream } from '../utils/stream.js';
|
|
4
|
+
async function* complete({ client, messages, functions, maxRecusion = 3, ...options }) {
|
|
5
|
+
if (maxRecusion <= 0) {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
messages = messages.slice(0);
|
|
9
|
+
const it = client
|
|
10
|
+
.complete(null, {
|
|
11
|
+
...options,
|
|
12
|
+
messages: messages.map(({ type, text, meta }) => {
|
|
13
|
+
return {
|
|
14
|
+
type,
|
|
15
|
+
text,
|
|
16
|
+
meta,
|
|
17
|
+
};
|
|
18
|
+
}),
|
|
19
|
+
functions: functions?.map(({ name, description, parameters }) => {
|
|
20
|
+
return {
|
|
21
|
+
name,
|
|
22
|
+
description,
|
|
23
|
+
parameters,
|
|
24
|
+
};
|
|
25
|
+
}),
|
|
26
|
+
})
|
|
27
|
+
.stream();
|
|
28
|
+
for await (const item of it) {
|
|
29
|
+
yield item;
|
|
30
|
+
const { type, data } = item;
|
|
31
|
+
if (type === 'message') {
|
|
32
|
+
const message = data;
|
|
33
|
+
messages.push(message);
|
|
34
|
+
if (message.meta?.activity?.type === 'request') {
|
|
35
|
+
const name = message.meta.activity.function?.name;
|
|
36
|
+
const args = message.meta.activity.function?.arguments;
|
|
37
|
+
const fn = functions?.find((fn) => fn.name === name);
|
|
38
|
+
if (fn && typeof fn.handler === 'function') {
|
|
39
|
+
const output = await fn.handler(args);
|
|
40
|
+
let text;
|
|
41
|
+
let children;
|
|
42
|
+
let result;
|
|
43
|
+
if (typeof output === 'string') {
|
|
44
|
+
text = undefined;
|
|
45
|
+
children = undefined;
|
|
46
|
+
result = output;
|
|
47
|
+
}
|
|
48
|
+
else if (isValidElement(output)) {
|
|
49
|
+
text = '';
|
|
50
|
+
children = output;
|
|
51
|
+
result = undefined;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
if (typeof output?.text === 'string') {
|
|
55
|
+
text = output.text;
|
|
56
|
+
}
|
|
57
|
+
if (isValidElement(output?.children)) {
|
|
58
|
+
children = output.children;
|
|
59
|
+
}
|
|
60
|
+
if (output?.result) {
|
|
61
|
+
result = output.result;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (text || children) {
|
|
65
|
+
yield {
|
|
66
|
+
type: 'message',
|
|
67
|
+
data: {
|
|
68
|
+
type: 'bot',
|
|
69
|
+
text: text ? text : '',
|
|
70
|
+
children: children ? _jsx(_Fragment, { children: children }) : undefined,
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
if (result) {
|
|
75
|
+
const activityMessage = {
|
|
76
|
+
type: 'activity',
|
|
77
|
+
text: '',
|
|
78
|
+
meta: {
|
|
79
|
+
activity: {
|
|
80
|
+
type: 'response',
|
|
81
|
+
function: {
|
|
82
|
+
name,
|
|
83
|
+
arguments: args,
|
|
84
|
+
result: JSON.stringify(result),
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
yield { type: 'message', data: activityMessage };
|
|
90
|
+
messages.push(activityMessage);
|
|
91
|
+
yield* complete({
|
|
92
|
+
...options,
|
|
93
|
+
client,
|
|
94
|
+
messages,
|
|
95
|
+
functions,
|
|
96
|
+
maxRecusion: maxRecusion - 1,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export function streamComplete(options) {
|
|
105
|
+
return stream(complete(options));
|
|
106
|
+
}
|
|
107
|
+
export default complete;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import React, {
|
|
2
|
+
import React, { useEffect, useRef } from 'react';
|
|
3
3
|
function AutoScrollAnchor() {
|
|
4
4
|
return _jsx("div", { className: "auto-scroll-anchor", style: { height: '1px' } });
|
|
5
5
|
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export function ConversationManager({ children, ...options }: import('../hooks/useConversationManager.js').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;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext } from 'react';
|
|
3
|
+
import useConversationManager from '../hooks/useConversationManager.js';
|
|
4
|
+
export const ConversationContext = createContext(({
|
|
5
|
+
token: undefined,
|
|
6
|
+
setToken: () => { },
|
|
7
|
+
conversationId: undefined,
|
|
8
|
+
setConversationId: () => { },
|
|
9
|
+
botId: undefined,
|
|
10
|
+
setBotId: () => { },
|
|
11
|
+
backstory: undefined,
|
|
12
|
+
setBackstory: () => { },
|
|
13
|
+
model: undefined,
|
|
14
|
+
setModel: () => { },
|
|
15
|
+
datasetId: undefined,
|
|
16
|
+
setDatasetId: () => { },
|
|
17
|
+
skillsetId: undefined,
|
|
18
|
+
setSkillsetId: () => { },
|
|
19
|
+
text: '',
|
|
20
|
+
setText: () => { },
|
|
21
|
+
messages: [],
|
|
22
|
+
setMessages: () => { },
|
|
23
|
+
thinking: false,
|
|
24
|
+
setThinking: () => { },
|
|
25
|
+
typing: false,
|
|
26
|
+
setTyping: () => { },
|
|
27
|
+
error: null,
|
|
28
|
+
setError: () => { },
|
|
29
|
+
submit: () => { },
|
|
30
|
+
trigger: () => { },
|
|
31
|
+
}));
|
|
32
|
+
export function ConversationManager({ children, ...options }) {
|
|
33
|
+
const manager = useConversationManager(options);
|
|
34
|
+
return (_jsx(ConversationContext.Provider, { value: manager, children: children }));
|
|
35
|
+
}
|
|
36
|
+
export default ConversationManager;
|