@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
package/dist/index.mjs CHANGED
@@ -31,171 +31,202 @@ var __objRest = (source, exclude) => {
31
31
  return target;
32
32
  };
33
33
 
34
- // src/chatbot-proivder.tsx
34
+ // src/components/provider/chatbot-proivder.tsx
35
+ import React3 from "react";
36
+
37
+ // src/components/provider/use-chatbot-frame-handler.tsx
35
38
  import React from "react";
39
+ function useChatbotFrameHandler({
40
+ setOpen,
41
+ setIsExpanded
42
+ }) {
43
+ const [iframeEl, setIframeEl] = React.useState(
44
+ null
45
+ );
46
+ const [isReady, setIsReady] = React.useState(false);
47
+ const [isInitialized, setIsInitialized] = React.useState(false);
48
+ const postMessage = React.useCallback(
49
+ (message) => {
50
+ if (iframeEl == null ? void 0 : iframeEl.contentWindow) {
51
+ if (process.env.NODE_ENV === "development") {
52
+ console.log("sending message to iframe", message);
53
+ }
54
+ iframeEl.contentWindow.postMessage(message, "*");
55
+ }
56
+ },
57
+ [iframeEl]
58
+ );
59
+ const init = React.useCallback(
60
+ (props) => {
61
+ postMessage(__spreadValues({
62
+ type: "init"
63
+ }, props));
64
+ },
65
+ [postMessage]
66
+ );
67
+ const setMcps = React.useCallback(
68
+ (mcps) => {
69
+ postMessage({ type: "mcps", mcps });
70
+ },
71
+ [postMessage]
72
+ );
73
+ const setAppName = React.useCallback(
74
+ (appName) => {
75
+ postMessage({ type: "appName", appName });
76
+ },
77
+ [postMessage]
78
+ );
79
+ React.useEffect(() => {
80
+ const iframe = iframeEl;
81
+ if (!iframe) {
82
+ return;
83
+ }
84
+ const handleMessage = (event) => {
85
+ var _a;
86
+ if (process.env.NODE_ENV === "development") {
87
+ console.log("iframe message", event.data);
88
+ }
89
+ switch ((_a = event.data) == null ? void 0 : _a.type) {
90
+ case "CHATBOT_READY":
91
+ setIsReady(true);
92
+ break;
93
+ case "CHATBOT_INITIALIZED":
94
+ setIsInitialized(true);
95
+ break;
96
+ case "CLOSE_CHATBOT":
97
+ setOpen(false);
98
+ break;
99
+ case "EXPAND_CHATBOT":
100
+ setIsExpanded(true);
101
+ break;
102
+ case "SHRINK_CHATBOT":
103
+ setIsExpanded(false);
104
+ break;
105
+ }
106
+ };
107
+ const handleIframeLoad = () => {
108
+ window.addEventListener("message", handleMessage);
109
+ };
110
+ iframe.addEventListener("load", handleIframeLoad);
111
+ return () => {
112
+ window.removeEventListener("message", handleMessage);
113
+ iframe.removeEventListener("load", handleIframeLoad);
114
+ };
115
+ }, [iframeEl]);
116
+ return { init, setMcps, setAppName, setIframeEl, isReady, isInitialized };
117
+ }
118
+
119
+ // src/components/provider/use-chatbot-window-states.tsx
120
+ import React2 from "react";
121
+ function useChatbotWindowStates() {
122
+ const [hasOpened, setHasOpened] = React2.useState(false);
123
+ const [open, setOpen] = React2.useState(false);
124
+ const [isExpanded, setIsExpanded] = React2.useState(false);
125
+ React2.useEffect(() => {
126
+ if (open) {
127
+ setHasOpened(true);
128
+ }
129
+ }, [open]);
130
+ return { hasOpened, open, isExpanded, setOpen, setIsExpanded };
131
+ }
132
+
133
+ // src/components/provider/chatbot-proivder.tsx
36
134
  import { jsx } from "react/jsx-runtime";
