@mitodl/smoot-design 3.3.0 → 3.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.
@@ -0,0 +1,28 @@
1
+ import * as React from "react";
2
+ import type { AiChatProps } from "../../components/AiChat/AiChat";
3
+ type ChatInitMessage = {
4
+ type: "smoot-design::chat-open";
5
+ payload: {
6
+ chatId?: AiChatProps["chatId"];
7
+ askTimTitle?: AiChatProps["title"];
8
+ conversationStarters?: AiChatProps["conversationStarters"];
9
+ initialMessages: AiChatProps["initialMessages"];
10
+ apiUrl: AiChatProps["requestOpts"]["apiUrl"];
11
+ };
12
+ };
13
+ type AiChatDrawerProps = {
14
+ className?: string;
15
+ /**
16
+ * The origin of the messages that will be received to open the chat.
17
+ * The drawer will ignore all message events not from this origin.
18
+ */
19
+ messageOrigin: string;
20
+ /**
21
+ * Transform the body of the request before sending it to the server.
22
+ * *This cannot be supplied via message events since the function is not serializable.*
23
+ */
24
+ transformBody?: AiChatProps["requestOpts"]["transformBody"];
25
+ };
26
+ declare const AiChatDrawer: React.FC<AiChatDrawerProps>;
27
+ export { AiChatDrawer };
28
+ export type { AiChatDrawerProps, ChatInitMessage };
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AiChatDrawer = void 0;
4
+ const React = require("react");
5
+ const AiChat_1 = require("../../components/AiChat/AiChat");
6
+ const Drawer_1 = require("@mui/material/Drawer");
7
+ const AiChatDrawer = ({ messageOrigin, transformBody, className, }) => {
8
+ const [open, setOpen] = React.useState(false);
9
+ const [chatSettings, setChatSettings] = React.useState(null);
10
+ React.useEffect(() => {
11
+ const cb = (event) => {
12
+ if (event.origin !== messageOrigin) {
13
+ if (process.env.NODE_ENV === "development") {
14
+ console.warn(`AiChatDrawer: received message from unexpected origin: ${event.origin}`);
15
+ }
16
+ return;
17
+ }
18
+ if (event.data.type === "smoot-design::chat-open") {
19
+ setOpen(true);
20
+ setChatSettings(event.data.payload);
21
+ }
22
+ };
23
+ console.log("Attaching listener");
24
+ window.addEventListener("message", cb);
25
+ return () => {
26
+ window.removeEventListener("message", cb);
27
+ };
28
+ }, [messageOrigin]);
29
+ return (React.createElement(Drawer_1.default, { className: className, PaperProps: {
30
+ sx: {
31
+ width: "600px",
32
+ maxWidth: "100%",
33
+ boxSizing: "border-box",
34
+ padding: "24px 40px",
35
+ ".MitAiChat--title": {
36
+ paddingTop: "0px",
37
+ },
38
+ },
39
+ }, anchor: "right", open: open, onClose: () => setOpen(false) }, chatSettings ? (React.createElement(AiChat_1.AiChat, Object.assign({}, chatSettings, { requestOpts: {
40
+ transformBody,
41
+ apiUrl: chatSettings === null || chatSettings === void 0 ? void 0 : chatSettings.apiUrl,
42
+ }, onClose: () => setOpen(false) }))) : null));
43
+ };
44
+ exports.AiChatDrawer = AiChatDrawer;
@@ -0,0 +1,6 @@
1
+ import type { Meta, StoryObj } from "@storybook/react";
2
+ import { AiChatDrawer } from "./RemoteAiChatDrawer";
3
+ declare const meta: Meta<typeof AiChatDrawer>;
4
+ export default meta;
5
+ type Story = StoryObj<typeof AiChatDrawer>;
6
+ export declare const StreamingResponses: Story;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StreamingResponses = void 0;
4
+ /* eslint-disable react-hooks/rules-of-hooks */
5
+ const React = require("react");
6
+ const RemoteAiChatDrawer_1 = require("./RemoteAiChatDrawer");
7
+ const tiny_invariant_1 = require("tiny-invariant");
8
+ const TEST_API_STREAMING = "http://localhost:4567/streaming";
9
+ const INITIAL_MESSAGES = [
10
+ {
11
+ content: "Hi! What are you interested in learning about?",
12
+ role: "assistant",
13
+ },
14
+ ];
15
+ const STARTERS = [
16
+ { content: "I'm interested in quantum computing" },
17
+ { content: "I want to understand global warming. " },
18
+ { content: "I am curious about AI applications for business" },
19
+ ];
20
+ const meta = {
21
+ title: "smoot-design/AI/RemoteAiChatDrawer",
22
+ component: RemoteAiChatDrawer_1.AiChatDrawer,
23
+ render: () => {
24
+ return (React.createElement(React.Fragment, null,
25
+ React.createElement("iframe", { width: "600px", height: "300px", ref: (el) => {
26
+ var _a;
27
+ if (!el)
28
+ return;
29
+ const doc = el.contentDocument;
30
+ const parent = (_a = el.contentWindow) === null || _a === void 0 ? void 0 : _a.parent;
31
+ (0, tiny_invariant_1.default)(doc && parent);
32
+ const button = doc.createElement("button");
33
+ button.textContent = "Trigger chat (Send message to parent)";
34
+ doc.body.appendChild(button);
35
+ const div = doc.createElement("div");
36
+ doc.body.appendChild(div);
37
+ const label = doc.createElement("label");
38
+ label.textContent = "Message Data:";
39
+ div.appendChild(label);
40
+ const textarea = doc.createElement("textarea");
41
+ div.append(textarea);
42
+ textarea.style["display"] = "block";
43
+ textarea.style["width"] = "100%";
44
+ textarea.style["height"] = "225px";
45
+ const message = {
46
+ type: "smoot-design::chat-open",
47
+ payload: {
48
+ askTimTitle: "for help with Problem: Derivatives 1.1",
49
+ apiUrl: TEST_API_STREAMING,
50
+ initialMessages: INITIAL_MESSAGES,
51
+ conversationStarters: STARTERS,
52
+ },
53
+ };
54
+ textarea.value = JSON.stringify(message, null, 2);
55
+ button.addEventListener("click", () => {
56
+ parent.postMessage(JSON.parse(textarea.value));
57
+ });
58
+ }, title: "button frame" }),
59
+ React.createElement(RemoteAiChatDrawer_1.AiChatDrawer, { messageOrigin: "http://localhost:6006" })));
60
+ },
61
+ };
62
+ exports.default = meta;
63
+ exports.StreamingResponses = {};
@@ -0,0 +1,3 @@
1
+ import type { AiChatDrawerProps } from "./RemoteAiChatDrawer/RemoteAiChatDrawer";
2
+ declare const init: (opts: AiChatDrawerProps) => void;
3
+ export { init };
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.init = void 0;
4
+ const React = require("react");
5
+ const client_1 = require("react-dom/client");
6
+ const RemoteAiChatDrawer_1 = require("./RemoteAiChatDrawer/RemoteAiChatDrawer");
7
+ const ThemeProvider_1 = require("../components/ThemeProvider/ThemeProvider");
8
+ const init = (opts) => {
9
+ const root = document.createElement("div");
10
+ root.id = "smoot-chat-drawer-root";
11
+ document.body.append(root);
12
+ (0, client_1.createRoot)(root).render(React.createElement(ThemeProvider_1.ThemeProvider, null,
13
+ React.createElement(RemoteAiChatDrawer_1.AiChatDrawer, Object.assign({}, opts))));
14
+ };
15
+ exports.init = init;
@@ -0,0 +1,28 @@
1
+ import * as React from "react";
2
+ import type { AiChatProps } from "../../components/AiChat/AiChat";
3
+ type ChatInitMessage = {
4
+ type: "smoot-design::chat-open";
5
+ payload: {
6
+ chatId?: AiChatProps["chatId"];
7
+ askTimTitle?: AiChatProps["title"];
8
+ conversationStarters?: AiChatProps["conversationStarters"];
9
+ initialMessages: AiChatProps["initialMessages"];
10
+ apiUrl: AiChatProps["requestOpts"]["apiUrl"];
11
+ };
12
+ };
13
+ type AiChatDrawerProps = {
14
+ className?: string;
15
+ /**
16
+ * The origin of the messages that will be received to open the chat.
17
+ * The drawer will ignore all message events not from this origin.
18
+ */
19
+ messageOrigin: string;
20
+ /**
21
+ * Transform the body of the request before sending it to the server.
22
+ * *This cannot be supplied via message events since the function is not serializable.*
23
+ */
24
+ transformBody?: AiChatProps["requestOpts"]["transformBody"];
25
+ };
26
+ declare const AiChatDrawer: React.FC<AiChatDrawerProps>;
27
+ export { AiChatDrawer };
28
+ export type { AiChatDrawerProps, ChatInitMessage };
@@ -0,0 +1,41 @@
1
+ import * as React from "react";
2
+ import { AiChat } from "../../components/AiChat/AiChat";
3
+ import Drawer from "@mui/material/Drawer";
4
+ const AiChatDrawer = ({ messageOrigin, transformBody, className, }) => {
5
+ const [open, setOpen] = React.useState(false);
6
+ const [chatSettings, setChatSettings] = React.useState(null);
7
+ React.useEffect(() => {
8
+ const cb = (event) => {
9
+ if (event.origin !== messageOrigin) {
10
+ if (process.env.NODE_ENV === "development") {
11
+ console.warn(`AiChatDrawer: received message from unexpected origin: ${event.origin}`);
12
+ }
13
+ return;
14
+ }
15
+ if (event.data.type === "smoot-design::chat-open") {
16
+ setOpen(true);
17
+ setChatSettings(event.data.payload);
18
+ }
19
+ };
20
+ console.log("Attaching listener");
21
+ window.addEventListener("message", cb);
22
+ return () => {
23
+ window.removeEventListener("message", cb);
24
+ };
25
+ }, [messageOrigin]);
26
+ return (React.createElement(Drawer, { className: className, PaperProps: {
27
+ sx: {
28
+ width: "600px",
29
+ maxWidth: "100%",
30
+ boxSizing: "border-box",
31
+ padding: "24px 40px",
32
+ ".MitAiChat--title": {
33
+ paddingTop: "0px",
34
+ },
35
+ },
36
+ }, anchor: "right", open: open, onClose: () => setOpen(false) }, chatSettings ? (React.createElement(AiChat, Object.assign({}, chatSettings, { requestOpts: {
37
+ transformBody,
38
+ apiUrl: chatSettings === null || chatSettings === void 0 ? void 0 : chatSettings.apiUrl,
39
+ }, onClose: () => setOpen(false) }))) : null));
40
+ };
41
+ export { AiChatDrawer };
@@ -0,0 +1,6 @@
1
+ import type { Meta, StoryObj } from "@storybook/react";
2
+ import { AiChatDrawer } from "./RemoteAiChatDrawer";
3
+ declare const meta: Meta<typeof AiChatDrawer>;
4
+ export default meta;
5
+ type Story = StoryObj<typeof AiChatDrawer>;
6
+ export declare const StreamingResponses: Story;
@@ -0,0 +1,60 @@
1
+ /* eslint-disable react-hooks/rules-of-hooks */
2
+ import * as React from "react";
3
+ import { AiChatDrawer } from "./RemoteAiChatDrawer";
4
+ import invariant from "tiny-invariant";
5
+ const TEST_API_STREAMING = "http://localhost:4567/streaming";
6
+ const INITIAL_MESSAGES = [
7
+ {
8
+ content: "Hi! What are you interested in learning about?",
9
+ role: "assistant",
10
+ },
11
+ ];
12
+ const STARTERS = [
13
+ { content: "I'm interested in quantum computing" },
14
+ { content: "I want to understand global warming. " },
15
+ { content: "I am curious about AI applications for business" },
16
+ ];
17
+ const meta = {
18
+ title: "smoot-design/AI/RemoteAiChatDrawer",
19
+ component: AiChatDrawer,
20
+ render: () => {
21
+ return (React.createElement(React.Fragment, null,
22
+ React.createElement("iframe", { width: "600px", height: "300px", ref: (el) => {
23
+ var _a;
24
+ if (!el)
25
+ return;
26
+ const doc = el.contentDocument;
27
+ const parent = (_a = el.contentWindow) === null || _a === void 0 ? void 0 : _a.parent;
28
+ invariant(doc && parent);
29
+ const button = doc.createElement("button");
30
+ button.textContent = "Trigger chat (Send message to parent)";
31
+ doc.body.appendChild(button);
32
+ const div = doc.createElement("div");
33
+ doc.body.appendChild(div);
34
+ const label = doc.createElement("label");
35
+ label.textContent = "Message Data:";
36
+ div.appendChild(label);
37
+ const textarea = doc.createElement("textarea");
38
+ div.append(textarea);
39
+ textarea.style["display"] = "block";
40
+ textarea.style["width"] = "100%";
41
+ textarea.style["height"] = "225px";
42
+ const message = {
43
+ type: "smoot-design::chat-open",
44
+ payload: {
45
+ askTimTitle: "for help with Problem: Derivatives 1.1",
46
+ apiUrl: TEST_API_STREAMING,
47
+ initialMessages: INITIAL_MESSAGES,
48
+ conversationStarters: STARTERS,
49
+ },
50
+ };
51
+ textarea.value = JSON.stringify(message, null, 2);
52
+ button.addEventListener("click", () => {
53
+ parent.postMessage(JSON.parse(textarea.value));
54
+ });
55
+ }, title: "button frame" }),
56
+ React.createElement(AiChatDrawer, { messageOrigin: "http://localhost:6006" })));
57
+ },
58
+ };
59
+ export default meta;
60
+ export const StreamingResponses = {};
@@ -0,0 +1,3 @@
1
+ import type { AiChatDrawerProps } from "./RemoteAiChatDrawer/RemoteAiChatDrawer";
2
+ declare const init: (opts: AiChatDrawerProps) => void;
3
+ export { init };
@@ -0,0 +1,12 @@
1
+ import * as React from "react";
2
+ import { createRoot } from "react-dom/client";
3
+ import { AiChatDrawer } from "./RemoteAiChatDrawer/RemoteAiChatDrawer";
4
+ import { ThemeProvider } from "../components/ThemeProvider/ThemeProvider";
5
+ const init = (opts) => {
6
+ const root = document.createElement("div");
7
+ root.id = "smoot-chat-drawer-root";
8
+ document.body.append(root);
9
+ createRoot(root).render(React.createElement(ThemeProvider, null,
10
+ React.createElement(AiChatDrawer, Object.assign({}, opts))));
11
+ };
12
+ export { init };