@super_studio/ecforce-ai-agent-react 0.3.1 → 0.4.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.
Files changed (48) hide show
  1. package/dist/components/chatbot-frame.d.ts +7 -0
  2. package/dist/components/chatbot-frame.d.ts.map +1 -0
  3. package/dist/components/chatbot-sheet.d.ts +8 -0
  4. package/dist/components/chatbot-sheet.d.ts.map +1 -0
  5. package/dist/components/provider/chatbot-proivder.d.ts +36 -0
  6. package/dist/components/provider/chatbot-proivder.d.ts.map +1 -0
  7. package/dist/components/provider/use-chatbot-frame-handler.d.ts +44 -0
  8. package/dist/components/provider/use-chatbot-frame-handler.d.ts.map +1 -0
  9. package/dist/components/provider/use-chatbot-window-states.d.ts +9 -0
  10. package/dist/components/provider/use-chatbot-window-states.d.ts.map +1 -0
  11. package/dist/components/sheet.d.ts.map +1 -0
  12. package/dist/components/tooltip.d.ts.map +1 -0
  13. package/dist/hooks/use-chatbot-config.d.ts +19 -0
  14. package/dist/hooks/use-chatbot-config.d.ts.map +1 -0
  15. package/dist/index.d.ts +6 -5
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +197 -167
  18. package/dist/index.mjs +199 -169
  19. package/dist/lib/constants.d.ts.map +1 -0
  20. package/package.json +3 -3
  21. package/src/components/chatbot-frame.tsx +26 -0
  22. package/src/{chatbot-sheet.tsx → components/chatbot-sheet.tsx} +25 -31
  23. package/src/components/provider/chatbot-proivder.tsx +79 -0
  24. package/src/components/provider/use-chatbot-frame-handler.tsx +130 -0
  25. package/src/components/provider/use-chatbot-window-states.tsx +15 -0
  26. package/src/{sheet.tsx → components/sheet.tsx} +1 -3
  27. package/src/{tooltip.tsx → components/tooltip.tsx} +0 -2
  28. package/src/hooks/use-chatbot-config.tsx +60 -0
  29. package/src/index.ts +6 -5
  30. package/dist/agent-frame.d.ts +0 -32
  31. package/dist/agent-frame.d.ts.map +0 -1
  32. package/dist/chatbot-proivder.d.ts +0 -15
  33. package/dist/chatbot-proivder.d.ts.map +0 -1
  34. package/dist/chatbot-sheet.d.ts +0 -6
  35. package/dist/chatbot-sheet.d.ts.map +0 -1
  36. package/dist/constants.d.ts.map +0 -1
  37. package/dist/sheet.d.ts.map +0 -1
  38. package/dist/tooltip.d.ts.map +0 -1
  39. package/src/agent-frame.tsx +0 -196
  40. package/src/chatbot-proivder.tsx +0 -51
  41. /package/dist/{sheet.d.ts → components/sheet.d.ts} +0 -0
  42. /package/dist/{tooltip.d.ts → components/tooltip.d.ts} +0 -0
  43. /package/dist/{constants.d.ts → lib/constants.d.ts} +0 -0
  44. /package/dist/{chatbot-sheet.css → styles/chatbot-sheet.css} +0 -0
  45. /package/dist/{preset.css → styles/preset.css} +0 -0
  46. /package/src/{constants.ts → lib/constants.ts} +0 -0
  47. /package/src/{chatbot-sheet.css → styles/chatbot-sheet.css} +0 -0
  48. /package/src/{preset.css → styles/preset.css} +0 -0
