@super_studio/ecforce-ai-agent-react 0.3.1 → 0.5.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 (45) hide show
  1. package/dist/components/chatbot-frame.d.ts +25 -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/index.d.ts +5 -5
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +197 -167
  16. package/dist/index.mjs +198 -168
  17. package/dist/lib/constants.d.ts.map +1 -0
  18. package/package.json +3 -3
  19. package/src/components/chatbot-frame.tsx +86 -0
  20. package/src/{chatbot-sheet.tsx → components/chatbot-sheet.tsx} +25 -31
  21. package/src/components/provider/chatbot-proivder.tsx +79 -0
  22. package/src/components/provider/use-chatbot-frame-handler.tsx +130 -0
  23. package/src/components/provider/use-chatbot-window-states.tsx +15 -0
  24. package/src/{sheet.tsx → components/sheet.tsx} +1 -3
  25. package/src/{tooltip.tsx → components/tooltip.tsx} +0 -2
  26. package/src/index.ts +5 -5
  27. package/dist/agent-frame.d.ts +0 -32
  28. package/dist/agent-frame.d.ts.map +0 -1
  29. package/dist/chatbot-proivder.d.ts +0 -15
  30. package/dist/chatbot-proivder.d.ts.map +0 -1
  31. package/dist/chatbot-sheet.d.ts +0 -6
  32. package/dist/chatbot-sheet.d.ts.map +0 -1
  33. package/dist/constants.d.ts.map +0 -1
  34. package/dist/sheet.d.ts.map +0 -1
  35. package/dist/tooltip.d.ts.map +0 -1
  36. package/src/agent-frame.tsx +0 -196
  37. package/src/chatbot-proivder.tsx +0 -51
  38. /package/dist/{sheet.d.ts → components/sheet.d.ts} +0 -0
  39. /package/dist/{tooltip.d.ts → components/tooltip.d.ts} +0 -0
  40. /package/dist/{constants.d.ts → lib/constants.d.ts} +0 -0
  41. /package/dist/{chatbot-sheet.css → styles/chatbot-sheet.css} +0 -0
  42. /package/dist/{preset.css → styles/preset.css} +0 -0
  43. /package/src/{constants.ts → lib/constants.ts} +0 -0
  44. /package/src/{chatbot-sheet.css → styles/chatbot-sheet.css} +0 -0
  45. /package/src/{preset.css → styles/preset.css} +0 -0
package/dist/index.mjs CHANGED
@@ -31,171 +31,203 @@ 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/components/chatbot-frame.tsx
168
+ import React4 from "react";
75
169
 
76
- // src/constants.ts
170
+ // src/lib/constants.ts
77
171
  var PROD_CHATBOT_URL = "https://ecforce-ai-agent.vercel.app";
78
172
 
79
- // src/agent-frame.tsx
173
+ // src/components/chatbot-frame.tsx
80
174
  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
- }
175
+ function ChatbotFrame({
176
+ agentId,
177
+ appName,
178
+ mcps,
179
+ enabled = true,
180
+ url,
181
+ className
182
+ }) {
183
+ var _a;
184
+ const chatbotUrl = (_a = url != null ? url : process.env.CHATBOT_URL) != null ? _a : PROD_CHATBOT_URL;
185
+ const { setIframeEl } = useChatbot();
186
+ const { init, setMcps, setAppName, isReady } = useChatbot();
187
+ const sentInitRef = React4.useRef(false);
188
+ const [currentMcps, setCurrentMcps] = React4.useState();
189
+ const [currentAppName, setCurrentAppName] = React4.useState();
190
+ React4.useEffect(() => {
191
+ if (enabled && isReady && !sentInitRef.current) {
192
+ init({ mcps, appName });
193
+ sentInitRef.current = true;
194
+ setCurrentMcps(mcps);
195
+ setCurrentAppName(appName);
196
+ }
197
+ }, [enabled, init, mcps, appName, isReady]);
198
+ React4.useEffect(() => {
199
+ if (sentInitRef.current && mcps && JSON.stringify(currentMcps) !== JSON.stringify(mcps)) {
200
+ setMcps(mcps);
201
+ setCurrentMcps(mcps);
202
+ }
203
+ }, [mcps]);
204
+ React4.useEffect(() => {
205
+ if (sentInitRef.current && appName && currentAppName !== appName) {
206
+ setCurrentAppName(appName);
207
+ setAppName(appName);
208
+ }
209
+ }, [appName]);
210
+ return /* @__PURE__ */ jsx2(
211
+ "iframe",
212
+ {
213
+ ref: setIframeEl,
214
+ src: `${chatbotUrl}/embed/${agentId}`,
215
+ className,
216
+ allow: "clipboard-write",
217
+ style: {
218
+ width: "100%",
219
+ height: "100%"
187
220
  }
188
- );
189
- }
190
- );
191
- AgentFrame.displayName = "AgentFrame";
221
+ }
222
+ );
223
+ }
192
224
 