37
- var ChatbotContext = React.createContext(
135
+ var ChatbotContext = React3.createContext(
38
136
  void 0
39
137
  );
40
138
  function useChatbot() {
41
- const context = React.useContext(ChatbotContext);
139
+ const context = React3.useContext(ChatbotContext);
42
140
  if (!context) {
43
141
  throw new Error("useChatbot must be used within a ChatbotProvider");
44
142
  }
45
143
  return context;
46
144
  }
47
145
  function ChatbotProvider({ children }) {
48
- const [hasOpened, setHasOpened] = React.useState(false);
49
- const [open, setOpen] = React.useState(false);
50
- const [isExpanded, setIsExpanded] = React.useState(false);
51
- React.useEffect(() => {
52
- if (open) {
53
- setHasOpened(true);
54
- }
55
- }, [open]);
146
+ const { hasOpened, open, isExpanded, setOpen, setIsExpanded } = useChatbotWindowStates();
147
+ const { setMcps, setAppName, init, isReady, isInitialized, setIframeEl } = useChatbotFrameHandler({
148
+ setOpen,
149
+ setIsExpanded
150
+ });
56
151
  const value = {
57
152
  hasOpened,
58
153
  open,
59
154
  setOpen,
60
155
  isExpanded,
61
- setIsExpanded
156
+ setIsExpanded,
157
+ init,
158
+ setMcps,
159
+ setAppName,
160
+ isReady,
161
+ isInitialized,
162
+ setIframeEl
62
163
  };
63
164
  return /* @__PURE__ */ jsx(ChatbotContext.Provider, { value, children });
64
165
  }
65
166
 
66
- // src/agent-frame.tsx
67
- import {
68
- forwardRef,
69
- useCallback,
70
- useEffect,
71
- useImperativeHandle,
72
- useRef,
73
- useState
74
- } from "react";
167
+ // src/hooks/use-chatbot-config.tsx
168
+ import React4 from "react";
169
+ function useChatbotConfig({
170
+ mcps,
171
+ appName,
172
+ enabled = true
173
+ }) {
174
+ const { init, setMcps, setAppName, isReady } = useChatbot();
175
+ const sentInitRef = React4.useRef(false);
176
+ const [currentMcps, setCurrentMcps] = React4.useState();
177
+ const [currentAppName, setCurrentAppName] = React4.useState();
178
+ React4.useEffect(() => {
179
+ if (enabled && isReady && !sentInitRef.current) {
180
+ init({ mcps, appName });
181
+ sentInitRef.current = true;
182
+ setCurrentMcps(mcps);
183
+ setCurrentAppName(appName);
184
+ }
185
+ }, [enabled, init, mcps, appName, isReady]);
186
+ React4.useEffect(() => {
187
+ if (sentInitRef.current && mcps && JSON.stringify(currentMcps) !== JSON.stringify(mcps)) {
188
+ setMcps(mcps);
189
+ setCurrentMcps(mcps);
190
+ }
191
+ }, [mcps]);
192
+ React4.useEffect(() => {
193
+ if (sentInitRef.current && appName && currentAppName !== appName) {
194
+ setCurrentAppName(appName);
195
+ setAppName(appName);
196
+ }
197
+ }, [appName]);
198
+ }
75
199
 
76
- // src/constants.ts
200
+ // src/lib/constants.ts
77
201
  var PROD_CHATBOT_URL = "https://ecforce-ai-agent.vercel.app";
78
202
 
79
- // src/agent-frame.tsx
203
+ // src/components/chatbot-frame.tsx
80
204
  import { jsx as jsx2 } from "react/jsx-runtime";
81
- var AgentFrame = forwardRef(
82
- ({ agentId, mcps = [], appName, className, url }, ref) => {
83
- var _a;
84
- const chatbotUrl = (_a = url != null ? url : process.env.CHATBOT_URL) != null ? _a : PROD_CHATBOT_URL;
85
- const iframeRef = useRef(null);
86
- const [isReady, setIsReady] = useState(false);
87
- const [hasInitialized, setHasInitialized] = useState(false);
88
- const [currentMcps, setCurrentMcps] = useState(mcps);
89
- const { setIsExpanded, setOpen } = useChatbot();
90
- const postMessage = useCallback((message) => {
91
- var _a2;
92
- if ((_a2 = iframeRef.current) == null ? void 0 : _a2.contentWindow) {
93
- iframeRef.current.contentWindow.postMessage(message, "*");
94
- }
95
- }, []);
96
- const init = useCallback(
97
- (props) => {
98
- postMessage(__spreadValues({
99
- type: "init"
100
- }, props));
101
- },
102
- [postMessage]
103
- );
104
- const setMcps = useCallback(
105
- (mcps2) => {
106
- postMessage({ type: "mcps", mcps: mcps2 });
107
- },
108
- [postMessage]
109
- );
110
- const setAppName = useCallback(
111
- (appName2) => {
112
- postMessage({ type: "appName", appName: appName2 });
113
- },
114
- [postMessage]
115
- );
116
- useImperativeHandle(
117
- ref,
118
- () => ({
119
- setMcps,
120
- setAppName,
121
- get isReady() {
122
- return isReady;
123
- }
124
- }),
125
- [setMcps, setAppName, isReady]
126
- );
127
- useEffect(() => {
128
- const iframe = iframeRef.current;
129
- if (!iframe) {
130
- return;
131
- }
132
- const handleMessage = (event) => {
133
- var _a2;
134
- if (process.env.NODE_ENV === "development") {
135
- console.log("iframe message", event.data);
136
- }
137
- switch ((_a2 = event.data) == null ? void 0 : _a2.type) {
138
- case "CHATBOT_READY":
139
- setIsReady(true);
140
- break;
141
- case "CLOSE_CHATBOT":
142
- setOpen(false);
143
- break;
144
- case "EXPAND_CHATBOT":
145
- setIsExpanded(true);
146
- break;
147
- case "SHRINK_CHATBOT":
148
- setIsExpanded(false);
149
- break;
150
- }
151
- };
152
- const handleIframeLoad = () => {
153
- window.addEventListener("message", handleMessage);
154
- };
155
- iframe.addEventListener("load", handleIframeLoad);
156
- return () => {
157
- window.removeEventListener("message", handleMessage);
158
- iframe.removeEventListener("load", handleIframeLoad);
159
- };
160
- }, []);
161
- useEffect(() => {
162
- if (isReady && !hasInitialized) {
163
- setHasInitialized(true);
164
- init({ mcps });
165
- if (appName) {
166
- setAppName(appName);
167
- }
168
- }
169
- }, [isReady, init, mcps, appName, hasInitialized, setAppName]);
170
- useEffect(() => {
171
- if (JSON.stringify(currentMcps) !== JSON.stringify(mcps)) {
172
- setCurrentMcps(mcps);
173
- setMcps(mcps);
174
- }
175
- }, [setMcps, mcps, currentMcps]);
176
- return /* @__PURE__ */ jsx2(
177
- "iframe",
178
- {
179
- ref: iframeRef,
180
- src: `${chatbotUrl}/embed/${agentId}`,
181
- className,
182
- allow: "clipboard-write",
183
- style: {
184
- width: "100%",
185
- height: "100%"
186
- }
205
+ function ChatbotFrame({ agentId, className, url }) {
206
+ var _a;
207
+ const chatbotUrl = (_a = url != null ? url : process.env.CHATBOT_URL) != null ? _a : PROD_CHATBOT_URL;
208
+ const { setIframeEl } = useChatbot();
209
+ return /* @__PURE__ */ jsx2(
210
+ "iframe",
211
+ {
212
+ ref: setIframeEl,
213
+ src: `${chatbotUrl}/embed/${agentId}`,
214
+ className,
215
+ allow: "clipboard-write",
216
+ style: {
217
+ width: "100%",
218
+ height: "100%"
187
219
  }
188
- );
189
- }
190
- );
191
- AgentFrame.displayName = "AgentFrame";
220
+ }
221
+ );
222
+ }
192
223
 
193
- // src/chatbot-sheet.tsx
194
- import { forwardRef as forwardRef3 } from "react";
224
+ // src/components/chatbot-sheet.tsx
225
+ import "react";
195
226
 
196
- // src/sheet.tsx
227
+ // src/components/sheet.tsx
197
228
  import * as SheetPrimitive from "@radix-ui/react-dialog";
198
- import * as React2 from "react";
229
+ import * as React5 from "react";
199
230
  import { jsx as jsx3 } from "react/jsx-runtime";
200
231
  function Sheet(_a) {
201
232
  var props = __objRest(_a, []);
@@ -210,7 +241,7 @@ function Sheet(_a) {
210
241
  }, props)
211
242
  );
212
243
  }
