@chatbotkit/react 1.11.0 → 1.13.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.
@@ -59,6 +59,9 @@ async function* complete({ client, messages, functions, maxRecusion = 3, ...opti
59
59
  }
60
60
  for await (const item of it) {
61
61
  yield item;
62
+ if (options.onItem) {
63
+ await options.onItem(item);
64
+ }
62
65
  const { type, data } = item;
63
66
  if (type === 'message') {
64
67
  const message = data;
@@ -184,6 +187,9 @@ async function* complete({ client, messages, functions, maxRecusion = 3, ...opti
184
187
  }
185
188
  }
186
189
  }
190
+ if (options.onFinish) {
191
+ await options.onFinish({ messages });
192
+ }
187
193
  }
188
194
  function streamComplete(options) {
189
195
  return (0, stream_js_1.stream)(complete(options));
@@ -2,6 +2,10 @@ export function streamComplete(options: Options): import('../utils/stream.cjs').
2
2
  export default complete;
3
3
  export type ReactElement = import('react').ReactElement;
4
4
  export type ReactNode = import('react').ReactNode;
5
+ export type Item = {
6
+ type: string;
7
+ data: object;
8
+ };
5
9
  export type BasicParametersSchema = Record<string, any>;
6
10
  export type ValidatingParametersSchema = {
7
11
  schema: BasicParametersSchema;
@@ -15,6 +19,12 @@ export type InputMessage = {
15
19
  text: string;
16
20
  meta?: Record<string, any>;
17
21
  };
22
+ export type OutputMessage = {
23
+ type: 'bot' | 'activity';
24
+ text: string;
25
+ meta?: Record<string, any>;
26
+ };
27
+ export type Message = InputMessage | OutputMessage;
18
28
  export type RenderFunction = () => AsyncGenerator<ReactNode> | ReactNode | Promise<ReactNode>;
19
29
  export type HandlerArgs = any;
20
30
  export type HandlerOptions = {
@@ -39,10 +49,16 @@ export type InputFunction = {
39
49
  parameters: BasicParametersSchema | ValidatingParametersSchema;
40
50
  handler?: (args: HandlerArgs, options: HandlerOptions) => Promise<HandlerResult>;
41
51
  };
52
+ export type OnItemHandler = (arg0: Item) => any;
53
+ export type OnFinishHandler = (arg0: {
54
+ messages: Message[];
55
+ }) => any;
42
56
  export type Options = Omit<any, 'messages' | 'functions'> & {
43
57
  client: import('@chatbotkit/sdk').ConversationClient;
44
58
  messages: InputMessage[];
45
59
  functions?: (InputFunction | (() => InputFunction | Promise<InputFunction>))[];
46
60
  maxRecusion?: number;
61
+ onItem?: OnItemHandler;
62
+ onFinish?: OnFinishHandler;
47
63
  };
48
64
  declare function complete({ client, messages, functions, maxRecusion, ...options }: Options): any;
@@ -0,0 +1,24 @@
1
+ 'use client';
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.useWidgetInstance = void 0;
5
+ const react_1 = require("react");
6
+ function useWidgetInstance() {
7
+ const [instance, setInstance] = (0, react_1.useState)(null);
8
+ (0, react_1.useEffect)(() => {
9
+ if (window.chatbotkitWidget) {
10
+ window.chatbotkitWidget.instancePromise.then((instance) => setInstance(instance));
11
+ return;
12
+ }
13
+ function onInit() {
14
+ window.chatbotkitWidget.instancePromise.then((instance) => setInstance(instance));
15
+ }
16
+ window.addEventListener('chatbotkitWidgetInit', onInit);
17
+ return () => {
18
+ window.removeEventListener('chatbotkitWidgetInit', onInit);
19
+ };
20
+ }, []);
21
+ return instance;
22
+ }
23
+ exports.useWidgetInstance = useWidgetInstance;
24
+ exports.default = useWidgetInstance;
@@ -0,0 +1,22 @@
1
+ export function useWidgetInstance(): ChatBotKitWidgetInstance | null;
2
+ export default useWidgetInstance;
3
+ export type WidgetMessage = {
4
+ id: string;
5
+ type: string;
6
+ text: string;
7
+ };
8
+ export type WidgetNotification = {
9
+ text: string;
10
+ };
11
+ export type WidgetFunction = {
12
+ description: string;
13
+ parameters: Record<string, any>;
14
+ results: {
15
+ data: any;
16
+ };
17
+ };
18
+ export type ChatBotKitWidgetInstance = {
19
+ messages?: WidgetMessage[] | null;
20
+ notifications?: Record<string, WidgetNotification> | null;
21
+ functions?: Record<string, WidgetFunction> | null;
22
+ };
@@ -0,0 +1,23 @@
1
+ 'use client';
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.useWidgetInstanceFunctions = void 0;
5
+ const tslib_1 = require("tslib");
6
+ const react_1 = require("react");
7
+ const useWidgetInstance_js_1 = tslib_1.__importDefault(require("./useWidgetInstance.cjs"));
8
+ function useWidgetInstanceFunctions(params) {
9
+ const instance = (0, useWidgetInstance_js_1.default)();
10
+ (0, react_1.useEffect)(() => {
11
+ if (!instance) {
12
+ return;
13
+ }
14
+ if ((params === null || params === void 0 ? void 0 : params.functions) === undefined) {
15
+ return;
16
+ }
17
+ instance.functions = {
18
+ ...params === null || params === void 0 ? void 0 : params.functions,
19
+ };
20
+ }, [params === null || params === void 0 ? void 0 : params.functions, instance]);
21
+ }
22
+ exports.useWidgetInstanceFunctions = useWidgetInstanceFunctions;
23
+ exports.default = useWidgetInstanceFunctions;
@@ -0,0 +1,5 @@
1
+ export function useWidgetInstanceFunctions(params?: {
2
+ functions?: Record<string, WidgetFunction> | null;
3
+ }): void;
4
+ export default useWidgetInstanceFunctions;
5
+ export type WidgetFunction = import('./useWidgetInstance.cjs').WidgetFunction;
@@ -0,0 +1,23 @@
1
+ 'use client';
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.useWidgetInstanceNotifications = void 0;
5
+ const tslib_1 = require("tslib");
6
+ const react_1 = require("react");
7
+ const useWidgetInstance_js_1 = tslib_1.__importDefault(require("./useWidgetInstance.cjs"));
8
+ function useWidgetInstanceNotifications(params) {
9
+ const instance = (0, useWidgetInstance_js_1.default)();
10
+ (0, react_1.useEffect)(() => {
11
+ if (!instance) {
12
+ return;
13
+ }
14
+ if ((params === null || params === void 0 ? void 0 : params.notifications) == undefined) {
15
+ return;
16
+ }
17
+ instance.notifications = {
18
+ ...params === null || params === void 0 ? void 0 : params.notifications,
19
+ };
20
+ }, [params === null || params === void 0 ? void 0 : params.notifications, instance]);
21
+ }
22
+ exports.useWidgetInstanceNotifications = useWidgetInstanceNotifications;
23
+ exports.default = useWidgetInstanceNotifications;
@@ -0,0 +1,5 @@
1
+ export function useWidgetInstanceNotifications(params?: {
2
+ notifications?: Record<string, WidgetNotification> | null;
3
+ }): void;
4
+ export default useWidgetInstanceNotifications;
5
+ export type WidgetNotification = import('./useWidgetInstance.cjs').WidgetNotification;
@@ -1,31 +1,37 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.consume = exports.stream = void 0;
4
- function stream(source) {
4
+ function streamIt(source) {
5
5
  let it;
6
6
  if ('next' in source && typeof source.next === 'function') {
7
7
  it = source.next();
8
8
  }
9
9
  else if ('stream' in source && typeof source.stream === 'function') {
10
- return stream(source.stream());
10
+ return streamIt(source.stream());
11
11
  }
12
12
  else {
13
13
  throw new Error('Invalid source');
14
14
  }
15
15
  return new Promise((resolve, reject) => {
16
- it.then((res) => {
16
+ it.then(async (res) => {
17
17
  if (res.done) {
18
18
  resolve({ iteratorResult: res });
19
19
  }
20
20
  else {
21
- resolve({ iteratorResult: res, next: stream(source) });
21
+ resolve({ iteratorResult: res, next: streamIt(source) });
22
22
  }
23
23
  });
24
24
  it.catch((error) => reject(error));
25
25
  });
26
26
  }
27
+ async function* stream(source, options) {
28
+ yield* streamIt(source);
29
+ if (options === null || options === void 0 ? void 0 : options.onFinish) {
30
+ await options.onFinish();
31
+ }
32
+ }
27
33
  exports.stream = stream;
28
- function consume(source) {
34
+ function consumeIt(source) {
29
35
  return {
30
36
  [Symbol.asyncIterator]: function () {
31
37
  return {
@@ -44,4 +50,10 @@ function consume(source) {
44
50
  },
45
51
  };
46
52
  }
53
+ async function* consume(source, options) {
54
+ yield* consumeIt(source);
55
+ if (options === null || options === void 0 ? void 0 : options.onFinish) {
56
+ await options.onFinish();
57
+ }
58
+ }
47
59
  exports.consume = consume;
@@ -1,6 +1,11 @@
1
- export function stream(source: StreamSource): StreamResult;
2
- export function consume(source: ConsumeSource): ConsumeResult;
1
+ export function stream(source: StreamSource, options?: StreamOptions): StreamResult;
2
+ export function consume(source: ConsumeSource, options?: ConsumeOptions): ConsumeResult;
3
+ export type Handlers = {
4
+ onFinish?: () => any;
5
+ };
3
6
  export type StreamSource = any;
4
7
  export type StreamResult = any;
8
+ export type StreamOptions = {} & Handlers;
5
9
  export type ConsumeSource = any;
6
10
  export type ConsumeResult = any;
11
+ export type ConsumeOptions = {} & Handlers;
@@ -2,6 +2,10 @@ export function streamComplete(options: Options): import('../utils/stream.js').S
2
2
  export default complete;
3
3
  export type ReactElement = import('react').ReactElement;
4
4
  export type ReactNode = import('react').ReactNode;
5
+ export type Item = {
6
+ type: string;
7
+ data: object;
8
+ };
5
9
  export type BasicParametersSchema = Record<string, any>;
6
10
  export type ValidatingParametersSchema = {
7
11
  schema: BasicParametersSchema;
@@ -15,6 +19,12 @@ export type InputMessage = {
15
19
  text: string;
16
20
  meta?: Record<string, any>;
17
21
  };
22
+ export type OutputMessage = {
23
+ type: 'bot' | 'activity';
24
+ text: string;
25
+ meta?: Record<string, any>;
26
+ };
27
+ export type Message = InputMessage | OutputMessage;
18
28
  export type RenderFunction = () => AsyncGenerator<ReactNode> | ReactNode | Promise<ReactNode>;
19
29
  export type HandlerArgs = any;
20
30
  export type HandlerOptions = {
@@ -39,10 +49,16 @@ export type InputFunction = {
39
49
  parameters: BasicParametersSchema | ValidatingParametersSchema;
40
50
  handler?: ((args: HandlerArgs, options: HandlerOptions) => Promise<HandlerResult>) | undefined;
41
51
  };
52
+ export type OnItemHandler = (arg0: Item) => any;
53
+ export type OnFinishHandler = (arg0: {
54
+ messages: Message[];
55
+ }) => any;
42
56
  export type Options = Omit<import('@chatbotkit/sdk/conversation/v1.js').ConversationCompleteRequest, 'messages' | 'functions'> & {
43
57
  client: import('@chatbotkit/sdk').ConversationClient;
44
58
  messages: InputMessage[];
45
59
  functions?: (InputFunction | (() => InputFunction | Promise<InputFunction>))[];
46
60
  maxRecusion?: number;
61
+ onItem?: OnItemHandler;
62
+ onFinish?: OnFinishHandler;
47
63
  };
48
64
  declare function complete({ client, messages, functions, maxRecusion, ...options }: Options): any;
@@ -55,6 +55,9 @@ async function* complete({ client, messages, functions, maxRecusion = 3, ...opti
55
55
  }
56
56
  for await (const item of it) {
57
57
  yield item;
58
+ if (options.onItem) {
59
+ await options.onItem(item);
60
+ }
58
61
  const { type, data } = item;
59
62
  if (type === 'message') {
60
63
  const message = data;
@@ -180,6 +183,9 @@ async function* complete({ client, messages, functions, maxRecusion = 3, ...opti
180
183
  }
181
184
  }
182
185
  }
186
+ if (options.onFinish) {
187
+ await options.onFinish({ messages });
188
+ }
183
189
  }
184
190
  export function streamComplete(options) {
185
191
  return stream(complete(options));
@@ -0,0 +1,22 @@
1
+ export function useWidgetInstance(): ChatBotKitWidgetInstance | null;
2
+ export default useWidgetInstance;
3
+ export type WidgetMessage = {
4
+ id: string;
5
+ type: string;
6
+ text: string;
7
+ };
8
+ export type WidgetNotification = {
9
+ text: string;
10
+ };
11
+ export type WidgetFunction = {
12
+ description: string;
13
+ parameters: Record<string, any>;
14
+ results: {
15
+ data: any;
16
+ };
17
+ };
18
+ export type ChatBotKitWidgetInstance = {
19
+ messages?: WidgetMessage[] | null;
20
+ notifications?: Record<string, WidgetNotification> | null;
21
+ functions?: Record<string, WidgetFunction> | null;
22
+ };
@@ -0,0 +1,20 @@
1
+ 'use client';
2
+ import { useEffect, useState } from 'react';
3
+ export function useWidgetInstance() {
4
+ const [instance, setInstance] = useState(null);
5
+ useEffect(() => {
6
+ if (window.chatbotkitWidget) {
7
+ window.chatbotkitWidget.instancePromise.then((instance) => setInstance(instance));
8
+ return;
9
+ }
10
+ function onInit() {
11
+ window.chatbotkitWidget.instancePromise.then((instance) => setInstance(instance));
12
+ }
13
+ window.addEventListener('chatbotkitWidgetInit', onInit);
14
+ return () => {
15
+ window.removeEventListener('chatbotkitWidgetInit', onInit);
16
+ };
17
+ }, []);
18
+ return instance;
19
+ }
20
+ export default useWidgetInstance;
@@ -0,0 +1,5 @@
1
+ export function useWidgetInstanceFunctions(params?: {
2
+ functions?: Record<string, import("./useWidgetInstance.js").WidgetFunction> | null | undefined;
3
+ } | undefined): void;
4
+ export default useWidgetInstanceFunctions;
5
+ export type WidgetFunction = import('./useWidgetInstance.js').WidgetFunction;
@@ -0,0 +1,18 @@
1
+ 'use client';
2
+ import { useEffect } from 'react';
3
+ import useWidgetInstance from './useWidgetInstance.js';
4
+ export function useWidgetInstanceFunctions(params) {
5
+ const instance = useWidgetInstance();
6
+ useEffect(() => {
7
+ if (!instance) {
8
+ return;
9
+ }
10
+ if (params?.functions === undefined) {
11
+ return;
12
+ }
13
+ instance.functions = {
14
+ ...params?.functions,
15
+ };
16
+ }, [params?.functions, instance]);
17
+ }
18
+ export default useWidgetInstanceFunctions;
@@ -0,0 +1,5 @@
1
+ export function useWidgetInstanceNotifications(params?: {
2
+ notifications?: Record<string, import("./useWidgetInstance.js").WidgetNotification> | null | undefined;
3
+ } | undefined): void;
4
+ export default useWidgetInstanceNotifications;
5
+ export type WidgetNotification = import('./useWidgetInstance.js').WidgetNotification;
@@ -0,0 +1,18 @@
1
+ 'use client';
2
+ import { useEffect } from 'react';
3
+ import useWidgetInstance from './useWidgetInstance.js';
4
+ export function useWidgetInstanceNotifications(params) {
5
+ const instance = useWidgetInstance();
6
+ useEffect(() => {
7
+ if (!instance) {
8
+ return;
9
+ }
10
+ if (params?.notifications == undefined) {
11
+ return;
12
+ }
13
+ instance.notifications = {
14
+ ...params?.notifications,
15
+ };
16
+ }, [params?.notifications, instance]);
17
+ }
18
+ export default useWidgetInstanceNotifications;
@@ -1,6 +1,11 @@
1
- export function stream(source: StreamSource): StreamResult;
2
- export function consume(source: ConsumeSource): ConsumeResult;
1
+ export function stream(source: StreamSource, options?: Handlers | undefined): StreamResult;
2
+ export function consume(source: ConsumeSource, options?: Handlers | undefined): ConsumeResult;
3
+ export type Handlers = {
4
+ onFinish?: () => any;
5
+ };
3
6
  export type StreamSource = any;
4
7
  export type StreamResult = any;
8
+ export type StreamOptions = {} & Handlers;
5
9
  export type ConsumeSource = any;
6
10
  export type ConsumeResult = any;
11
+ export type ConsumeOptions = {} & Handlers;
@@ -1,27 +1,33 @@
1
- export function stream(source) {
1
+ function streamIt(source) {
2
2
  let it;
3
3
  if ('next' in source && typeof source.next === 'function') {
4
4
  it = source.next();
5
5
  }
6
6
  else if ('stream' in source && typeof source.stream === 'function') {
7
- return stream(source.stream());
7
+ return streamIt(source.stream());
8
8
  }
9
9
  else {
10
10
  throw new Error('Invalid source');
11
11
  }
12
12
  return new Promise((resolve, reject) => {
13
- it.then((res) => {
13
+ it.then(async (res) => {
14
14
  if (res.done) {
15
15
  resolve({ iteratorResult: res });
16
16
  }
17
17
  else {
18
- resolve({ iteratorResult: res, next: stream(source) });
18
+ resolve({ iteratorResult: res, next: streamIt(source) });
19
19
  }
20
20
  });
21
21
  it.catch((error) => reject(error));
22
22
  });
23
23
  }
24
- export function consume(source) {
24
+ export async function* stream(source, options) {
25
+ yield* streamIt(source);
26
+ if (options?.onFinish) {
27
+ await options.onFinish();
28
+ }
29
+ }
30
+ function consumeIt(source) {
25
31
  return {
26
32
  [Symbol.asyncIterator]: function () {
27
33
  return {
@@ -40,3 +46,9 @@ export function consume(source) {
40
46
  },
41
47
  };
42
48
  }
49
+ export async function* consume(source, options) {
50
+ yield* consumeIt(source);
51
+ if (options?.onFinish) {
52
+ await options.onFinish();
53
+ }
54
+ }