193
- // src/chatbot-sheet.tsx
194
- import { forwardRef as forwardRef3 } from "react";
225
+ // src/components/chatbot-sheet.tsx
226
+ import "react";
195
227
 
196
- // src/sheet.tsx
228
+ // src/components/sheet.tsx
197
229
  import * as SheetPrimitive from "@radix-ui/react-dialog";
198
- import * as React2 from "react";
230
+ import * as React5 from "react";
199
231
  import { jsx as jsx3 } from "react/jsx-runtime";
200
232
  function Sheet(_a) {
201
233
  var props = __objRest(_a, []);
@@ -210,7 +242,7 @@ function Sheet(_a) {
210
242
  }, props)
211
243
  );
212
244
  }
213
- var SheetTrigger = React2.forwardRef((_a, ref) => {
245
+ var SheetTrigger = React5.forwardRef((_a, ref) => {
214
246
  var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
215
247
  return /* @__PURE__ */ jsx3(
216
248
  SheetPrimitive.Trigger,
@@ -222,7 +254,7 @@ var SheetTrigger = React2.forwardRef((_a, ref) => {
222
254
  );
223
255
  });
224
256
  SheetTrigger.displayName = SheetPrimitive.Trigger.displayName;
225
- var SheetClose = React2.forwardRef((_a, ref) => {
257
+ var SheetClose = React5.forwardRef((_a, ref) => {
226
258
  var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
227
259
  return /* @__PURE__ */ jsx3(
228
260
  SheetPrimitive.Close,
@@ -238,7 +270,7 @@ function SheetPortal(_a) {
238
270
  var props = __objRest(_a, []);
239
271
  return /* @__PURE__ */ jsx3(SheetPrimitive.Portal, __spreadValues({ "data-slot": "sheet-portal" }, props));
240
272
  }
241
- var SheetContent = React2.forwardRef((_a, ref) => {
273
+ var SheetContent = React5.forwardRef((_a, ref) => {
242
274
  var _b = _a, { className, children, style } = _b, props = __objRest(_b, ["className", "children", "style"]);
243
275
  const { isExpanded, open, hasOpened } = useChatbot();
244
276
  const width = isExpanded ? "848px" : "400px";
@@ -262,7 +294,7 @@ var SheetContent = React2.forwardRef((_a, ref) => {
262
294
  ) });
263
295
  });
264
296
  SheetContent.displayName = SheetPrimitive.Content.displayName;
265
- var SheetHeader = React2.forwardRef((_a, ref) => {
297
+ var SheetHeader = React5.forwardRef((_a, ref) => {
266
298
  var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
267
299
  return /* @__PURE__ */ jsx3(
268
300
  "div",
@@ -274,7 +306,7 @@ var SheetHeader = React2.forwardRef((_a, ref) => {
274
306
  );
275
307
  });
276
308
  SheetHeader.displayName = "SheetHeader";
277
- var SheetFooter = React2.forwardRef((_a, ref) => {
309
+ var SheetFooter = React5.forwardRef((_a, ref) => {
278
310
  var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
279
311
  return /* @__PURE__ */ jsx3(
280
312
  "div",
@@ -286,7 +318,7 @@ var SheetFooter = React2.forwardRef((_a, ref) => {
286
318
  );
287
319
  });
288
320
  SheetFooter.displayName = "SheetFooter";
289
- var SheetTitle = React2.forwardRef((_a, ref) => {
321
+ var SheetTitle = React5.forwardRef((_a, ref) => {
290
322
  var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
291
323
  return /* @__PURE__ */ jsx3(
292
324
  SheetPrimitive.Title,
@@ -298,7 +330,7 @@ var SheetTitle = React2.forwardRef((_a, ref) => {
298
330
  );
299
331
  });
300
332
  SheetTitle.displayName = SheetPrimitive.Title.displayName;
301
- var SheetDescription = React2.forwardRef((_a, ref) => {
333
+ var SheetDescription = React5.forwardRef((_a, ref) => {
302
334
  var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
303
335
  return /* @__PURE__ */ jsx3(
304
336
  SheetPrimitive.Description,
@@ -311,7 +343,7 @@ var SheetDescription = React2.forwardRef((_a, ref) => {
311
343
  });
312
344
  SheetDescription.displayName = SheetPrimitive.Description.displayName;
313
345
 
314
- // src/tooltip.tsx
346
+ // src/components/tooltip.tsx
315
347
  import {
316
348
  Arrow,
317
349
  Content as Content2,
@@ -374,29 +406,27 @@ function Tooltip({
374
406
  );
375
407
  }
376
408
 
377
- // src/chatbot-sheet.tsx
409
+ // src/components/chatbot-sheet.tsx
378
410
  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
- );
411
+ function ChatbotSheet(_a) {
412
+ var _b = _a, { sheetStyle } = _b, props = __objRest(_b, ["sheetStyle"]);
413
+ return /* @__PURE__ */ jsxs2(Sheet, { children: [
414
+ /* @__PURE__ */ jsx5(
415
+ Tooltip,
416
+ {
417
+ side: "top",
418
+ align: "end",
419
+ content: "AI\u306B\u8CEA\u554F\u3057\u3066\u307F\u307E\u3057\u3087\u3046",
420
+ trigger: /* @__PURE__ */ jsx5(SheetTrigger, { children: /* @__PURE__ */ jsx5(EcforceAiIcon, {}) })
421
+ }
422
+ ),
423
+ /* @__PURE__ */ jsxs2(SheetContent, { style: sheetStyle, children: [
424
+ /* @__PURE__ */ jsx5(SheetTitle, { children: "AI\u306B\u8CEA\u554F\u3057\u3066\u307F\u307E\u3057\u3087\u3046" }),
425
+ /* @__PURE__ */ jsx5(SheetDescription, { children: "AI\u306B\u8CEA\u554F\u3057\u3066\u307F\u307E\u3057\u3087\u3046" }),
426
+ /* @__PURE__ */ jsx5(ChatbotFrame, __spreadValues({}, props))
427
+ ] })
428
+ ] });
429
+ }
400
430
  function EcforceAiIcon() {
401
431
  return /* @__PURE__ */ jsx5(
402
432
  "svg",
@@ -418,7 +448,7 @@ function EcforceAiIcon() {
418
448
  );
419
449
  }
420
450
  export {
421
- AgentFrame,
451
+ ChatbotFrame,
422
452
  ChatbotProvider,
423
453
  ChatbotSheet,
424
454
  Sheet,
@@ -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.5.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,86 @@
1
+ import React from "react";
2
+ import { PROD_CHATBOT_URL } from "../lib/constants";
3
+ import { useChatbot } from "./provider/chatbot-proivder";
4
+ import type { MCP } from "./provider/use-chatbot-frame-handler";
5
+
6
+ export type ChatbotFrameProps = {
7
+ /** 管理画面上のエージェントID */
8
+ agentId: string;
9
+ /** エージェントにつなげるMCPの情報 */
10
+ mcps?: MCP[];
11
+ /** アプリ名のメタデータ */
12
+ appName?: string;
13
+ /**
14
+ * trueになるまで、初期化されません。
15
+ * 初期化せれたあとはもう使われません。
16
+ * @default true
17
+ */
18
+ enabled?: boolean;
19
+ /**
20
+ * エージェントのURLを上書きしたい場合にこちらを使います。
21
+ * 基本的には使われません。
22
+ * [CHATBOT_URL]の環境変数でも設定可能。
23
+ * @internal
24
+ */
25
+ url?: string;
26
+ className?: string;
27
+ };
28
+
29
+ export function ChatbotFrame({
30
+ agentId,
31
+ appName,
32
+ mcps,
33
+ enabled = true,
34
+ url,
35
+ className,
36
+ }: ChatbotFrameProps) {
37
+ const chatbotUrl = url ?? process.env.CHATBOT_URL ?? PROD_CHATBOT_URL;
38
+ const { setIframeEl } = useChatbot();
39
+ const { init, setMcps, setAppName, isReady } = useChatbot();
40
+ const sentInitRef = React.useRef(false);
41
+ const [currentMcps, setCurrentMcps] = React.useState<MCP[]>();
42
+ const [currentAppName, setCurrentAppName] = React.useState<string>();
43
+
44
+ // 初期化の仕組み
45
+ React.useEffect(() => {
46
+ if (enabled && isReady && !sentInitRef.current) {
47
+ init({ mcps, appName });
48
+ sentInitRef.current = true;
49
+ setCurrentMcps(mcps);
50
+ setCurrentAppName(appName);
51
+ }
52
+ }, [enabled, init, mcps, appName, isReady]);
53
+
54
+ // mcpsの更新の仕組み
55
+ React.useEffect(() => {
56
+ if (
57
+ sentInitRef.current &&
58
+ mcps &&
59
+ JSON.stringify(currentMcps) !== JSON.stringify(mcps)
60
+ ) {
61
+ setMcps(mcps);
62
+ setCurrentMcps(mcps);
63
+ }
64
+ }, [mcps]);
65
+
66
+ // appNameの更新の仕組み
67
+ React.useEffect(() => {
68
+ if (sentInitRef.current && appName && currentAppName !== appName) {
69
+ setCurrentAppName(appName);
70
+ setAppName(appName);
71
+ }
72
+ }, [appName]);
73
+
74
+ return (
75
+ <iframe
76
+ ref={setIframeEl}
77
+ src={`${chatbotUrl}/embed/${agentId}`}
78
+ className={className}
79
+ allow="clipboard-write"
80
+ style={{
81
+ width: "100%",
82
+ height: "100%",
83
+ }}
84
+ />
85
+ );
86
+ }
@@ -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 (