@@ -0,0 +1,130 @@
1
+ import React from "react";
2
+
3
+ export type IframeMessage =
4
+ | { type: "CHATBOT_READY" }
5
+ | { type: "CHATBOT_INITIALIZED" }
6
+ | { type: "CLOSE_CHATBOT" }
7
+ | { type: "EXPAND_CHATBOT" }
8
+ | { type: "SHRINK_CHATBOT" };
9
+
10
+ export type MCP = {
11
+ name: string;
12
+ url: string;
13
+ headers?: Record<string, string>;
14
+ };
15
+
16
+ export type InitProps = {
17
+ mcps?: MCP[];
18
+ appName?: string;
19
+ };
20
+
21
+ export type ParentMessage =
22
+ | {
23
+ type: "mcps";
24
+ mcps: MCP[];
25
+ }
26
+ | {
27
+ type: "appName";
28
+ appName: string;
29
+ }
30
+ | {
31
+ type: "init";
32
+ mcps?: MCP[];
33
+ appName?: string;
34
+ };
35
+
36
+ export function useChatbotFrameHandler({
37
+ setOpen,
38
+ setIsExpanded,
39
+ }: {
40
+ setOpen: (open: boolean) => void;
41
+ setIsExpanded: (expanded: boolean) => void;
42
+ }) {
43
+ const [iframeEl, setIframeEl] = React.useState<HTMLIFrameElement | null>(
44
+ null,
45
+ );
46
+ const [isReady, setIsReady] = React.useState(false);
47
+ const [isInitialized, setIsInitialized] = React.useState(false);
48
+
49
+ // helper to post message to the iframe
50
+ const postMessage = React.useCallback(
51
+ (message: ParentMessage) => {
52
+ if (iframeEl?.contentWindow) {
53
+ if (process.env.NODE_ENV === "development") {
54
+ console.log("sending message to iframe", message);
55
+ }
56
+ iframeEl.contentWindow.postMessage(message, "*");
57
+ }
58
+ },
59
+ [iframeEl],
60
+ );
61
+
62
+ const init = React.useCallback(
63
+ (props: InitProps) => {
64
+ postMessage({
65
+ type: "init",
66
+ ...props,
67
+ });
68
+ },
69
+ [postMessage],
70
+ );
71
+
72
+ const setMcps = React.useCallback(
73
+ (mcps: MCP[]) => {
74
+ postMessage({ type: "mcps", mcps });
75
+ },
76
+ [postMessage],
77
+ );
78
+
79
+ const setAppName = React.useCallback(
80
+ (appName: string) => {
81
+ postMessage({ type: "appName", appName });
82
+ },
83
+ [postMessage],
84
+ );
85
+
86
+ // Initialize and setup listeners
87
+ React.useEffect(() => {
88
+ const iframe = iframeEl;
89
+ if (!iframe) {
90
+ return;
91
+ }
92
+
93
+ const handleMessage = (event: MessageEvent<IframeMessage>) => {
94
+ if (process.env.NODE_ENV === "development") {
95
+ console.log("iframe message", event.data);
96
+ }
97
+ // Handle iframe messages
98
+ switch (event.data?.type) {
99
+ case "CHATBOT_READY":
100
+ setIsReady(true);
101
+ break;
102
+ case "CHATBOT_INITIALIZED":
103
+ setIsInitialized(true);
104
+ break;
105
+ case "CLOSE_CHATBOT":
106
+ setOpen(false);
107
+ break;
108
+ case "EXPAND_CHATBOT":
109
+ setIsExpanded(true);
110
+ break;
111
+ case "SHRINK_CHATBOT":
112
+ setIsExpanded(false);
113
+ break;
114
+ }
115
+ };
116
+
117
+ const handleIframeLoad = () => {
118
+ window.addEventListener("message", handleMessage);
119
+ };
120
+
121
+ iframe.addEventListener("load", handleIframeLoad);
122
+
123
+ return () => {
124
+ window.removeEventListener("message", handleMessage);
125
+ iframe.removeEventListener("load", handleIframeLoad);
126
+ };
127
+ }, [iframeEl]); // Run when iframe gets mounted
128
+
129
+ return { init, setMcps, setAppName, setIframeEl, isReady, isInitialized };
130
+ }
@@ -0,0 +1,15 @@
1
+ import React from "react";
2
+
3
+ export function useChatbotWindowStates() {
4
+ const [hasOpened, setHasOpened] = React.useState(false);
5
+ const [open, setOpen] = React.useState(false);
6
+ const [isExpanded, setIsExpanded] = React.useState(false);
7
+
8
+ React.useEffect(() => {
9
+ if (open) {
10
+ setHasOpened(true);
11
+ }
12
+ }, [open]);
13
+
14
+ return { hasOpened, open, isExpanded, setOpen, setIsExpanded };
15
+ }
@@ -1,8 +1,6 @@
1
- "use client";
2
-
3
1
  import * as SheetPrimitive from "@radix-ui/react-dialog";