213
- var SheetTrigger = React2.forwardRef((_a, ref) => {
244
+ var SheetTrigger = React5.forwardRef((_a, ref) => {
214
245
  var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
215
246
  return /* @__PURE__ */ jsx3(
216
247
  SheetPrimitive.Trigger,
@@ -222,7 +253,7 @@ var SheetTrigger = React2.forwardRef((_a, ref) => {
222
253
  );
223
254
  });
224
255
  SheetTrigger.displayName = SheetPrimitive.Trigger.displayName;
225
- var SheetClose = React2.forwardRef((_a, ref) => {
256
+ var SheetClose = React5.forwardRef((_a, ref) => {
226
257
  var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
227
258
  return /* @__PURE__ */ jsx3(
228
259
  SheetPrimitive.Close,
@@ -238,7 +269,7 @@ function SheetPortal(_a) {
238
269
  var props = __objRest(_a, []);
239
270
  return /* @__PURE__ */ jsx3(SheetPrimitive.Portal, __spreadValues({ "data-slot": "sheet-portal" }, props));
240
271
  }
241
- var SheetContent = React2.forwardRef((_a, ref) => {
272
+ var SheetContent = React5.forwardRef((_a, ref) => {
242
273
  var _b = _a, { className, children, style } = _b, props = __objRest(_b, ["className", "children", "style"]);
243
274
  const { isExpanded, open, hasOpened } = useChatbot();
244
275
  const width = isExpanded ? "848px" : "400px";
@@ -262,7 +293,7 @@ var SheetContent = React2.forwardRef((_a, ref) => {
262
293
  ) });
263
294
  });
264
295
  SheetContent.displayName = SheetPrimitive.Content.displayName;
265
- var SheetHeader = React2.forwardRef((_a, ref) => {
296
+ var SheetHeader = React5.forwardRef((_a, ref) => {
266
297
  var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
267
298
  return /* @__PURE__ */ jsx3(
268
299
  "div",
@@ -274,7 +305,7 @@ var SheetHeader = React2.forwardRef((_a, ref) => {
274
305
  );
275
306
  });
276
307
  SheetHeader.displayName = "SheetHeader";
277
- var SheetFooter = React2.forwardRef((_a, ref) => {
308
+ var SheetFooter = React5.forwardRef((_a, ref) => {
278
309
  var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
279
310
  return /* @__PURE__ */ jsx3(
280
311
  "div",
@@ -286,7 +317,7 @@ var SheetFooter = React2.forwardRef((_a, ref) => {
286
317
  );
287
318
  });
288
319
  SheetFooter.displayName = "SheetFooter";
289
- var SheetTitle = React2.forwardRef((_a, ref) => {
320
+ var SheetTitle = React5.forwardRef((_a, ref) => {
290
321
  var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
291
322
  return /* @__PURE__ */ jsx3(
292
323
  SheetPrimitive.Title,
@@ -298,7 +329,7 @@ var SheetTitle = React2.forwardRef((_a, ref) => {
298
329
  );
299
330
  });
300
331
  SheetTitle.displayName = SheetPrimitive.Title.displayName;
301
- var SheetDescription = React2.forwardRef((_a, ref) => {
332
+ var SheetDescription = React5.forwardRef((_a, ref) => {
302
333
  var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
303
334
  return /* @__PURE__ */ jsx3(
304
335
  SheetPrimitive.Description,
@@ -311,7 +342,7 @@ var SheetDescription = React2.forwardRef((_a, ref) => {
311
342
  });
312
343
  SheetDescription.displayName = SheetPrimitive.Description.displayName;
313
344
 
314
- // src/tooltip.tsx
345
+ // src/components/tooltip.tsx
315
346
  import {
316
347
  Arrow,
317
348
  Content as Content2,
@@ -374,29 +405,27 @@ function Tooltip({
374
405
  );
375
406
  }
376
407
 
377
- // src/chatbot-sheet.tsx
408
+ // src/components/chatbot-sheet.tsx
378
409
  import { jsx as jsx5, jsxs as jsxs2 } from "react/jsx-runtime";
379
- var ChatbotSheet = forwardRef3(
380
- (_a, ref) => {
381
- var _b = _a, { sheetStyle } = _b, props = __objRest(_b, ["sheetStyle"]);
382
- return /* @__PURE__ */ jsxs2(Sheet, { children: [
383
- /* @__PURE__ */ jsx5(
384
- Tooltip,
385
- {
386
- side: "top",
387
- align: "end",
388
- content: "AI\u306B\u8CEA\u554F\u3057\u3066\u307F\u307E\u3057\u3087\u3046",
389
- trigger: /* @__PURE__ */ jsx5(SheetTrigger, { children: /* @__PURE__ */ jsx5(EcforceAiIcon, {}) })
390
- }
391
- ),
392
- /* @__PURE__ */ jsxs2(SheetContent, { style: sheetStyle, children: [
393
- /* @__PURE__ */ jsx5(SheetTitle, { children: "AI\u306B\u8CEA\u554F\u3057\u3066\u307F\u307E\u3057\u3087\u3046" }),
394
- /* @__PURE__ */ jsx5(SheetDescription, { children: "AI\u306B\u8CEA\u554F\u3057\u3066\u307F\u307E\u3057\u3087\u3046" }),
395
- /* @__PURE__ */ jsx5(AgentFrame, __spreadProps(__spreadValues({}, props), { ref }))
396
- ] })
397
- ] });
398
- }
399
- );
410
+ function ChatbotSheet(_a) {
411
+ var _b = _a, { sheetStyle } = _b, props = __objRest(_b, ["sheetStyle"]);
412
+ return /* @__PURE__ */ jsxs2(Sheet, { children: [
413
+ /* @__PURE__ */ jsx5(
414
+ Tooltip,
415
+ {
416
+ side: "top",
417
+ align: "end",
418
+ content: "AI\u306B\u8CEA\u554F\u3057\u3066\u307F\u307E\u3057\u3087\u3046",
419
+ trigger: /* @__PURE__ */ jsx5(SheetTrigger, { children: /* @__PURE__ */ jsx5(EcforceAiIcon, {}) })
420
+ }
421
+ ),
422
+ /* @__PURE__ */ jsxs2(SheetContent, { style: sheetStyle, children: [
423
+ /* @__PURE__ */ jsx5(SheetTitle, { children: "AI\u306B\u8CEA\u554F\u3057\u3066\u307F\u307E\u3057\u3087\u3046" }),
424
+ /* @__PURE__ */ jsx5(SheetDescription, { children: "AI\u306B\u8CEA\u554F\u3057\u3066\u307F\u307E\u3057\u3087\u3046" }),
425
+ /* @__PURE__ */ jsx5(ChatbotFrame, __spreadValues({}, props))
426
+ ] })
427
+ ] });
428
+ }
400
429
  function EcforceAiIcon() {
401
430
  return /* @__PURE__ */ jsx5(
402
431
  "svg",
@@ -418,7 +447,7 @@ function EcforceAiIcon() {
418
447
  );
419
448
  }
420
449
  export {
421
- AgentFrame,
450
+ ChatbotFrame,
422
451
  ChatbotProvider,
423
452
  ChatbotSheet,
424
453
  Sheet,
@@ -430,5 +459,6 @@ export {
430
459
  SheetTitle,
431
460
  SheetTrigger,
432
461
  Tooltip,
433
- useChatbot
462
+ useChatbot,
463
+ useChatbotConfig
434
464
  };
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/lib/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,wCAAwC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@super_studio/ecforce-ai-agent-react",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",
@@ -17,8 +17,8 @@
17
17
  "require": "./dist/index.js",
18
18
  "default": "./dist/index.js"
19
19
  },
20
- "./preset.css": "./dist/preset.css",
21
- "./chatbot-sheet.css": "./dist/chatbot-sheet.css"
20
+ "./preset.css": "./dist/styles/preset.css",
21
+ "./chatbot-sheet.css": "./dist/styles/chatbot-sheet.css"
22
22
  },
23
23
  "publishConfig": {
24
24
  "access": "public"
@@ -0,0 +1,26 @@
1
+ import { PROD_CHATBOT_URL } from "../lib/constants";
2
+ import { useChatbot } from "./provider/chatbot-proivder";
3
+
4
+ export type ChatbotFrameProps = {
5
+ agentId: string;
6
+ url?: string;
7
+ className?: string;
8
+ };
9
+
10
+ export function ChatbotFrame({ agentId, className, url }: ChatbotFrameProps) {
11
+ const chatbotUrl = url ?? process.env.CHATBOT_URL ?? PROD_CHATBOT_URL;
12
+ const { setIframeEl } = useChatbot();
13
+
14
+ return (
15
+ <iframe
16
+ ref={setIframeEl}
17
+ src={`${chatbotUrl}/embed/${agentId}`}
18
+ className={className}
19
+ allow="clipboard-write"
20
+ style={{
21
+ width: "100%",
22
+ height: "100%",
23
+ }}
24
+ />
25
+ );
26
+ }
@@ -1,9 +1,5 @@
1
- import { forwardRef, type CSSProperties } from "react";
2
- import {
3
- AgentFrame,
4
- type AgentElement,
5
- type AgentFrameProps,
6
- } from "./agent-frame";
1
+ import React from "react";
2
+ import { ChatbotFrame, type ChatbotFrameProps } from "./chatbot-frame";
7
3
  import {
8
4
  Sheet,
9
5
  SheetContent,
@@ -13,33 +9,31 @@ import {
13
9
  } from "./sheet";
14
10
  import { Tooltip } from "./tooltip";
15
11
 
16
- type ChatbotSheetProps = AgentFrameProps & {
17
- sheetStyle?: CSSProperties;
12
+ type ChatbotSheetProps = ChatbotFrameProps & {
13
+ sheetStyle?: React.CSSProperties;
18
14
  };
19
15
 
20
- export const ChatbotSheet = forwardRef<AgentElement, ChatbotSheetProps>(
21
- ({ sheetStyle, ...props }, ref) => {
22
- return (
23
- <Sheet>
24
- <Tooltip
25
- side="top"
26
- align="end"
27
- content="AIに質問してみましょう"
28
- trigger={
29
- <SheetTrigger>
30
- <EcforceAiIcon />
31
- </SheetTrigger>
32
- }
33
- />
34
- <SheetContent style={sheetStyle}>
35
- <SheetTitle>AIに質問してみましょう</SheetTitle>
36
- <SheetDescription>AIに質問してみましょう</SheetDescription>
37
- <AgentFrame {...props} ref={ref} />
38
- </SheetContent>
39
- </Sheet>
40
- );
41
- },
42
- );
16
+ export function ChatbotSheet({ sheetStyle, ...props }: ChatbotSheetProps) {
17
+ return (
18
+ <Sheet>
19
+ <Tooltip
20
+ side="top"
21
+ align="end"
22
+ content="AIに質問してみましょう"
23
+ trigger={
24
+ <SheetTrigger>
25
+ <EcforceAiIcon />
26
+ </SheetTrigger>
27
+ }
28
+ />
29
+ <SheetContent style={sheetStyle}>
30
+ <SheetTitle>AIに質問してみましょう</SheetTitle>
31
+ <SheetDescription>AIに質問してみましょう</SheetDescription>
32
+ <ChatbotFrame {...props} />
33
+ </SheetContent>
34
+ </Sheet>
35
+ );
36
+ }
43
37
 
44
38
  function EcforceAiIcon() {
45
39
  return (
@@ -0,0 +1,79 @@
1
+ import React from "react";
2
+ import {
3
+ useChatbotFrameHandler,
4
+ type InitProps,
5
+ type MCP,
6
+ } from "./use-chatbot-frame-handler";
7
+ import { useChatbotWindowStates } from "./use-chatbot-window-states";
8
+
9
+ type ChatbotContextType = {
10
+ /** 1回チャットボットを開いたら、固定でtrueになる */
11
+ hasOpened: boolean;
12
+ /** チャットボットを開いているか */
13
+ open: boolean;
14
+ /** チャットボットを開く */
15
+ setOpen: (open: boolean) => void;
16
+ /** チャットボットを展開しているか */
17
+ isExpanded: boolean;
18
+ /** チャットボットを展開する */
19
+ setIsExpanded: (expanded: boolean) => void;
20
+ /** チャットボットを初期化 */
21
+ init: (props: InitProps) => void;
22
+ /** MCPを設定 */
23
+ setMcps: (mcps: MCP[]) => void;
24
+ /** アプリ名を設定 */
25
+ setAppName: (appName: string) => void;
26
+ /** チャットボットが準備できたか */
27
+ isReady: boolean;
28
+ /** チャットボットが初期化されたか */
29
+ isInitialized: boolean;
30
+ /**
31
+ * @internal
32
+ * チャットボットのiframe要素を設定
33
+ */
34
+ setIframeEl: (iframeEl: HTMLIFrameElement | null) => void;
35
+ };
36
+
37
+ const ChatbotContext = React.createContext<ChatbotContextType | undefined>(
38
+ undefined,
39
+ );
40
+
41
+ export function useChatbot() {
42
+ const context = React.useContext(ChatbotContext);
43
+ if (!context) {
44
+ throw new Error("useChatbot must be used within a ChatbotProvider");
45
+ }
46
+ return context;
47
+ }
48
+
49
+ type ChatbotProviderProps = {
50
+ children: React.ReactNode;
51
+ };
52
+
53
+ export function ChatbotProvider({ children }: ChatbotProviderProps) {
54
+ const { hasOpened, open, isExpanded, setOpen, setIsExpanded } =
55
+ useChatbotWindowStates();
56
+ const { setMcps, setAppName, init, isReady, isInitialized, setIframeEl } =
57
+ useChatbotFrameHandler({
58
+ setOpen,
59
+ setIsExpanded,
60
+ });
61
+
62
+ const value = {
63
+ hasOpened,
64
+ open,
65
+ setOpen,
66
+ isExpanded,
67
+ setIsExpanded,
68
+ init,
69
+ setMcps,
70
+ setAppName,
71
+ isReady,
72
+ isInitialized,
73
+ setIframeEl,
74
+ };
75
+
76
+ return (
77
+ <ChatbotContext.Provider value={value}>{children}</ChatbotContext.Provider>
78
+ );
79
+ }