4
2
  import * as React from "react";
5
- import { useChatbot } from "./chatbot-proivder";
3
+ import { useChatbot } from "./provider/chatbot-proivder";
6
4
 
7
5
  function Sheet({ ...props }: React.ComponentProps<typeof SheetPrimitive.Root>) {
8
6
  const { open, setOpen } = useChatbot();
@@ -1,5 +1,3 @@
1
- "use client";
2
-
3
1
  import {
4
2
  Arrow,
5
3
  Content,
@@ -0,0 +1,60 @@
1
+ import React from "react";
2
+ import { useChatbot } from "../components/provider/chatbot-proivder";
3
+ import { type MCP } from "../components/provider/use-chatbot-frame-handler";
4
+
5
+ type ChatbotConfig = {
6
+ /** エージェントにつなげるMCPの情報 */
7
+ mcps?: MCP[];
8
+ /** アプリ名のメタデータ */
9
+ appName?: string;
10
+ /**
11
+ * trueになるまで、初期化されません。
12
+ * 初期化せれたあとはもう使われません。
13
+ * @default true
14
+ */
15
+ enabled?: boolean;
16
+ };
17
+
18
+ /**
19
+ * このフックに渡される情報で受動的にinitを呼び出し、mcpsとappNameを更新する
20
+ */
21
+ export function useChatbotConfig({
22
+ mcps,
23
+ appName,
24
+ enabled = true,
25
+ }: ChatbotConfig) {
26
+ const { init, setMcps, setAppName, isReady } = useChatbot();
27
+ const sentInitRef = React.useRef(false);
28
+ const [currentMcps, setCurrentMcps] = React.useState<MCP[]>();
29
+ const [currentAppName, setCurrentAppName] = React.useState<string>();
30
+
31
+ // 初期化の仕組み
32
+ React.useEffect(() => {
33
+ if (enabled && isReady && !sentInitRef.current) {
34
+ init({ mcps, appName });
35
+ sentInitRef.current = true;
36
+ setCurrentMcps(mcps);
37
+ setCurrentAppName(appName);
38
+ }
39
+ }, [enabled, init, mcps, appName, isReady]);
40
+
41
+ // mcpsの更新の仕組み
42
+ React.useEffect(() => {
43
+ if (
44
+ sentInitRef.current &&
45
+ mcps &&
46
+ JSON.stringify(currentMcps) !== JSON.stringify(mcps)
47
+ ) {
48
+ setMcps(mcps);
49
+ setCurrentMcps(mcps);
50
+ }
51
+ }, [mcps]);
52
+
53
+ // appNameの更新の仕組み
54
+ React.useEffect(() => {
55
+ if (sentInitRef.current && appName && currentAppName !== appName) {
56
+ setCurrentAppName(appName);
57
+ setAppName(appName);
58
+ }
59
+ }, [appName]);
60
+ }
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
- export * from "./chatbot-proivder";
2
- export * from "./agent-frame";
3
- export * from "./chatbot-sheet";
4
- export * from "./sheet";
5
- export * from "./tooltip";
1
+ export * from "./components/provider/chatbot-proivder";
2
+ export * from "./hooks/use-chatbot-config";
3
+ export * from "./components/chatbot-frame";
4
+ export * from "./components/chatbot-sheet";
5
+ export * from "./components/sheet";
6
+ export * from "./components/tooltip";
@@ -1,32 +0,0 @@
1
- type MCP = {
2
- name: string;
3
- url: string;
4
- headers?: Record<string, string>;
5
- };
6
- export type InitProps = {
7
- mcps?: MCP[];
8
- };
9
- export type AgentFrameProps = {
10
- agentId: string;
11
- mcps?: MCP[];
12
- appName?: string;
13
- url?: string;
14
- className?: string;
15
- };
16
- export type AgentElement = {
17
- /**
18
- * Push a new list of MCPs to the embedded agent.
19
- */
20
- setMcps: (mcps: MCP[]) => void;
21
- /**
22
- * Set the app name for the embedded agent.
23
- */
24
- setAppName: (appName: string) => void;
25
- /**
26
- * `true` once the iframe has sent the `CHATBOT_READY` handshake message.
27
- */
28
- isReady: boolean;
29
- };
30
- export declare const AgentFrame: import("react").ForwardRefExoticComponent<AgentFrameProps & import("react").RefAttributes<AgentElement>>;
31
- export {};
32
- //# sourceMappingURL=agent-frame.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"agent-frame.d.ts","sourceRoot":"","sources":["../src/agent-frame.tsx"],"names":[],"mappings":"AAiBA,KAAK,GAAG,GAAG;IACT,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG;IACtB,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;CACd,CAAC;AAiBF,MAAM,MAAM,eAAe,GAAG;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB;;OAEG;IACH,OAAO,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;IAC/B;;OAEG;IACH,UAAU,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACtC;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;CAClB,CAAC;AAEF,eAAO,MAAM,UAAU,0GAgItB,CAAC"}
@@ -1,15 +0,0 @@
1
- import React from "react";
2
- interface ChatbotContextType {
3
- hasOpened: boolean;
4
- open: boolean;
5
- setOpen: (open: boolean) => void;
6
- isExpanded: boolean;
7
- setIsExpanded: (expanded: boolean) => void;
8
- }
9
- export declare function useChatbot(): ChatbotContextType;
10
- interface ChatbotProviderProps {
11
- children: React.ReactNode;
12
- }
13
- export declare function ChatbotProvider({ children }: ChatbotProviderProps): import("react/jsx-runtime").JSX.Element;
14
- export {};
15
- //# sourceMappingURL=chatbot-proivder.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"chatbot-proivder.d.ts","sourceRoot":"","sources":["../src/chatbot-proivder.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,UAAU,kBAAkB;IAC1B,SAAS,EAAE,OAAO,CAAC;IACnB,IAAI,EAAE,OAAO,CAAC;IACd,OAAO,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,CAAC;IACjC,UAAU,EAAE,OAAO,CAAC;IACpB,aAAa,EAAE,CAAC,QAAQ,EAAE,OAAO,KAAK,IAAI,CAAC;CAC5C;AAMD,wBAAgB,UAAU,uBAMzB;AAED,UAAU,oBAAoB;IAC5B,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B;AAED,wBAAgB,eAAe,CAAC,EAAE,QAAQ,EAAE,EAAE,oBAAoB,2CAsBjE"}
@@ -1,6 +0,0 @@
1
- import { type CSSProperties } from "react";
2
- import { type AgentElement, type AgentFrameProps } from "./agent-frame";
3
- export declare const ChatbotSheet: import("react").ForwardRefExoticComponent<AgentFrameProps & {
4
- sheetStyle?: CSSProperties;
5
- } & import("react").RefAttributes<AgentElement>>;
6
- //# sourceMappingURL=chatbot-sheet.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"chatbot-sheet.d.ts","sourceRoot":"","sources":["../src/chatbot-sheet.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAc,KAAK,aAAa,EAAE,MAAM,OAAO,CAAC;AACvD,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,eAAe,EACrB,MAAM,eAAe,CAAC;AAcvB,eAAO,MAAM,YAAY;iBAHV,aAAa;gDAyB3B,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,wCAAwC,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"sheet.d.ts","sourceRoot":"","sources":["../src/sheet.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,cAAc,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAG/B,iBAAS,KAAK,CAAC,EAAE,GAAG,KAAK,EAAE,EAAE,KAAK,CAAC,cAAc,CAAC,OAAO,cAAc,CAAC,IAAI,CAAC,2CAW5E;AAED,QAAA,MAAM,YAAY,mKAUhB,CAAC;AAGH,QAAA,MAAM,UAAU,iKAUd,CAAC;AASH,QAAA,MAAM,YAAY,6JAgChB,CAAC;AAGH,QAAA,MAAM,WAAW,6GAUf,CAAC;AAGH,QAAA,MAAM,WAAW,6GAUf,CAAC;AAGH,QAAA,MAAM,UAAU,mKAUd,CAAC;AAGH,QAAA,MAAM,gBAAgB,6KAUpB,CAAC;AAGH,OAAO,EACL,KAAK,EACL,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,WAAW,EACX,WAAW,EACX,UAAU,EACV,gBAAgB,GACjB,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"tooltip.d.ts","sourceRoot":"","sources":["../src/tooltip.tsx"],"names":[],"mappings":"AAWA,KAAK,IAAI,GAAG,KAAK,GAAG,OAAO,GAAG,QAAQ,GAAG,MAAM,CAAC;AAChD,KAAK,KAAK,GAAG,OAAO,GAAG,QAAQ,GAAG,KAAK,CAAC;AAExC,KAAK,YAAY,GAAG;IAClB,OAAO,EAAE,KAAK,CAAC,SAAS,CAAC;IACzB,IAAI,CAAC,EAAE,IAAI,CAAC;IACZ,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,OAAO,EAAE,KAAK,CAAC,SAAS,CAAC;IACzB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B,CAAC;AAEF,wBAAgB,OAAO,CAAC,EACtB,OAAO,EACP,OAAO,EACP,IAAI,EACJ,KAAK,EACL,QAAQ,EACR,aAAa,EACb,iBAAiB,EACjB,SAAS,EACT,IAAI,EACJ,WAAW,EACX,UAAU,EACV,cAAc,EACd,eAAe,GAChB,EAAE,YAAY,2CA0Cd"}
@@ -1,196 +0,0 @@
1
- import {
2
- forwardRef,
3
- useCallback,
4
- useEffect,
5
- useImperativeHandle,
6
- useRef,
7
- useState,
8
- } from "react";
9
- import { useChatbot } from "./chatbot-proivder";
10
- import { PROD_CHATBOT_URL } from "./constants";
11
-
12
- type IframeMessage =
13
- | { type: "CHATBOT_READY" }
14
- | { type: "CLOSE_CHATBOT" }
15
- | { type: "EXPAND_CHATBOT" }
16
- | { type: "SHRINK_CHATBOT" };
17
-
18
- type MCP = {
19
- name: string;
20
- url: string;
21
- headers?: Record<string, string>;
22
- };
23
-
24
- export type InitProps = {
25
- mcps?: MCP[];
26
- };
27
-
28
- type ParentMessage =
29
- | {
30
- type: "mcps";
31
- mcps: MCP[];
32
- }
33
- | {
34
- type: "appName";
35
- appName: string;
36
- }
37
- | {
38
- type: "init";
39
- mcps?: MCP[];
40
- appName?: string;
41
- };
42
-
43
- export type AgentFrameProps = {
44
- agentId: string;
45
- mcps?: MCP[];
46
- appName?: string;
47
- url?: string;
48
- className?: string;
49
- };
50
-
51
- export type AgentElement = {
52
- /**
53
- * Push a new list of MCPs to the embedded agent.
54
- */
55
- setMcps: (mcps: MCP[]) => void;
56
- /**
57
- * Set the app name for the embedded agent.
58
- */
59
- setAppName: (appName: string) => void;
60
- /**
61
- * `true` once the iframe has sent the `CHATBOT_READY` handshake message.
62
- */
63
- isReady: boolean;
64
- };
65
-
66
- export const AgentFrame = forwardRef<AgentElement, AgentFrameProps>(
67
- ({ agentId, mcps = [], appName, className, url }, ref) => {
68
- const chatbotUrl = url ?? process.env.CHATBOT_URL ?? PROD_CHATBOT_URL;
69
-
70
- const iframeRef = useRef<HTMLIFrameElement>(null);
71
- const [isReady, setIsReady] = useState(false);
72
- const [hasInitialized, setHasInitialized] = useState(false);
73
- const [currentMcps, setCurrentMcps] = useState<MCP[]>(mcps);
74
- const { setIsExpanded, setOpen } = useChatbot();
75
-
76
- // helper to post message to the iframe
77
- const postMessage = useCallback((message: ParentMessage) => {
78
- if (iframeRef.current?.contentWindow) {
79
- iframeRef.current.contentWindow.postMessage(message, "*");
80
- }
81
- }, []);
82
-
83
- const init = useCallback(
84
- (props: InitProps) => {
85
- postMessage({
86
- type: "init",
87
- ...props,
88
- });
89
- },
90
- [postMessage],
91
- );
92
-
93
- const setMcps = useCallback(
94
- (mcps: MCP[]) => {
95
- postMessage({ type: "mcps", mcps });
96
- },
97
- [postMessage],
98
- );
99
-
100
- const setAppName = useCallback(
101
- (appName: string) => {
102
- postMessage({ type: "appName", appName });
103
- },
104
- [postMessage],
105
- );
106
-
107
- // Expose imperative API to parents.
108
- useImperativeHandle(
109
- ref,
110
- () => ({
111
- setMcps,
112
- setAppName,
113
- get isReady() {
114
- return isReady;
115
- },
116
- }),
117
- [setMcps, setAppName, isReady],
118
- );
119
-
120
- // Initialize and setup listeners
121
- useEffect(() => {
122
- const iframe = iframeRef.current;
123
- if (!iframe) {
124
- return;
125
- }
126
-
127
- const handleMessage = (event: MessageEvent<IframeMessage>) => {
128
- if (process.env.NODE_ENV === "development") {
129
- console.log("iframe message", event.data);
130
- }
131
- // Handle iframe messages
132
- switch (event.data?.type) {
133
- case "CHATBOT_READY":
134
- // When the iframe is ready, set isReady flag to kick off the initialization
135
- setIsReady(true);
136
- break;
137
- case "CLOSE_CHATBOT":
138
- setOpen(false);
139
- break;
140
- case "EXPAND_CHATBOT":
141
- setIsExpanded(true);
142
- break;
143
- case "SHRINK_CHATBOT":
144
- setIsExpanded(false);
145
- break;
146
- }
147
- };
148
-
149
- const handleIframeLoad = () => {
150
- window.addEventListener("message", handleMessage);
151
- };
152
-
153
- iframe.addEventListener("load", handleIframeLoad);
154
-
155
- return () => {
156
- window.removeEventListener("message", handleMessage);
157
- iframe.removeEventListener("load", handleIframeLoad);
158
- };
159
- // eslint-disable-next-line react-hooks/exhaustive-deps
160
- }, []); // Only setup the message listeners once
161
-
162
- // initialize the chatbot
163
- useEffect(() => {
164
- if (isReady && !hasInitialized) {
165
- setHasInitialized(true);
166
- init({ mcps });
167
- if (appName) {
168
- setAppName(appName);
169
- }
170
- }
171
- }, [isReady, init, mcps, appName, hasInitialized, setAppName]);
172
-
173
- // set mcps every time they change
174
- useEffect(() => {
175
- if (JSON.stringify(currentMcps) !== JSON.stringify(mcps)) {
176
- setCurrentMcps(mcps);
177
- setMcps(mcps);
178
- }
179
- }, [setMcps, mcps, currentMcps]);
180
-
181
- return (
182
- <iframe
183
- ref={iframeRef}
184
- src={`${chatbotUrl}/embed/${agentId}`}
185
- className={className}
186
- allow="clipboard-write"
187
- style={{
188
- width: "100%",
189
- height: "100%",
190
- }}
191
- />
192
- );
193
- },
194
- );
195
-
196
- AgentFrame.displayName = "AgentFrame";
@@ -1,51 +0,0 @@
1
- "use client";
2
-
3
- import React from "react";
4
-
5
- interface ChatbotContextType {
6
- hasOpened: boolean;
7
- open: boolean;
8
- setOpen: (open: boolean) => void;
9
- isExpanded: boolean;
10
- setIsExpanded: (expanded: boolean) => void;
11
- }
12
-
13
- const ChatbotContext = React.createContext<ChatbotContextType | undefined>(
14
- undefined,
15
- );
16
-
17
- export function useChatbot() {
18
- const context = React.useContext(ChatbotContext);
19
- if (!context) {
20
- throw new Error("useChatbot must be used within a ChatbotProvider");
21
- }
22
- return context;
23
- }
24
-
25
- interface ChatbotProviderProps {
26
- children: React.ReactNode;
27
- }
28
-
29
- export function ChatbotProvider({ children }: ChatbotProviderProps) {
30
- const [hasOpened, setHasOpened] = React.useState(false);
31
- const [open, setOpen] = React.useState(false);
32
- const [isExpanded, setIsExpanded] = React.useState(false);
33
-
34
- React.useEffect(() => {
35
- if (open) {
36
- setHasOpened(true);
37
- }
38
- }, [open]);
39
-
40
- const value = {
41
- hasOpened,
42
- open,
43
- setOpen,
44
- isExpanded,
45
- setIsExpanded,
46
- };
47
-
48
- return (
49
- <ChatbotContext.Provider value={value}>{children}</ChatbotContext.Provider>
50
- );
51
- }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes